如何观察NSTextField的值 [英] How to observe the value of an NSTextField

查看:216
本文介绍了如何观察NSTextField的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎很简单,但是以下代码不起作用,因为尽管我不断更改NSTextfield中的文本,但从未调用过"observeValueForKeyPath"函数:

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");
}

永远不会打印日志消息@"observeValueForKeyPath".我尝试观察键@"stringValue",但这都不起作用...

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

我错过了什么吗?

推荐答案

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

这篇关于如何观察NSTextField的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆