如何仅覆盖 UIControlEvent.allEditingEvents 的 UITextField 中的 addTarget?

How can I override addTarget in a UITextField for only UIControlEvent.allEditingEvents?

如何覆盖 UITextField class 中的 addTarget 函数,但仅限于 UIControlEvent.allEditingEvents

功能是这样的,但是我不确定如何只对特定的控制事件应用它。

override func addTarget(_ target: Any?, action: Selector, for controlEvents: UIControlEvents) {
    <#code#>
}

您应该调用 super.addTarget,并且仅当 controlEvents 等于 .allEditingEvents 时才调用您的自定义代码。

override func addTarget(_ target: Any?, action: Selector, for controlEvents: UIControlEvents) {
    super.addTarget(target, action: action, for: controlEvents)

    if controlEvents == .allEditingEvents {
        [your code]
    }
}