开发者

How to observe the value of an NSTextField

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 ??


[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(textFieldDIdChange:) 
                                             name:NSControlTextDidChangeNotification 
                                           object:self.textField];


[[NSNotificationCenter defaultCenter] addObserverForName:NSTextViewDidChangeSelectionNotification
            object:self.textView 
            queue:[NSOperationQueue mainQueue] 
            usingBlock:^(NSNotification *note){
                NSLog(@"Text: %@", self.textView.textStorage.string);
            }];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜