swift - 如何在其子类中观察 UITexFiled startEditing 和 EndEditing?

标签 swift uitextfield

如何在 UITexFiled 子类中观察,如果当前 textField 在同一个类中开始结束或结束编辑。

如果我写 self.delegate = self 那么我的 ViewController UITextField 方法将不会被调用。

我想制作一个 UITexFiled SubClass 当每次编辑开始时,我将放大 textFiled ,当编辑完成时,然后 UITexField 将恢复正常大小。但我想从我的子类中处理,不要在所有 ViewController 中编写逻辑每个 UITexFeildDelegate 请帮助我。

最佳答案

您可以为特定事件添加目标

class MyTextField: UITextField {
    override init(frame: CGRect) {
        super.init(frame:frame)
        commonInit()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }
    func commonInit() {
        self.addTarget(self, action: #selector(didBegin), for: .editingDidBegin)
        self.addTarget(self, action: #selector(didEnd), for: .editingDidEnd)
    }
    @objc func didBegin() {

    }
    @objc func didEnd() {

    }
}

关于swift - 如何在其子类中观察 UITexFiled startEditing 和 EndEditing?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55569149/

相关文章:

ios - 封装iOS逻辑

swift - NSLocalizedString 中参数的不同字体

iphone - 如何在 iPhone 应用程序中垂直居中显示 UITextField 中的文本

ios - 在 UITextFields 中放置一个字符范围

swift - 在 Swift 中计算 UITextInput 文本值长度的最佳方法?

ios - UIAlertView 保存 textfield.tex

ios - 多个完成 block 完成后执行的函数

ios - 设置UIImageView的tintColor为多种颜色

swift - 协议(protocol) 'BaseListPresenter' 只能用作通用约束,因为它具有 Self 或关联类型要求

ios - '位置管理器(_ :didRangeBeacons:inRegion: )' conflicts with optional requirement method ' locationManager(_:didRangeBeacons:inRegion:)' in protocol