键值观察和NSButton状态 [英] Key Value Observing and NSButton state
问题描述
我正在尝试观察复选框状态,并在复选框状态更改时在应用程序中进行适当的更改.在使用复选框管理窗口的窗口管理器中,我具有以下观察者设置:
I'm trying to observe checkbox status and make appropriate changes in the app when checkbox status changes. In a window manager that manages the window with checkbox I have following observer setup:
- (void)awakeFromNib
{
[myCheckBox addObserver:self
forKeyPath:@"state"
options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld)
context:NULL];
}
- (void)dealloc
{
[myCheckBox removeObserver:self forKeyPath:@"state"];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"KeyPath: %@", keyPath);
NSLog(@"ofObject: %@", object);
NSLog(@"change: %@", change);
}
我还已经将myCheckBox连接到文件所有者(这是窗口控制器)并连接到窗口中的相应复选框.但是,当我运行我的应用程序时,永远不会调用observeValueForKeyPath:ofObject:change:context:
方法.
I also have wired up myCheckBox to file owner (which is window controller) to appropriate checkbox in the window. However when I run my app observeValueForKeyPath:ofObject:change:context:
method is never called.
我在做什么错了?
推荐答案
在-awakeFromNib
中,检查myCheckbox
不是零.如果为零,则说明它在IB中未正确连接.
In -awakeFromNib
check that myCheckbox
is not nil. If it's nil then it's not connected properly in IB.
正确的密钥路径是"cell.state".
The correct keypath is "cell.state".
这篇关于键值观察和NSButton状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!