UIAlertcontroller 作为 Swift 中的一个动作

UIAlertcontroller as an action in Swift

所以我想弹出一个警报告诉我一条消息,然后我希望它等我,直到我按确定,然后它会继续执行该功能。例如。

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

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

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

提前致谢!

您需要将代码放入确定按钮处理程序,而不是将 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)

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