2016-01-01 38 views
1

我有一个UIAlertController实例,一旦用户点击一个按钮就会弹出给用户。用户将在警报视图中输入他/她的全名,然后按确认。我将这个值存储到数据库中。Swift - UIAlertController post action

我想在保存操作在数据库中完成时显示另一个警报控制器。我试图在通话的完成部分做到这一点,但没有奏效。这里是我试过的代码:

@IBAction func changeNamePressed(sender: AnyObject) { 
     let alertController = UIAlertController(title: "Change Full Name", message: "Please enter your name as you want it displayed in your profile", preferredStyle: .Alert) 

     let confirmAction = UIAlertAction(title: "Confirm", style: .Default) { (_) in 
      if let field = alertController.textFields![0] as? UITextField { 
       // store your data 
       self.userObject.name = field.text 
       self.nameLabel.text = field.text 
       SwiftSpinner.show("Updating Full Name") 
       let errorFound: NSError? = self.utilities.changeFullName(field.text!) 
       SwiftSpinner.hide() 
       if(errorFound?.domain != "") 
       { 
        print("Error Updating Name Change: \(errorFound?.description)") 
       }else{ 
        print("No Errors Found") 
       } 
      } else { 
       // user did not fill field 
      } 
     } 
     let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (_) in } 
     alertController.addTextFieldWithConfigurationHandler { (textField) in 
      textField.placeholder = "Your Full Name" 
     } 
     alertController.addAction(confirmAction) 
     alertController.addAction(cancelAction) 
     self.presentViewController(alertController, animated: true, completion: displayNameChangeConfirmation) 

    } 

    func displayNameChangeConfirmation() 
    { 
     let alert = UIAlertController(title: "Profile Updated", message: "Your full name has been successfully updated.", preferredStyle: UIAlertControllerStyle.Alert) 
     alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) 
     self.presentViewController(alert, animated: true, completion: nil) 

    } 

在执行上面的代码时,我得到这个错误:

Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior

任何想法是怎么回事,它如何能解决吗?我应该采取不同的方法吗?

感谢,从完成任务的方法“displayNameChangeConfirmation”

回答

1

改变电话确认动作,在成功的情况下

当前的情况下,试图展示一日一这已经是后立即显示第2个控制器表示

+0

能否请您详细阐述更多...得到了由答案 –

+0

困惑让我修改,以显示你应该叫方法 –

0

改变这一行

self.presentViewController(alertController, animated: true, completion: displayNameChangeConfirmation) // 

行打印(“未发现错误”)之后
self.presentViewController(alertController, animated: true, completion: nil) // this completion handler is called immediately once alert is appeared so put it nil. 

呼叫displayNameChangeConfirmation如下:

 print("No Errors Found" + field.text!) 
     dispatch_after(1, dispatch_get_main_queue(), { 
      self.displayNameChangeConfirmation() 
     }) 
相关问题