Re: self registering NSValueTransformer and bindings
Re: self registering NSValueTransformer and bindings
- Subject: Re: self registering NSValueTransformer and bindings
- From: "I. Savant" <email@hidden>
- Date: Wed, 7 May 2008 13:32:36 -0400
> I have a self registering NSValueTransformer that requires a binding.
Did you mean it's required for use in a binding?
> @interface MyValueTransformer : NSValueTransformer {
> IBOutlet NSTabView *tabView;
> }
That's probably not going to work out ... (see below)
> // from http://www.gigliwood.com/weblog/
> + (void)load
> {
> NSLog(@"registered %@", NSStringFromClass([self class]));
>
> NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
> NSValueTransformer *theTransformer = [[[self alloc] init] autorelease];
> [NSValueTransformer setValueTransformer:theTransformer
> forName:NSStringFromClass([self class])];
> [pool release];
> }
See:
http://developer.apple.com/documentation/Cocoa/Conceptual/ValueTransformers/Concepts/Registration.html
Here you're instantiating an instance of the generic
NSValueTransformer class and letting the NSValueTransformer class know
that it should use that generic transformer any time
"MyValueTransformer" is requested. When you then establish a binding
asking for "MyValueTransformer" to do its thing, you're not getting
your MyValueTransformer, but a generic NSValueTransformer that (per
the documentation) only returns the same value that's passed into it.
This incidentally has to do with your outlet problem ... more on that
in a second.
> I've instantiate it through the NIB. Which works fine! I do see the NSLog
> lines and all.
I'm not sure how that's happening, given the above. I would expect
your transform method not to be called at all, given the above. I
could be wrong, then again it's not working right for you, so it's
worth checking out. ;-)
> Unfortunately the IBOutlet stays nil ...but in the bindings inspector I can
> see the proper binding on the transformer.
I don't know what you mean about 'the proper binding', but no, I
wouldn't expect the outlet to be anything but nil. The *instance* you
*instantiated* in your nib is not the same one you're passing when
registering the transformer for the name "MyValueTransformer". In
*that* instance, the outlet is nil.
Further, since +load is called on all objects in the nib's object
graph *prior to* connections being reestablished (such as outlets and
actions), +load is not a good place to assign the outlet because the
tab view you are referencing may not exist yet and its named outlet is
definitely not connected yet.
Perhaps in some -awakeFromNib method you could ask
NSValueTransformer for the transformer for the name
"MyValueTransformer" and set its outlet then (you'd need a 'setter'
accessor method) ... but what you're doing is strange.
A value transformer really shouldn't know anything about a tab view.
Its sole job is to transform one value to another. A number to a
string, or a class name to an image, etc. and possibly vice-versa
(reverse transformations). Why does yours care about a tab view?
Perhaps all you want is a few constants that's included into your
transformer via a header include ...
If this has anything to do with your other posts (I suspect it
does), you're trying to bind the selected tab of a tab view to some
attribute of a selected object (like "accountType"). In this case,
you'd want your "Account" object to use some handy constants for its
-accountType attribute ... constants that are defined in your
Account's header. That header can be included in your
AccountTypeToSelectedTabTransformer ... where it gives the correct tab
index depending on whether the passed value is kNormalAccountType or
kSuperHappyFunLuckyMegaAccountType or some other ridiculous example.
Keeps everything simple, straight-forward, and easy to read months
later when you decide to modify it.
--
I.S.
_______________________________________________
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