问题
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!
回答1:
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)
}
来源:https://stackoverflow.com/questions/27880588/uialertcontroller-as-an-action-in-swift