On May 30, 2009, at 2:18 PM, Kelvin Chung wrote:

I seem to have a very simple question, but the answer is eluding me for some odd reason, but here goes:

Suppose I have two NSArrayControllers, ac1 and ac2. Suppose now that I have a third controller ac3, whose content array is dependent on both ac1.selection and ac2.selection (or some keypath therein). Since I can't bind ac3 to both ac1 and ac2 at the same time, I have a class which has a dependent key. In particular, suppose I have the following:

@interface Foo {
  IBOutlet NSArrayController* ac1;
  IBOutlet NSArrayController* ac2;
}
/* ... */
@end

@implementation Foo
- (id) ac1Selection { return [[ac1 selection] valueForKey:@"bar"]; }
- (id) ac2Selection { return [[ac2 selection] valueForKey:@"baz"]; }

+ (NSSet*) keyPathsForValuesAffectingAc3ContentArray {
  return [NSSet setWithObjects:@"ac1Selection", @"ac2Selection", nil];
}

The +keyPathsForValuesAffectingKey is, at least in theory, correct, but it won't work. You will never receive KVO notifications when the selection changes. Instead, why not just observe "selectedObjects" of each array controller, and in - observeValueForKeyPath:ofObject:change:context: send willchange/ didchange for the "ac3ContentArray" key?

- (NSArray*) ac3ContentArray {
  /* Do stuff using -ac1Selection and -ac2Selection */
}
@end

Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"

_______________________________________________

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

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

Reply via email to