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

标签 swift uialertview

我尝试简单地调用一个名为“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)

最佳答案

您可以通过使用 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")
}

关于swift - 在 UIAlert 中按下 Ok 时如何调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32535495/

相关文章:

swift - 如何从 UnsafeMutableRawPointer 中获取字节?

ios - 当我从 Swift 调用 objective-c 函数时,这些函数中的 NSLog 什么都不做

ios - 显示 UIActionSheet 后,UIAlertView 未触发

ios - 从编辑表单返回时更新 UITableView,刷新整个表数据源或被编辑的行?

cocoa - fatal error : unexpectedly found nil while unwrapping an Optional value in Swift when tried to parse JSON

swift - addsubview View 不可点击

ios - UIDatePicker 自定义

ios - Swift 自定义 UIAlertView

ios - 当 ViewController 已经有委托(delegate)时如何设置委托(delegate)?

iOS 关闭警报 打开文本字段