Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertcontroller as an action in Swift

So I want to have an alert popping up telling me a message, then I want it to wait for me until i press OK and then it will continue with the function. For example.

@IBAction Alert() {
    let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
    alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
    self.presentViewController(alertController, animated: true, completion: nil)
}

The problem is that now it won't do anything and if I just put in a function afterwards it does it directly, not waiting until I've pressed OK. Does anyone know how to do this?

By the way, in this example the message would be title, message, which I'm aware of and thats not what i need in this case ;)

Thanks in advance!

like image 633
Mats Avatar asked Dec 02 '22 18:12

Mats


1 Answers

You need to put your code into the OK button handler, instead of putting nil there.

Change the code:

@IBAction func Alert()
{
   let alertController = UIAlertController(title: title, message: message, preferredStyle:UIAlertControllerStyle.Alert)

   alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default)
   { action -> Void in
     // Put your code here
   })
   self.presentViewController(alertController, animated: true, completion: nil)

}
like image 129
Midhun MP Avatar answered Dec 24 '22 09:12

Midhun MP