2016-08-10 70 views
2

我試圖在用戶選擇第一個alertViewController中的動作之後呈現新的警報。請在下面找到我的代碼。在解散先前的AlertViewController後呈現新的AlertViewController - Swift

@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

似乎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) 

    } 
})) 
+0

我已經嘗試過這個,它的工作原理,但會得到很多像下面的警告。此應用程序正在修改自動佈局引擎從後臺線程,這可能會導致引擎損壞和奇怪的崩潰。這將在未來的版本中引發異常。 –

+0

你有什麼想法嗎? –

-2

嘗試完成處理程序中的這一個。

dispatch_async(dispatch_get_main_queue(),^ {

});

+0

完成處理程序中 –

相關問題