PDA

View Full Version : addObserver: forKeyPath:options: question




mdeh
Jul 2, 2009, 04:49 PM
Hi all,
I'm stuck :confused:

I have implemented a very basic app. It is based on Chap 7 of Hillegass.
A slider, two textFields, and a couple of buttons. Bascially, the the slider's value, the textField's value are bound to a number(fido) in a userDefined Controller(AppController). This works as expected...the slider causes the textField to update.
The first button initiates KVO in the second textField.

Now, **this** works. ( this is the method in the startKVOButton)

[ self addObserver:(NSObject *) self forKeyPath:(NSString *) @"fido"
options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
context: @"Starting KVO"];

And is implemented thus;

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"keyPath:%@\n Object: %@\n change: %@:", keyPath, object, change);
if ( [ keyPath isEqual:@"fido"])
[textField setObjectValue: [change objectForKey: NSKeyValueChangeNewKey]];
/* NSLog(@"%@", context); */

}


Here is my question. Hillegass seems to imply that one can take an object eg a text field and specify that the text field **alone** register itself as an observer

How did the text field become an observer of the "fido" key.....the text field added itself as an observer."

However, if I try and implement this in the above methods, for example change the line of code from
self addObserver:(NSObject *) self
to

self addObserver:(NSObject *) textField **where textField is an outlet of AppController, suitable linked in IB to the second textField**

I get an error thus
<NSTextField: 0x128ea0>: An -observeValueForKeyPath: ofObject:change:context: message was received but not handled.An -observeValueForKeyPath: ofObject:change:context: message was received but not handled.

Which leads me to ask this.

Is a control in of itself not an object? Or am I trying to do something that is simply not possible?

Thanks in advance.