如何在代码中访问NSTextField中的NSNumberFormatter属性

时间:2021-07-21 05:15:47

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.

我已经为NSTextField创建了一个带有普通IBOulet的nib文件。在IB(4.4)中,为此NSTextField添加一个NSNumberFormatter,并为其提供一些属性,如Decimal Style,max和min值等。

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

现在,我想在代码中修改已经嵌入在文本字段中的NSNumberFormatter的一些其他属性,如SetGroupingSeparator,SetMaximunFractionDigits等。

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

假设我的NSTextField名为pf,通常我可以访问单元格中的值(字符串):

[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?.

这没有任何问题。但是,我无法在文档中或任何地方找到如何在代码中修改NSNumberFormatter属性的示例。我知道如何在代码中创建一个NSNumberFormatter,但不知道如何将它分配给NSTextField(我不知道是否会覆盖IB中的一个创建)。谁能帮我一个明确的例子如何做到这一点?

2 个解决方案

#1


1  

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

要在代码中创建和设置NSNumberFormatter到NSTextField,可以通过以下方式完成:

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

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

我在Apple的数据格式指南中找到的详细信息

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

修改格式化程序应该非常类似,通过[NSCell formatter]访问格式化程序:

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

#2


0  

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.

你就像IB中的任何其他对象一样。在.h文件中为格式化程序创建一个IBOutlet并将其连接到IB中的格式化程序,然后您可以通过插座访问其属性。

#1


1  

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

要在代码中创建和设置NSNumberFormatter到NSTextField,可以通过以下方式完成:

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

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

我在Apple的数据格式指南中找到的详细信息

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

修改格式化程序应该非常类似,通过[NSCell formatter]访问格式化程序:

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

#2


0  

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.

你就像IB中的任何其他对象一样。在.h文件中为格式化程序创建一个IBOutlet并将其连接到IB中的格式化程序,然后您可以通过插座访问其属性。