Key Status and NSButton Status

I try to respect the status of the flag and make appropriate changes to the application when the status of the flag changes. In the window manager, which controls the window using a checkbox, I have the following observer setting:

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

I also connected myCheckBox to the owner of the file (which is the window controller) to the corresponding checkbox in the window. However, when starting my application, observeValueForKeyPath:ofObject:change:context: method is never called.

What am I doing wrong?

+3
source share
2 answers

In -awakeFromNib make sure myCheckbox not null. If it is zero, then it is incorrectly connected to IB.

Edit:

The correct key path is "cell.state".

+7
source

If it is not indicated that it complies with a key value, you should not expect accessors of this class to implement KVO support.

Buttons implement key value binding, so instead of observing the state property, you can bind one of your logical attributes to the button value binding.

+2
source

All Articles