2

This might seem straightforward, but the following code does not work, because the "observeValueForKeyPath" function is never called, although I keep changing the text in the 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");
}

The log message @"observeValueForKeyPath" is never printed. I tried observing the key @"stringValue", but this does not work neither...

Am I missing something ??

dewphy
  • 21
  • 3

2 Answers2

1
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(textFieldDIdChange:) 
                                             name:NSControlTextDidChangeNotification 
                                           object:self.textField];
mityaika07
  • 652
  • 5
  • 14
-1
[[NSNotificationCenter defaultCenter] addObserverForName:NSTextViewDidChangeSelectionNotification
            object:self.textView 
            queue:[NSOperationQueue mainQueue] 
            usingBlock:^(NSNotification *note){
                NSLog(@"Text: %@", self.textView.textStorage.string);
            }];
Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
Thawe
  • 171
  • 6
  • This code is for an `NSTextView`, not an `NSTextField`. [This answer](http://stackoverflow.com/a/11488611/39974) suggests setting a delegate that implements the `NSTextFieldDelegate` protocol and implementing the `controlTextDidChange:` delegate method. – Ivan Vučica Dec 31 '12 at 14:05