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

Hun*_*ter 12 uialertview swift

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

码:

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

Dha*_*esh 24

您可以通过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")
}
Run Code Online (Sandbox Code Playgroud)