UIAlertController作为Swift中的一个动作

4

我希望弹出一个警告框,告诉我一条信息,然后等待我按下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)
}

问题在于现在它什么也不做,如果我之后只是放进一个函数,它会直接执行,而不是等到我按下“确定”按钮。有人知道怎么做吗?顺便说一句,在这个例子中,消息将是标题、消息,我知道这不是我在这种情况下需要的 ;) 预先感谢!

你的意思是在按下“确定”按钮后需要执行一个任务吗? - Midhun MP
是的,完全正确 @MidhunMP - Mats
2个回答

16

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

}

1
let alertController = UIAlertController(title: "Alert", message: "Please fill Bill Total, Select Tip, Choose Split", preferredStyle:.alert)

alertController.addAction(UIAlertAction(title: "OK", style: .default)
          { action -> Void in
            // Put your code here
          })
self.present(alertController, animated: true, completion: nil)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接