One of my viewcontrollers uses the representedObject to bind to the 
NSArrayController that holds all the data for the application (OS X, ARC on). I 
declared a local property mySelection (an NSArray) and bind it to the 
representedObject as follows:

    [self bind:@"mySelection" toObject:self.representedObject 
withKeyPath:@"selectedObjects" options:nil];

So far so good.

Now when the user changes mySelection,  not only my local property needs to be 
updated, but also some other parts in my code. So I cannot just rely on the 
automatically generated setter, and thus need to monitor a change in 
mySelection.  After some searching I came up with the following:

    [self.representedObject addObserver:self forKeyPath:@"selectedObjects"  
options:NSKeyValueObservingOptionNew context: nil];
 
and then:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object 
change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString: @"selectedObjects"])
    {
       // do additional stuff
    }
    else 
    {
        [super observeValueForKeyPath:keyPath ofObject:object change:change 
context:context];
    }
}

Again, this all works. Whenever the user changes the selection, 
observeValueForKeyPath: gets called and the "do additional stuff" gets executed.

But I have the feeling I am over-complicating things.  From reading on this 
subject I get the impression the approach above seems to be more for monitoring 
properties in other classes, not in the owner class.

So, is this the correct way to do this (responding to a change in a local 
property, or am I overlooking something very obvious?

Thanks,

- Koen.
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to