UIAlertcontroller as an action in Swift

与世无争的帅哥 提交于 2019-12-11 11:14:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!