NSObjectController selection - now you see it, now you don't
NSObjectController selection - now you see it, now you don't
- Subject: NSObjectController selection - now you see it, now you don't
- From: Luke Evans <email@hidden>
- Date: Sat, 7 Mar 2009 22:05:24 -0800
I'm a little bamboozled by a discovery I've just presently made.
I was intending using an NSObjectController in one of my classes to
keep a track of which visible object is selected. There are reasons
why I've chosen to have an object controller rather than just a
"selectedObject" ivar, which aren't important here.
The docs on NSObjectController say this:
selectedObjects
Returns an array of all objects to be affected by editing.
- (NSArray *)selectedObjects
Return Value
An array of all objects to be affected by editing. If the receiver
supports a selection mechanism, the array contains key value coding
compliant proxies of the selected objects; otherwise proxies for all
content objects are returned. If the receiver is a concrete instance
of NSObjectController, returns an array containing the receiver’s
content object.
...
The last sentence quoted was just what I was hoping to get from this
instance of NSObjectController, and I wrote the code to -setContent:
of my selected object into this NSObjectController, in anticipation of
being able to look this up later with a -selectedObjects.
Well, what actually happens is that -selectedObjects almost always
returns an empty array. Sending -setContent: does not appear to
result in this content being available as the selectedObject in the
way described (or there's other magic to get this to happen?).
In order to declutter and focus on what was going on, I created a
simple xcode project with one class called "AppController" (made the
application delegate), and the following code:
- (id) init {
self = [super init];
if (self != nil) {
objController = [NSObjectController new];
}
return self;
}
- (void)awakeFromNib {
[objController setContent:@"This is a nice string"];
NSArray *selectedObjects = [objController selectedObjects];
id selectedObject = (selectedObjects && [selectedObjects count])?
[selectedObjects objectAtIndex:0] : nil;
NSLog(@"The selectedObjects were: %@,\n the selectedObject was: %@",
selectedObjects, selectedObject);
[objController setContent:@"Another string"];
selectedObjects = [objController selectedObjects];
selectedObject = (selectedObjects && [selectedObjects count])?
[selectedObjects objectAtIndex:0] : nil;
NSLog(@"The selectedObjects were: %@,\n the selectedObject was: %@",
selectedObjects, selectedObject);
}
What happens when this is run is that the first time -setContent: is
used, the -selectedObjects method returns the object (the string).
The second time however, has -selectedObjects return NO object (i.e.
the empty array).
This looks like a bug, or I've misinterpreted the documentation, or I
need to poke the NSObjectController in some way to accept the object
provided in the second call to -setContent: as the selected object
(... but how and why?!).
_______________________________________________
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