来自其他 class 的函数回调

callback on function from other class

我有 customView,还有 UITextField,在我的 ViewController 中,我使用了很多这个视图。所以在 customView.swift 中我有 UITextFieldDelegate。但是我想在 ViewController 中对这个回调做一些操作。例子 : 当我点击文本字段时,委托执行

func textFieldDidBeginEditing(textField: UITextField) {
    println("delegate")
}

然后我想要执行一些与 ViewController UI 一起使用的函数,例如:

func textFieldDidBeginEditing(textField: UITextField) {
        println("delegate")
        ViewController().check()
}

但是它变成了零,所以我想创建一些回调,你能建议正确的方法吗?

在您的 customView class 中创建类型 ViewController 的自定义 属性。在ViewControllerviewDidLoad函数中,调用

theCustomView.viewController = self

(其中 theCustomView 是实例 - 您可能也有对此的引用,否则创建一个 IBOutlet)

然后,在textFieldDidBeginEditing函数中你可以使用

self.viewController.check()