Skip navigation.
 
mlRe: NSNumberFormatter Changes Reflected In NSTextField
FROM : Simon Wolf
DATE : Mon May 12 18:19:55 2008

On 9 May 2008, at 16:59, Simon Wolf wrote:

> I have an NSTextField with an associated NSNumberFormatter which I 
> am using to format the values shown to a specific number of decimal 
> places. I have a second NSTextField which I am using to display and 
> edit the number of decimal places that I want shown and I have used 
> the controlTextDidEndEditing delegate method to detect a change. 
> Upon a change I am using the setMinimumFractionDigits and 
> setMaximumFractionDigits methods of the NSNumberFormatter to change 
> the number of decimal places to be shown but I am having trouble 
> getting the NSTextField to refresh without giving the control the 
> focus.
>
> If I change the value of the field then the value is displayed 
> correctly:
>
> float temp;
> temp = [netAmountField floatValue];
> temp++;
> [netAmountField setIntValue:(int)temp];
>
> If I don't actually change the value then the NSTextField is not 
> refreshed:
>
> [netAmountField setFloatValue:[netAmountField floatValue]];
>
> I'm fairly sure that this is simply due to the NSTextField being 
> clever enough to realise that the value has not changed and 
> therefore it doesn't re-display the value and trigger the new format 
> and I am equally sure that I'm missing a simple way to achieve the 
> refresh but after an hour of messing around I though I'd ask wiser 
> folk.



I realise that it may be bad form to reply to my own message but I 
wanted a solution to be stored in the archives for anyone else who is 
in the early stages of Cocoa and hits the same sort of problem.

As an aside, I removed the Number formatters from my NIB file and 
replaced them with NSNumberFormatters. This was so that I could use 
the OS X v10.4 version rather than the buggy 10.0 one. I'm sure it 
would make no difference in this instance but I think it's better to 
adopt best practices early.

However, the actual solution is to re-set the string value of the 
NSTextFields:

[netAmountField setStringValue:[NSString stringWithFormat:@"%f",
[netAmountField floatValue]]];

I'm still not 100% sure why this works when setting the value of the 
NSTextField to a float doesn't but, hey, at least it works.

Simon

Related mailsAuthorDate
mlNSNumberFormatter Changes Reflected In NSTextField Simon Wolf May 9, 17:59
mlRe: NSNumberFormatter Changes Reflected In NSTextField Simon Wolf May 12, 18:19