在setStringValue上没有调用ControlTextDidChange

mas*_*eth 3 xcode cocoa objective-c

我已经实现了一个委托及其controlTextDidChange方法,只要我在中输入文本就会调用它NSTextField.

当我以编程方式设置文本时[field setStringValue:@"newText"],controlTextDidChange不会被调用.

你能解释一下吗?我应该如何以编程方式设置一个新的值来触发我的委托方法的调用controlTextDidChange.

Ann*_*nne 7

controlTextDidChange委托时,才会触发用户交互,
更新NSControl手动并通过设计不会触发委托.

适当的解决方案

解决方案是创建一个单独的方法来处理文本更改:

- (void)myCustomFunction {
     // Triggered in case the text is changed,
     // no matter by user or programmatically
}
Run Code Online (Sandbox Code Playgroud)

从委托中触发此方法:

- (void)controlTextDidChange:(NSNotification *)aNotification {
    [self myCustomFunction];
}
Run Code Online (Sandbox Code Playgroud)

如果文本以编程方式更改:

[theField setStringValue:@"text"];
[self myCustomFunction];
Run Code Online (Sandbox Code Playgroud)

现在,您可以在同一方法中处理这两种情况.