在关闭以前的 AlertViewController 后呈现新的 AlertViewController - Swift

Posted

技术标签:

【中文标题】在关闭以前的 AlertViewController 后呈现新的 AlertViewController - Swift【英文标题】:Presenting new AlertViewController after dismissing the previous AlertViewController - Swift 【发布时间】:2016-08-10 03:25:23 【问题描述】:

我试图在用户选择第一个 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)

我在完成块中编写的任何代码都不会被执行,因此不会显示第二个警报。请帮我解决这个问题。

【问题讨论】:

【参考方案1】:

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)

    
))

【讨论】:

我以前试过这个,它可以工作,但会收到很多类似下面的警告。这个应用程序正在从后台线程修改自动布局引擎,这可能导致引擎损坏和奇怪的崩溃。这将在未来的版本中导致异常。 你还有什么想法吗?【参考方案2】:

在完成处理程序中尝试这个。

dispatch_async(dispatch_get_main_queue(), ^

);

【讨论】:

完成处理程序内部

以上是关于在关闭以前的 AlertViewController 后呈现新的 AlertViewController - Swift的主要内容,如果未能解决你的问题,请参考以下文章

关闭以前的 VC 后仅重新加载新数据

如何正确关闭以前的视图控制器

使用 Swift 呈现新的 Viewcontroller 后如何关闭以前的 Viewcontroller?

关闭 ViewController 后,我的视图保持在 iOS7 中以前的视图控制器方向

如果用户忘记注销并关闭浏览器,如何删除 Rails 中以前的会话?

java 关闭所有以前的活动