user5034511
user5034511

Reputation:

Can't add an Alert Action to UIAlertController (Swift)

I'm trying to add an Ok button to a UI Alert but I cannot add it using alertController.addAction

What do I do in this case?

Thanks in advance!!

if error == nil {
                let alert: UIAlertController = UIAlertController(title: "Account Created", message: "Please confirm your email", preferredStyle: .Alert)

                let okButton = UIAlertAction(title: "Ok", style: .Default) { action -> Void in
                    self.performSegueWithIdentifier("toMain", sender: self)

                    alertController.addAction(okButton)


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

                }

                } else {

                println("\(error)")
            }

Upvotes: 1

Views: 1163

Answers (1)

iRiziya
iRiziya

Reputation: 3245

  1. alert instead of alertController
  2. alert.addAction should be outside of your okbutton action.

Change your code with :

if error == nil {
    let alert: UIAlertController = UIAlertController(title: "Account Created", message: "Please confirm your email", preferredStyle: .Alert)

    let okButton = UIAlertAction(title: "Ok", style: .Default) { action -> Void in
        self.performSegueWithIdentifier("toMain", sender: self)

    }
    alert.addAction(okButton)

    self.presentViewController(alert, animated: true, completion: nil)
 } else {

            println("\(error)")
        }

Upvotes: 1

Related Questions