AlertController - 与UIViewController冲突

时间:2016-05-01 14:07:59

标签: ios swift uiviewcontroller uialertcontroller

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

当前代码存在的问题是,当Firebase找到输入的电子邮件并发送密码重置电子邮件时,我会提供警报控制器以确认该用户。问题是,当我点击" ok"在警报控制器上,我想要重置密码视图控制器也被解雇。不知道为什么它现在不起作用。我收到了电子邮件,但是当我点击警报控制器上的“确定”按钮时,它只会关闭警报控制器,而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)
    }
}

2 个答案:

答案 0 :(得分:0)

这样做:

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

   //Add your logic here

}))

答案 1 :(得分:0)

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

    @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)
    }
}