Swift UITextField 子类以编程方式处理文本更改 [英] Swift UITextField subclass handle text change programmatically

查看:42
本文介绍了Swift UITextField 子类以编程方式处理文本更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITextField 的子类,专门用于处理日期文本.我有一个使用此文本字段的 tableviewcell:

I have a subclass of UITextField that is specific to handle Date text. I have a tableviewcell that uses this text field:

let dateInput: DateTextField

现在控制器需要在显示之前初始化dateInput的文本如下:

Now the controller needs to initialize the text of the dateInput prior to display as follows:

cell.dateInput.text = "01/29/2016"

现在,我希望能够检测到从子类更改的文本,以便我可以更新内部日期变量,使其与文本同步.

Now, I want to be able to detect that the text changed from the subclass so that I can update the internal date variable so that is it in-sync with the text.

我实现了文本字段委托方法,但它只捕获用户所做的更改,而不是以编程方式进行的.

I implemented the textfield delegate methods but that just catches changes made by the user and not programmatically.

推荐答案

您可以覆盖属性并在您的自定义类中添加 didSet 观察者:

You can override property and add didSet observer in your custom class:

class DateTextField: UITextField {

    override var text: String? {
        didSet {
           // Do your stuff here    
        }
    }   
}

这篇关于Swift UITextField 子类以编程方式处理文本更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆