user1544073
user1544073

Reputation: 105

How to access in code the NSNumberFormatter properties in a NSTextField

I have created a nib file with a normal IBOulet for a NSTextField. In IB (4.4), add a NSNumberFormatter for this NSTextField and give this some properties, like Decimal Style, max and min values, etc.

Now, I want to modify in code, some other properties for this NSNumberFormatter already embedded in the textfield, like SetGroupingSeparator, SetMaximunFractionDigits etc.

Let say my NSTextField is named pf, normally I can access the value (string) in the cell as:

[pf SetFloatValue]

or

myFloat = [pf floatValue].

That is working without any problems. However, I have been unable to find in the documentation or anywhere an example how to modify the NSNumberFormatter properties in code. I know how to create a NSNumberFormatter in code, but do not know how to assign it to the NSTextField (which I don't know if will overrides the one create in IB). Could anyone help me with a clear example how to do this?.

Upvotes: 1

Views: 351

Answers (2)

Michael Dautermann
Michael Dautermann

Reputation: 89519

To create and set a NSNumberFormatter to a NSTextField in code can be done via:

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[[textField cell] setFormatter:numberFormatter];

The details for which I found in Apple's Data Formatting Guide

Modifying a formatter should be pretty similiar, accessing the formatter via [NSCell formatter]:

NSNumberFormatter * numberFormatter = [[textField cell] formatter];

Upvotes: 1

rdelmar
rdelmar

Reputation: 104082

You do it just like any other object in IB. Create an IBOutlet for the formatter in your .h file and connect it to the formatter in IB, then you can access its properties through the outlet.

Upvotes: 0

Related Questions