AlertController - 与 UIViewController 冲突

Posted

技术标签:

【中文标题】AlertController - 与 UIViewController 冲突【英文标题】:AlertController - conflict with UIViewController 【发布时间】:2016-05-01 14:07:59 【问题描述】:

我正在使用 Swift 开发一个 ios 项目。我有简单的登录/注册/丢失密码视图控制器,以确保使用 Firebase 的安全性。问题在于重置密码视图控制器。如果用户单击它,它们将被发送(以模态方式呈现)到丢失密码视图控制器。

当前代码的问题在于,当 Firebase 找到输入的电子邮件并发送密码重置电子邮件时,我会提供警报控制器以供用户确认。问题是当我在警报控制器上单击“确定”时,我希望重置密码视图控制器也被解除。不知道为什么它现在不起作用。我确实收到了电子邮件,但是当我单击警报控制器上的确定按钮时,它只会关闭警报控制器,self.dismissViewControllerAnimated(true, completion: nil) 似乎并没有关闭模态呈现的重置密码视图控制器。

我试过self.dismissViewController(true, completion: nil)self.performSegueWithIdentifier("goToLoginVC", sender: nil)。非似乎工作,我不知道为什么。

这是函数本身:

    @IBAction func resetPasswordPressed(sender: AnyObject) 

    let email = emailTextField.text

    if email != "" 

         DataService.ds.REF_BASE.resetPasswordForUser(email, withCompletionBlock:  error in

            if error != nil 

                // Error - Unidentified Email
                showAlert(title: "Unidentified Email Address", msg: "Please, re-enter the email you have registered with.", actionButton: "OK", viewController: self)

             else 

                // Success - Sent recovery email

                let alertController = UIAlertController(title: "Email Sent", message: "An email has been sent. Please, check your email now.", preferredStyle: UIAlertControllerStyle.Alert)
                let okAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
                alertController.addAction(okAction)
                self.presentViewController(alertController, animated: true, completion: nil)

                self.dismissViewControllerAnimated(true, completion: nil)
            

         )

     else 

        showAlert(title: "Error!", msg: "Email is required in order to reset your password. Please, enter your email. ", actionButton: "OK", viewController: self)
    

【问题讨论】:

【参考方案1】:

这样做:

alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle. Default, handler:  action in

   //Add your logic here

))

【讨论】:

【参考方案2】:

有人回答并提供了解决方案,但在此之后删除了他的答案。所以,这就是他所说的,而且效果很好(所以归功于那个人!):

    @IBAction func resetPasswordPressed(sender: AnyObject) 

    let email = emailTextField.text

    if email != "" 

         DataService.ds.REF_BASE.resetPasswordForUser(email, withCompletionBlock:  error in

            if error != nil 

                // Error - Unidentified Email
                showAlert(title: "Unidentified Email Address", msg: "Please, re-enter the email you have registered with.", actionButton: "OK", viewController: self)

             else 

                // Success - Sent recovery email

                let alertController = UIAlertController(title: "Email Sent", message: "An email has been sent. Please, check your email now.", preferredStyle: UIAlertControllerStyle.Alert)

                alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler:  action in

                    self.dismissViewControllerAnimated(true, completion: nil)

                ))
                self.presentViewController(alertController, animated: true, completion: nil)
            

         )

     else 

        showAlert(title: "Error!", msg: "Email is required in order to reset your password. Please, enter your email. ", actionButton: "OK", viewController: self)
    

【讨论】:

我以为你不需要那个.. :) 这正是我所需要的!谢谢! 很高兴为您提供帮助.. :)

以上是关于AlertController - 与 UIViewController 冲突的主要内容,如果未能解决你的问题,请参考以下文章

内存泄漏AlertController / UIImagePickerController

如何在 alertController 之后更新 tableView?

在通过代码呈现的另一个 ViewController 上显示 AlertController

在 FCMPlugin Ionic 2 中调用时,AlertController 不起作用

Swift 2 - 当 AlertController 出现时如何切换到另一个 ViewController?

膨胀类 android.support.v7.app.AlertController.RecycleListView 时出错