UIAlertcontroller是Swift中的一个动作

Mat*_*ats 1 ios swift uialertcontroller

所以我想要一个警告弹出告诉我一条消息,然后我希望它等我,直到我按OK然后它将继续该功能.例如.

@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)
}
Run Code Online (Sandbox Code Playgroud)

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

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

提前致谢!

Mid*_* MP 11

您需要将代码放入OK按钮处理程序,而不是放在nil那里.

更改代码:

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

}
Run Code Online (Sandbox Code Playgroud)