Re: value transformer for structs?
Re: value transformer for structs?
- Subject: Re: value transformer for structs?
- From: "John C. Randolph" <email@hidden>
- Date: Thu, 23 Mar 2006 08:23:13 -0800
On Mar 23, 2006, at 5:45 AM, Michael B Johnson wrote:
On Mar 22, 2006, at 11:19 PM, John C. Randolph wrote:
On Mar 22, 2006, at 9:07 PM, Michael B Johnson wrote:
I'm assuming I'm missing something obvious, but let's say I have
an object that returns a struct (like an NSRect, or better yet, a
QTTime) that I want to use bindings to show in the UI.
Looking at the doc it seems (understandably) expecting objects.
Can I do this, or should I have my class return that thing as an
NSString or somesuch?
Value transformers do have to take and return objects. For an
NSRect, I've used NSRectFromString() and NSStringFromRect() to go
back and forth with string representation. There are equivalent
also point, range, and size functions. As it happens, there's
also a QQTimeFromString() function.
Okay, but my question is how do I wire it up? I realize I'm
betraying my minimal knowledge of bindings here, but let's say I
implement it like this:
@implementation MYQTTimeRangeValueTransformer
+ (Class)transformedValueClass { return [NSString self]; }
+ (BOOL)allowsReverseTransformation { return NO; }
- (id)transformedValue:(id)value {
QTTimeRange* r = (QTTimeRange*)value;
return (value == nil) ? nil : QTStringFromTimeRange(*r);
}
@end
and then register it in my app delegate like this:
MYQTTimeRangeValueTransformer* timeRangeXformer;
timeRangeXformer = [[[MYQTTimeRangeValueTransformer alloc] init]
autorelease];
[NSValueTransformer setValueTransformer:timeRangeXformer
forName:@"MYQTTimeRangeValueTransformer"];
I then have a NSTextEntry that I bind to a keypath
element.timeRange that returns a QTTimeRange and put the value
transformer MYQTTimeRangeValueTransformer on it.
So my value transformer gets called (always with a nil value), but
the accessor for timeRange never gets called.
Just as a sanity check I made another accessor called
timeRangeString that returned QTStringFromTimeRange([self
timeRange]) and bound that to another entry, and that gets called
fine.
I'm obviously missing something...
And I can mention the obvious - bindings are currently very
difficult to debug when they're not working right... Are there some
obvious breakpoints that everyone knows about to follow the action
as your nib is getting wired up?
Thanks for any insight.
From your description, it sounds like you're doing everything you
should. Other than double-checking for typos in the keypath or the
name of the value transformer in IB, I don't really know what else to
suggest.
-jcr
John C. Randolph <email@hidden> (408) 914-0013
Roaming Cocoa Engineer,
Available for your projects at great Expense and Inconvenience.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden