我實現了一個委託及其controlTextDidChange
方法,只要我在NSTextField
中輸入文本,就會調用它。ControlTextDidChange未在setStringValue上調用
當我以編程方式通過[field setStringValue:@"newText"]
設置文本時,不會調用controlTextDidChange
。
你能解釋一下嗎?以及如何以編程方式設置一個觸發調用我的代理方法controlTextDidChange
的新值。
我實現了一個委託及其controlTextDidChange
方法,只要我在NSTextField
中輸入文本,就會調用它。ControlTextDidChange未在setStringValue上調用
當我以編程方式通過[field setStringValue:@"newText"]
設置文本時,不會調用controlTextDidChange
。
你能解釋一下嗎?以及如何以編程方式設置一個觸發調用我的代理方法controlTextDidChange
的新值。
controlTextDidChange
代表僅在用戶交互時觸發,
更新NSControl
手動通過設計不觸發代表。
恰當解
的解決方案是創建一個單獨的方法來處理文本變化:
- (void)myCustomFunction {
// Triggered in case the text is changed,
// no matter by user or programmatically
}
觸發該方法從委託:
- (void)controlTextDidChange:(NSNotification *)aNotification {
[self myCustomFunction];
}
而如果文本以編程方式更改:
[theField setStringValue:@"text"];
[self myCustomFunction];
現在,您可以在同一方法內處理兩種情況。
可以觸發功能controlTextDidChange
編程
[[NSNotificationCenter defaultCenter] postNotificationName: NSControlTextDidChangeNotification
object:textFieldExample
userInfo:[NSDictionary dictionaryWithObject: [[textFieldExample window] fieldEditor:YES
forObject:textFieldExample]
forKey:@"NSFieldEditor"]];
這是更有用,當controlTextDidChange
是由多於一個文本框觸發!
它是一個解決方案,但將work-thx – maseth