本文介绍了在关闭之前的 AlertViewController 后呈现新的 AlertViewController - Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在用户选择第一个 alertViewController 中的操作后呈现新警报.请在下面找到我的代码.
I'm trying to present new alert after user chooses the action in first alertViewController. Please find my code below.
@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)
}
我在完成块中编写的任何代码都不会执行,因此不会显示第二个警报.请帮我解决这个问题.
Whatever code I have written in completion block its not getting executed so the second alert is not getting displayed. Please help me to resolve this.
推荐答案
UIAlertView 似乎在其操作后会自行关闭.
It seems that the UIAlertView will dismiss itself after its action.
//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)
}
}))
这篇关于在关闭之前的 AlertViewController 后呈现新的 AlertViewController - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!