您需要注册一个观察者来监听NSNotification
.
// When the NSWindow is displayed, register the observer.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(controlTextDidChange:) name:NSControlTextDidChangeNotification object:nil];
- (void)controlTextDidChange:(NSNotification *)obj {
// You can get the NSTextField, which is calling the method, through the userInfo dictionary.
NSTextField *textField = [[obj userInfo] valueForKey:@"NSFieldEditor"];
}
看起来,返回的对象NSFieldEditor
是 a ,而不是您可能期望NSTextView
的同一个对象。NSTextField
但是,根据 Apple 的文档,如果您实现此方法并且控件委托已注册到此对象,则应自动注册通知。
控件发布一个 NSControlTextDidChangeNotification 通知,如果控件的委托实现了这个方法,就会自动注册接收通知
来源:https ://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSControl_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject/controlTextDidChange :