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

时间:2017-12-29 18:16:34

标签: ios swift uitextfield uicontrolevents addtarget

如何覆盖addTarget课程中的UITextField功能,但仅适用于UIControlEvent.allEditingEvents

函数是这样的,但是我不知道如何仅将它应用于特定的控件事件。

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

1 个答案:

答案 0 :(得分:2)

如果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]
    }
}