gpt4 book ai didi

ios - 听 UITextField setText

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:04:16 27 4
gpt4 key购买 nike

我有一个从不同模块更新的 UITextField(我将我的 UITextField 传递给上面提到的模块以填充它)。我需要一种方法来识别 UITextField 的文本值何时在 UITextField 最初所在的模块内更改。 UITextField 委托(delegate)方法或 UIControlEvents 将不起作用,因为文本是以编程方式填充的。

最佳答案

将以下内容放在某处,例如 -viewDidLoad:

[textField addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil];

然后添加一个 -observeValueForKeyPath,例如:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (object == textField)
{
// Do whatever with your text field here
}
}

这只会在以编程方式设置文本时调用,而不是在用户在字段中键入时调用。

或者只是子类化 UITextField 并传入你的子类。你可以根据需要覆盖 setText:

关于ios - 听 UITextField setText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25326857/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com