Re: Binding of "Object" and "Value" in NSPopupButtonCell
Re: Binding of "Object" and "Value" in NSPopupButtonCell
- Subject: Re: Binding of "Object" and "Value" in NSPopupButtonCell
- From: Ken Thomases <email@hidden>
- Date: Tue, 6 Jan 2009 21:19:48 -0600
On Jan 2, 2009, at 5:47 PM, Jerry Krinock wrote:
I want to add a "rank" attribute to the Employee class, an
enumerated NSNumber representing something like this:
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.
I would think you would want "Content" to refer to the allowed ranks
in their "raw" object form, not their string. So, don't use a value
transformer here. You wouldn't bind "Content Objects" because it
would just be the same as "Content". In order to get proper display
strings, you'd bind "Content Values". You could declare a category on
NSNumber to add a read-only "rankNameValue" property, and then bind
"Content Values" to the key path allowedRanks.rankNameValue. Or, you
could bind to allowedRanks and keep your nonreversible value
transformer.
Since the objects of the pop-up are now the raw number objects,
binding "Selected Object" becomes trivial. You just bind it to the
"rank" property with no value transformer.
The docs don't make it crystal clear, but if I recall the release
notes[1] do: the "Content Objects" or "Content Values" bindings must
use a key path which is an extension of the "Content" key path. The
purpose of the contentValues binding is relatively straightforward:
provide a better display value than the "whole" object would through
its -description method. The purpose of the contentObjects binding
has mostly to do with the selection. It's the value that gets sets to
the property bound to the selectedObject binding, and on settting up
the pop-up, it's used to match the initially selected entry to the
value obtained through the selectedObject binding. Since your
selection matches the "whole" object (the NSNumber held by an
Employee's rank property), you don't need a separate contentObjects
binding.
On another topic, you say you're binding "Content" with a Controller
Key of "selectedObject" (which you define through a category as the
first element of the array controller's selectedObjects property).
This doesn't make sense to me. The Controller Key should be
arrangedObjects. Otherwise, all of the rows of the column will show
the rank for whichever row you select.
So the Content binding of the pop-up column might be (as displayed in
IB) EmployeesController.arrangedObjects.allowedRanks. This resolves
to an array of arrays. The outer array has as many elements as there
are employees. The elements within that are themselves arrays, the
allowed ranks, as NSNumber objects, for each employee. Similarly, for
Content Values, binding to
EmployeesController.arrangedObjects.allowedRanks.rankNameValue would
produce an array of arrays -- for each employee, there would be an
array of strings which are the names of the allowed ranks.
Cheers,
Ken
[1] Search for "contentObjects" on this page: http://developer.apple.com/releasenotes/Cocoa/AppKitOlderNotes.html
_______________________________________________
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