使用 Swift 中的另一个 class 配置操作处理程序

Configure action Handler with another class in Swift

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

我想添加与 AlertController 相同的 Action Handler 功能。

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"被调用时,它会打印"done"??

我怎样才能做到这一点?

谢谢,

观察您的代码,我假设您已经在 CustomAlertController 中定义了 closer class

喜欢-

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

你做的一切都正确,只是当你按下 ok 按钮时你必须把它叫得更近。

喜欢-

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

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