2016-01-01 59 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() 
     }) 
相關問題