matthewdossantos matthewdossantos - 4 months ago 19
iOS Question

Alert Popup doesnt show

I have some code in my project:

@IBAction func createAccountAction(sender: AnyObject) {
if self.emailField.text == "" || self.passwordField.text == ""
{
let alertController = UIAlertController(title: "Oops!", message: "Please enter an email and password.", preferredStyle: .Alert)

let defaultAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
alertController.addAction(defaultAction)

self.presentViewController(alertController, animated: true, completion: nil)
}
else
{
FIRAuth.auth()?.createUserWithEmail(self.emailField.text!, password: self.passwordField.text!) { (user, error) in

if error == nil
{
let alertController = UIAlertController(title: "Done", message: "Account created!", preferredStyle: .Alert)

let defaultAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
alertController.addAction(defaultAction)
self.emailField.text = ""
self.passwordField.text = ""



}
else
{
let alertController = UIAlertController(title: "Oops!", message: error?.localizedDescription, preferredStyle: .Alert)

let defaultAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
alertController.addAction(defaultAction)

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


As you can see in the last else statement I have a alertController with the message
Oops
that will show when clicking the createAccountAction button.

But that happens when the user presses the button and has not filled in anything in the textfields.

Now what I want is that when a user succesfully fills in the textfields, that a same popup should appear along with other text that I specified.

When I run the code it does the part

self.emailField.text = ""
self.passwordField.text = ""


but does not present the AlertController.

How can I achieve what I want?

Answer

You're missing the line:

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

which should be after:

self.emailField.text = ""
self.passwordField.text = ""

So you aren't presenting the alert.

Also. You can change that entire function to this much simpler one:

@IBAction func createAccountAction(sender: AnyObject) {

   if self.emailField.text == "" || self.passwordField.text == "" {

        let title = "Oops"
        let message = "Please enter an email and password."

        let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
        let defaultAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
        alertController.addAction(defaultAction)
        self.presentViewController(alertController, animated: true, completion: nil)

    } else {
        FIRAuth.auth()?.createUserWithEmail(self.emailField.text!, password: self.passwordField.text!) { (user, error) in

            var title : String
            var message : String

            if error == nil {
                title = "Done"
                message = "Account created!"
                self.emailField.text = ""
                self.passwordField.text = ""
            } else {
                title = "Oops!"
                message = "error?.localizedDescription"
            }

        let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
        let defaultAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
        alertController.addAction(defaultAction)
        self.presentViewController(alertController, animated: true, completion: nil)

        }
    }
}