UIAlertcontroller作为Swift中的一个动作

所以我想要popup一个警告,告诉我一个消息,然后我希望它等待我,直到我按下确定,然后它将继续与该function。 例如。

@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) } 

问题是,现在它不会做任何事情,如果我只是把它放在一个函数,它直接做,而不是等到我按下OK。 有谁知道如何做到这一点?

顺便说一下,在这个例子中,这个消息将是标题,消息,我知道,这不是我需要在这种情况下;)

提前致谢!

您需要将您的代码放入OKbutton处理程序,而不是放在那里。

更改代码:

 @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) }