这可能看起来很简单,但是下面的代码不起作用,因为永远不会调用“observeValueForKeyPath”函数,尽管我不断更改NSTextfield中的文本:
- (void)awakeFromNib
{
[myNSTextField addObserver:self forKeyPath:@"value" options:NSKeyValueObservingOptionOld context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"observeValueForKeyPath");
}
永远不会打印日志消息@“observeValueForKeyPath”。我尝试观察密钥@“stringValue”,但这不起作用......
我错过了什么吗?
答案 0 :(得分:1)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textFieldDIdChange:)
name:NSControlTextDidChangeNotification
object:self.textField];
答案 1 :(得分:-1)
[[NSNotificationCenter defaultCenter] addObserverForName:NSTextViewDidChangeSelectionNotification
object:self.textView
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note){
NSLog(@"Text: %@", self.textView.textStorage.string);
}];