我已经实现了一个委托(delegate)及其 controlTextDidChange
方法,只要我在 NSTextField
中输入文本,它就会被调用。
当我通过 [field setStringValue:@"newText"]
以编程方式设置文本时,不会调用 controlTextDidChange
。
你能解释一下吗?以及我应该如何以编程方式设置触发调用我的委托(delegate)方法 controlTextDidChange
的新值。
最佳答案
controlTextDidChange
委托(delegate)仅在用户交互时触发,
手动更新 NSControl
不会触发委托(delegate)。
正确的解决方案
解决方案是创建一个单独的方法来处理文本更改:
- (void)myCustomFunction {
// Triggered in case the text is changed,
// no matter by user or programmatically
}
从委托(delegate)中触发这个方法:
- (void)controlTextDidChange:(NSNotification *)aNotification {
[self myCustomFunction];
}
如果文本以编程方式更改:
[theField setStringValue:@"text"];
[self myCustomFunction];
现在您可以在同一个方法中处理这两种情况。
关于objective-c - 未在 setStringValue 上调用 ControlTextDidChange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12611062/