Binding of "Object" and "Value" in NSPopupButtonCell
Binding of "Object" and "Value" in NSPopupButtonCell
- Subject: Binding of "Object" and "Value" in NSPopupButtonCell
- From: Jerry Krinock <email@hidden>
- Date: Fri, 2 Jan 2009 15:47:07 -0800
I'm working on a Core Data document-based app. Think of
DepartmentsAndEmployees, where each document models one department,
containing employees. I want to add a "rank" attribute to the
Employee class, an enumerated NSNumber representing something like this:
NSNumber Description
-------- -----------
100 Bean Counter
200 Manager
300 Worker
400 Grunt
Of course, in the table view of this model, I want the words and not
the represented numbers to appear in a popup...
Name Rank (NSPopupButtonCell)
------------- -------
Tom Worker ↕
Suzie Manager ↕
Dick Grunt ↕
To further complicate things, the gods restrict the allowed ranks for
employees they don't like. To model this, the Employees class has
defined a selector -allowedRanks which returns an array of the allowed
ranks (NSNumbers) that the gods allow for the employee instance.
By trial and error, I have gotten this to work completely as desired
with the following three bindings on the Table Column:
Bind "Content" to the array controller providing the table rows, with
Controller Key set to selectedObject [2] and Model Key Path set to
allowedRanks, and Value Transformer set to
TransformArrayOfRankNumbersToStrings, a nonreversible value
transformer which transforms an array of numbers to an array of the
represented strings.
Bind "Content Objects" to the same array controller, same Controller
Key and same Model Key Path, but no Value Transformer.
Bind "Selected Object" to the same array controller, Controller Key to
arrangedObjects, Model Key Path set to 'rank', and Value Transformer
set to TransformRankNumberToString, which is a simplified (scalar) but
reversible version of the above-described
TransformArrayOfRankNumbersToStrings.
But this seems like a real kludge, serially transforming things back
and forth between the "represented" object and the "displayed"
object. The reverse transformer in particular has a stupid-looking
chain of "else if ([isEqualToString:...])" branches, and it will be
slightly more complicated when I wish to display localized strings.
The Cocoa Bindings Reference [1] seems to imply that there are two
variables available in the menu items:
"Object": the represented object, in my case the NSNumber
with value 100, 200, 300 or 400.
"Value" : the string displayed to the user, in my case
"Manager", "Grunt", etc.
It seems to me that the intent of these two separate variables is to
allow the "represented" and "displayed" data to travel in a parallel
structure, avoiding the kind of kludge I've resorted to. Since this
message is already too long, I will end by saying that I've tried
several of the 65,535 other ways that there are to configure all those
popups, checkboxes and other fields in Interface Builder's Table
Column binding inspector, but so far my kludge is the only way that
works.
Ain't there a better way?
Jerry
[1] http://developer.apple.com/documentation/Cocoa/Reference/CocoaBindingsRef/BindingsText/NSPopUpButtonCell.html#/
/apple_ref/doc/uid/NSPopUpButtonCell-DontLinkElementID_801
[2] I have defined a category for NSArrayController which defines -
selectedObject to return the first element in -selectedObjects, or
else 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