Re: Force redisplay of NSTextField after NSFormatter subclass change
Re: Force redisplay of NSTextField after NSFormatter subclass change
- Subject: Re: Force redisplay of NSTextField after NSFormatter subclass change
- From: Matt Neuburg <email@hidden>
- Date: Tue, 27 Nov 2007 10:10:49 -0800
- Thread-topic: Force redisplay of NSTextField after NSFormatter subclass change
On Tue, 27 Nov 2007 16:27:23 +0000, Jonathan Fewtrell
<email@hidden> said:
>I have an NSTextField bound to a certain key in a Core Data model
>entity. A switch in the interface allows the user to display the
>value in either of two formats. The formats are quite different, so I
>have two different NSFormatter subclasses and I set the appropriate
>one when the user toggles the switch. I find that the displayed value
>immediately after a switch is spurious, So I want to force the field
>to redisplay. Nothing I try seems to get it to redisplay. I have
>tried the following:
>
>sending -setNeedsDisplay: to the field
>sending -display to the field
>setting the field hidden and then unhidden
>sending a -willChangeValueForKey: / -didChangeValueForKey; pair to
>the model object
>
>It appears that because the underlying value of the key (which is an
>NSNumber) does not change, the field refuses to update. The only way
>I can get it to work is to change the value of the key and change it
>back again. But this seems a clumsy hack. Is there a better way?
This answer has nothing to do with Core Data - I experience the same
difficulty *any* time I change NSFormatters in the middle of the app's
lifetime. My solution is: when I change the formatter, I also manually pass
the current value being used as the source of the textfield's text thru the
formatter:
NSString* s = [self valueForKey:@"filenameSearch"];
[myTextField setFormatter: (newMode ? f : nil)];
if (newMode) {
id val;
if ([f getObjectValue:&val forString:s errorDescription:nil])
[self setValue:val forKey:@"filenameSearch"];
My ivar "filenameSearch" is bound from the textfield, so capturing that
value before the formatter change and then passing it thru the formatter
after the change and resetting it in a KVO-compliant way causes the right
thing to appear in the field.
This sounds a lot like what you're already doing, unfortunately... :)
m.
--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
One of the 2007 MacTech Top 25: <http://tinyurl.com/2rh4pf>
AppleScript: the Definitive Guide - Second Edition!
<http://www.amazon.com/gp/product/0596102119>
_______________________________________________
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