1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

addObserver: forKeyPath:options: question

Discussion in 'Mac Programming' started by mdeh, Jul 2, 2009.

  1. macrumors 6502

    #1
    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)
    Code:
    [ self addObserver:(NSObject *) self forKeyPath:(NSString *) @"fido" 
    options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) 
    context: @"Starting KVO"];
    
    And is implemented thus;
    Code:
    - (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
    However, if I try and implement this in the above methods, for example change the line of code from
    Code:
    self addObserver:(NSObject *) self
    to
    Code:
    self addObserver:(NSObject *) textField 
    **where textField is an outlet of AppController, suitable linked in IB to the second textField**

    I get an error thus
    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.
     

Share This Page