Re: Can a value transformer get its own registration name?
Re: Can a value transformer get its own registration name?
- Subject: Re: Can a value transformer get its own registration name?
- From: Bill Cheeseman <email@hidden>
- Date: Thu, 12 Apr 2007 13:33:57 -0400
- Thread-topic: Can a value transformer get its own registration name?
on 2007-04-12 11:46 AM, Jim Correia at email@hidden wrote:
> I'm still not understanding how a transformer knowing which name(s)
> it was registered under helps solve the problem posed.
In my one "generalized" transformer subclass, I override the
-transformedValue: method by using chained if-else statements based on the
several names under which multiple instances of this one "generalized"
transformer subclass are registered by my app's +initialize method.
Using the somewhat unrealistic "multiply" example from the documentation, if
the registered name is "MultiplyByTwoTransformer" then my -transformedValue:
override method multiplies by 2, else if the registered name is
"MultiplyByTenTransformer" then my override method multiplies by 10, and so
on. The benefit to using the registered name is that I need only a single
"generalized" transformer subclass, which I can instantiate and register
several times using names that are meaningful to me in the context of my
application. I then use the appropriate registered name in the value
transformer field of several different view bindings in IB. This is what I
understand the documentation I quoted originally to be driving at, and it
really works quite well.
Of course I could do the same thing with multiple transformer subclasses or,
as you pointed out, with a single subclass with a registered name iVar to
use in the if-else chain. To my mind, the "generalized" subclass approach
based on registered names and pointer comparisons is simpler to implement,
both because I only need to write one subclass and I don't need to mess with
iVars, -initWith... methods and all that. I just use the registration naming
facility that NSValueTransformer already provides.
I'm not really multiplying, but it's a similar issue. My app has a sheet
where I configure a certain object. The sheet has half a dozen radio groups,
each with a different number of radio buttons. In each group, the last radio
button has special significance, because selecting the last button causes
certain subsidiary text fields or other controls to become enabled. It's
easy to do this with the several controls' enabled binding, but I need a
value transformer to identify whether the last radio button in the relevant
radio group is selected -- the last radio button might be the button with
index or tag no. 3, or index or tag no. 7, or whatever, depending on which
radio group I'm examining. So I implement a single "generalized" value
transformer called XXLastRadioButtonIsSelected, then I instantiate and
register it several times under names like
@"XXLastButtonInSecondGroupIsSelected" and
@"XXLastbuttonInSeventhGroupIsSelected" (actually, I use terminology that is
descriptive within my application's functionality). The if-else chain in my
one "generalized" transformer subclass's -transformedValue: override method
returns a Boolean NSNumber object encoding YES if the selectedIndex binding
of the corresponding radio group is 3 or 7 or whatever, depending on which
registered name I used in the value transformer field of the enabled binding
for a given control. I know this sounds complicated when I explain it, but
it's simple to implement and requires less code than any of the other
techniques that have been mentioned. I would show the code here -- it's just
a few lines -- except I'm at work and I leave my programming at home.
--
Bill Cheeseman
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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