views:

131

answers:

1

I have a NSTreeController which array is bound to a "items" (custom) property of an NSArrayController subclass. As the tree controller is not bound to the selection of the NSArrayController I need to make sure to let the tree controller know that items has to be fetched after the selection of the array controller changes.

I have done the following in a subclass of NSArrayController:

+ (NSSet *)keyPathsForValuesAffectingItems
{
    return [NSSet setWithObjects:@"selectedObjects",nil];
}

Which should be sufficient AFAIK. The class method is called but does not seems to have any effect.

If I implement an observer for selectedObjects it works fine:

- (void)awakeFromNib;
{
    [self addObserver:self forKeyPath:@"selectedObjects" options:0 context:nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(NSObjectController *)context;
{
    if ([keyPath isEqual:@"selectedObjects"]) {
        [self willChangeValueForKey:@"items"];
        [self didChangeValueForKey:@"items"];
    }

    [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}

Any clue?

A: 

First, "items" is not an observable property of NSArrayController. Do you mean it's a property of the class for which your array controller manages a collection? Ie, it manages an array of Foo and Foo has a property "items"?

In any case, you're making this harder than it needs to be. Why not just go ahead and bind the tree controller's contents to the array controller's selection.items path? There are few situations where this isn't possible.

Joshua Nozzi
items is a custom property of my NSArrayController subclass. It is also a property of the class the NSArrayController manages. I cannot bind directly as I need to do some additional logic that can not be handled by the class that the array controller manages.
Diederik Hoogenboom
I'd recommend posting the relevant parts of your custom code. Too much guessing is needed since details are missing.
Joshua Nozzi