Why doesn't -[NSArrayController selection] et al fire keyPathsForValuesAffectingKey?
Why doesn't -[NSArrayController selection] et al fire keyPathsForValuesAffectingKey?
- Subject: Why doesn't -[NSArrayController selection] et al fire keyPathsForValuesAffectingKey?
- From: Jerry Krinock <email@hidden>
- Date: Fri, 2 Apr 2010 20:25:57 -0700
I have a table bound to an array controller containing Foo objects and wanted to enable a "Perform Foo" button whenever there is a non-nil selection. Not trusting those pesky proxy objects you get from an array controller's -selection, I wrote a -selectedFoo method which will return either the first selected Foo or nil if there is no selection, and bound my button's 'enabled' binding to it via NSIsNotNil value transformer.
Now, NSArrayController has five methods which give the selection in some form, and they are all documented to be "observable using key-value observing". So to make this work I implemented a +keyPathsForValuesAffectingSelectedFoo which returned the path to one of these keys, but when that didn't work I put in all five.
But I never could get it to work. By logging, I determined the problem: None of these five observers are firing the -selectedFoo getter when the array and table controller's selection changes. When I added, as a test, some other object's key path to keyPathsForValuesAffectingKey, and changed its value, the -selectedAgent method was invoked and updated the enabled state of my button as desired. So the problem seems to be that "observeable using key-value observing" doesn't mean what I think it means. Where am I going wrong?
I solved the problem by eliminating all this code and binding my button's 'enabled' binding instead to the array controller's 'selection' and, to my surprise, it worked. My surprise is because, according to superclass NSObjectController documentation, 'selection' returns NSNoSelectionMarker when there is no selection, not nil which is what my NSIsNotNil value transformer would expect.
So, what should work does not work and what should not work does work.
Thanks,
Jerry Krinock
// fooArrayController is an IBOutlet
+ (NSSet*)keyPathsForValuesAffectingSelectedFoo {
return [NSSet setWithObjects:
// Shotgun approach. Try 'em all!!
@"fooArrayController.selectedObjects",
@"fooArrayController.selectionIndex",
@"fooArrayController.selection",
@"fooArrayController.selectionIndexes",
@"fooArrayController.selectedObjects",
nil] ;
}
- (Foo*)selectedFoo {
NSArray* selectedFoos = [fooArrayController selectedObjects] ;
if ([selectedFoos count] > 0) {
return [selectedFoos objectAtIndex:0] ;
}
return nil ;
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden