Re: model key path and properties
Re: model key path and properties
- Subject: Re: model key path and properties
- From: Quincey Morris <email@hidden>
- Date: Thu, 14 Jul 2011 17:20:58 -0700
On Jul 14, 2011, at 15:29, Torsten Curdt wrote:
> I have a NSArrayController and I add items to that controller. In the
> nib I have bound a NSPopUpButton's "Content" to that
> NSArrayController's arrangedObjects. Now with an empty model key path
> I see the object's description in the popup. All good and expected.
> Now these items the NSArrayController manages have a "name" property.
> Accessing the name like this works just fine:
>
> NSLog(@"name: %@", [[[self.myArrayController arrangedObjects]
> objectAtIndex:0] name]);
>
> Now I would think I just need to change the model key path of the
> "Content" binding to be "name" but IB gives me the exclamation mark
> and says "no completion found".
The popup button's Content binding is an *entire* array. Arrays don't have a "name" property. IOW, "arrangedObjects.name" is not a valid key path.
Now, it's certainly true that NSArray responds to the KVC method 'valueForKey:@"name"', and it should produce an array of names, but since NSArrayController is something of a black box, you don't really know that this mechanism works via a binding to an array controller. You can try binding it anyway, and I guess if it works it works. We had a long thread about something similar a few months ago, and in that case it didn't work. The binding of a popup button does *not* work like the binding of a table column to its value, and that's probably the conceptual model that led you to try this. (I can't remember if this scenario is the exact same thing, though.)
The other problem, though, is that even if it works, it's not a KVO-compliant mechanism. The popup menu content isn't going to update properly if the array changes, or if its elements' name properties change.
Assuming that the "un-arranged" array that your array controller's content is bound to is (or is in) your data model, then the best solution is probably to do the whole thing "correctly":
1. In your window controller, observe the data model array. It also needs a mechanism to be told about changes to the "name" properties of the individual array elements, since the array's KVO notifications don't handle such property changes.
2. In your window controller, define a derived array property that is the desired values of the popup menu.
3. Have your window controller maintain this derived array property KVO-compliantly.
4. Bind the popup button directly to the window controller's derived array property.
Unfortunately, there's no free lunch in this case. OTOH, doing it the "correct" way makes it pretty easy to do things like add separators and "Other..." items to the popup menu.
Of course, if you go to all that trouble, it's probably easier just to set the popup button's menu directly. However, you still don't escape the data model KVO . :)
_______________________________________________
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