Samuel Krut
Samuel Krut

Reputation: 31

UIAlertController addAction not working

So the UIAlertController is displaying the alert on the screen correctly, but in the xCode simulator pressing ok does nothing and it doesn't leave the alert. alert view image

    func displayAlertMessage(userMessage:String) {

    let myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert);

    let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil);

    myAlert.addAction(okAction);

    self.presentViewController(myAlert, animated: true, completion: nil);

}

Upvotes: 0

Views: 728

Answers (1)

Sahil
Sahil

Reputation: 9226

change UIAlertActionStyle.Default to UIAlertActionStyle.Cancel

func displayAlertMessage(userMessage:String) {

let myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert);

let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Cancel, handler: nil);

myAlert.addAction(okAction);

self.presentViewController(myAlert, animated: true, completion: nil);

}

and if you want to do something in ok

 let okAction = UIAlertAction(title: "OK", style: .Default) { (action) in
      //do whatever you want to do here
     // for e.g dismiss view controller or segue your view controller etc.
    }

Upvotes: 1

Related Questions