[英]Swift - UIAlertController post action
我有一個UIAlertController實例,一旦用戶單擊按鈕,該實例就會彈出。 用戶將在警報視圖中輸入他/她的全名,然后按確認。 我將此值存儲到數據庫中。
當數據庫中的保存操作完成時,我想顯示另一個警報控制器。 我嘗試在通話的完成部分中執行此操作,但這沒有用。 這是我嘗試的代碼:
@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)
}
當執行以上代碼時,出現此錯誤:
不允許在取消分配時嘗試加載視圖控制器的視圖,這可能導致未定義的行為
知道發生了什么,如何解決? 我應該采用其他方法嗎?
謝謝,
如果成功,將方法“ displayNameChangeConfirmation”的調用從完成任務更改為“確認”操作
當前情況下,您嘗試在顯示已經顯示的第一個控制器之后立即顯示第二個控制器
更改此行
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()
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.