在Swift中使用另一个类配置动作处理程序

时间:2017-02-23 09:16:19

标签: ios iphone swift xcode uialertcontroller

我尝试将自定义控件与AlertController相同。

我想将函数添加为 Action Handler ,与A​​lertController相同。

ViewController.swift

@IBAction func showcustomAlert(){
        let story = UIStoryboard(name: "Main", bundle: Bundle.main)
        let CustomAlertController = story.instantiateViewController(withIdentifier: "CustomAlertController") as! CustomAlertViewController
        CustomAlertController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext

        CustomAlertController.addOKaction  = { print("done")} //Main issue
        self.present(CustomAlertController, animated: true) {

        }
    }

CustomAlertController.swift

@IBAction func OkPressed(){

}

如何在customAlertController中定义hander,所以当" okPressed"叫它,它会打印"完成" ??

我怎样才能做到这一点?

谢谢,

1 个答案:

答案 0 :(得分:2)

观察你的代码我假设你已经在CustomAlertController类中定义了更近的代码

像 -

var addOKaction : (() -> ())?

你完成了所有事情,只需按下确定按钮就可以调得更近。

喜欢 -

@IBAction func OkPressed(){
 self.addOKaction()
}

当按下确定按钮时,您将收到回叫。