Swifter Swifter - 1 year ago 106
Swift Question

AlertController - conflict with UIViewController

I'm working on an iOS project using Swift. I have simple login/register/lost password view controllers for security using Firebase. The issue is with the Reset Password View Controller. If a user clicks on it, they are sent (presented modally) to lost password view controller.

The problem with the current code is that when Firebase finds the entered email and sends a password reset email, I present Alert controller for confirmation for the user. The issue is that when I click "ok" on the Alert Controller, I want the Reset Password View Controller to be dismissed as well. Not sure why it's not working now. I do get emails, but when I click the OK button on the Alert controller, it only dismisses the Alert controller and the

self.dismissViewControllerAnimated(true, completion: nil)
doesn't seem to dismiss the modally presented Reset Password View Controller.

I tried with
self.dismissViewController(true, completion: nil)
as well as
self.performSegueWithIdentifier("goToLoginVC", sender: nil)
. Non seems to work and I can't figure why.

Here's the function itself:

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

Answer Source

Do it this way:

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

   //Add your logic here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download