在 UIAlert 中按下 Ok 时如何调用函数

How to Call a Function when Ok is pressed in an UIAlert

我尝试简单地调用一个名为 "GoToNewShoot" 的函数,当用户按下 "ok" 按钮时弹出警报谢谢!

代码:

   @IBAction func GobacktoCamera(sender: AnyObject) {
    var alertController = UIAlertController(title: "Just Checking", message: "Are You Sure You Want to Start over ", preferredStyle: UIAlertControllerStyle.Alert)
    alertController.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Cancel, handler: nil))
    self.presentViewController(alertController, animated: true, completion: nil)

您可以通过使用 addAction 中的处理程序来完成此操作:

@IBAction func GobacktoCamera(sender: AnyObject) {

    var alertController = UIAlertController(title: "Just Checking", message: "Are You Sure You Want to Start over", preferredStyle: UIAlertControllerStyle.Alert)

    alertController.addAction(UIAlertAction(title: "Yes", style: .Default, handler: { action in
        //run your function here
        self.GoToNewShoot()
    }))

    self.presentViewController(alertController, animated: true, completion: nil)
}


func GoToNewShoot(){

    println("Method Called")
}
alerty.addAction(UIAlertAction(title: "Just Checking",
                      style: UIAlertActionStyle.Default,
                    handler: {(alert: UIAlertAction!) in print("Are You Sure You Want to Start over")}))