用户在第一个alertViewController中选择操作后,我尝试显示新警报。请在下面找到我的代码。
@IBAction func forgotPassword(sender : AnyObject){
//1. Create the alert controller.
let alert = UIAlertController(title: "Forgot Password?", message: "We'll email a link to reset it.", preferredStyle: .Alert)
//2. Add the text field. You can configure it however you need.
alert.addTextFieldWithConfigurationHandler({ (textField) -> Void in
textField.placeholder = "Email Address"
})
//3. Grab the value from the text field, and print it when the user clicks OK.
alert.addAction(UIAlertAction(title: "Send", style: .Default, handler: { (action) -> Void in
let textField = alert.textFields![0] as UITextField
let email = textField.text!
FIRAuth.auth()?.sendPasswordResetWithEmail(email) { error in
if error != nil {
// An error happened.
} else {
print("error is nil")
if self.presentedViewController != nil {
self.presentedViewController?.dismissViewControllerAnimated(true, completion: {
print("inside completion")
let alertController = UIAlertController(title: "Email Sent!", message: "Please check you email and follow the password reset instructions", preferredStyle: .Alert)
let action = UIAlertAction(title: "Done", style: .Default, handler: nil)
alertController.addAction(action)
self.presentViewController(alertController, animated: true, completion: nil)
})
}
}
}
}))
// 4. Present the alert.
self.presentViewController(alert, animated: true, completion: nil)
}
无论我在完成块中编写了什么代码,它都没有被执行,因此第二个警报没有显示出来。请帮我解决这个问题。
答案 0 :(得分:0)
似乎UIAlertView在行动后会自行解散。
//3. Grab the value from the text field, and print it when the user clicks OK.
alert.addAction(UIAlertAction(title: "Send", style: .Default, handler: { (action) -> Void in
let textField = alert.textFields![0] as UITextField
guard let email = textField.text else {
return
}
FIRAuth.auth()?.sendPasswordResetWithEmail(email) { error in
if let error = error {
print(error.localizedDescription)
return
}
let alertController = UIAlertController(title: "Email Sent!", message: "Please check you email and follow the password reset instructions", preferredStyle: .Alert)
let action = UIAlertAction(title: "Done", style: .Default, handler: nil)
alertController.addAction(action)
self.presentViewController(alertController, animated: true, completion: nil)
}
}))
答案 1 :(得分:-2)
在完成处理程序中尝试这个。
dispatch_async(dispatch_get_main_queue(),^ {
});