views:

32

answers:

1

I have a textfield that I would like to use for multiple types of input. I have two custom NSFormatter classes that I would like to use for that field depending on what kind of input the user would like to provide. The input type is selected by a popup button before data is entered in the text field. I have an IBAction for the popup button and in it's method I do the following:

[myTextField setFormatter:formatter];

This doesn't enable the formatter as expected. The only time the formatter works is when I set it in my awakeFromNib function. Is there a method I need to call to refresh the text field after setting the formatter to make it work?

I tried updating the display via:

[myView setNeedsDisplay:YES] 

but that doesn't produce the desired result as well.

A: 

This hack will cause the text field to update its displayed value using the new formatter. Obviously, it only works if you are using number formatters.

[textField setObjectValue:[NSNumber numberWithFloat:[textField floatValue]]];
J. Hoover
I have custom formatters that change the appearance of strings, i.e. URLs, dates, etc. How would you change the formatter in that case?
David K
You could try iterating your view hierarchy, making each text field (with a delegate, to differentiate from labels) the first responder to cause the view to refresh, the return first responder to whatever needs it. An even worse hack than the previous one!
J. Hoover
I ended up using different NSTextFields for the different formatters, but I will give that hack a try, thanks!
David K