UIAlertcontroller作为Swift中的一个动作

时间:2015-01-10 19:57:28

标签: 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)
}

问题在于现在它不会做任何事情,如果我之后直接输入一个函数就直接执行它,而不是等到我按下OK之后。有谁知道怎么做?

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

提前致谢!

1 个答案:

答案 0 :(得分:10)

您需要将代码放入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)

}