NSNumberFormatter不允许输入十进制数字 [英] NSNumberFormatter doesn't allow typing decimal numbers
问题描述
我对NSNumberFormatter感到迷惑不解.这应该很简单,但我无法正常工作.
I am totally bewildered using NSNumberFormatter. This should be totally simple but I can't get it to work.
我想将NSTextField设置为允许键入带小数点或不带小数点的十进制数字.这是我认为可行的方法:
I'd like to set an NSTextField to allow typing decimal numbers, either with a decimal point or without. Here is what I'd think would work:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMinimumFractionDigits:0];
[formatter setMaximumFractionDigits:4];
[formatter setAllowsFloats:YES];
[[timeFlowMultiplierTF cell] setFormatter:formatter];
但是,在文本字段中键入时,按小数点的句点"键不会产生一个.键入"3.14"给我"314".最初输入[formatter setAlwaysShowsDecimalSeparator:YES]会正确格式化数字,但是如果我键入数字,我将再次无法输入小数点.
However, when typing in the textfield, pressing the "period" key for the decimal point doesn't yield one. Typing "3.14" give me "314". Throwing in [formatter setAlwaysShowsDecimalSeparator:YES] will initially format the number correctly, but if I type over it, I once again cannot type the decimal point.
我在这里想念什么?您会认为这真的很简单
What am I missing here? You would think this would be really simple
推荐答案
我意识到这已经晚了大约4年,但是我只是遇到了同样的废话,以为我会分享问题的根源(或可能是) ,以供后代使用.
I realize this is about 4 years too late, but I just ran into this same nonsense and thought I'd share what the problem is (or could be), for posterity.
事实证明,NSTextField的所有值访问器(-objectValue
,-stringValue
,-doubleValue
等)最终都调用-validateEditing
.反过来,-validateEditing
使用所附的NSFormatter
将编辑后的文本转换为对象值 ,然后使用重新格式化的值重置字段中的文本 .
It turns out that all of the value accessors of NSTextField (-objectValue
, -stringValue
, -doubleValue
, and so on) all end up calling -validateEditing
. -validateEditing
, in turn, uses the attached NSFormatter
to convert the edited text into an object value, and then resets the text in the field with the reformatted value.
因此,如果您有任何代码在用户编辑字段时监视该字段,并且您偷看"该字段中的值,那么您将无意中重新格式化并重置文本字段中的文本.
So if you have any code that watches the field as the user edits it and you "peek" at the value in the field, you are inadvertently reformatting and resetting the text in the text field.
这并不是说文本字段不允许您输入句点;这是因为文本字段中已经包含"3",并且当您键入句点时,文本将变为"3".如果您有一个action/notification/delegate方法,该方法在字段中的某些内容发生变化时都运行,并且您调用了- type Value方法中的任何一种,则为"3".格式设置为"3",它会更新单元格,从而删除您刚刚键入的句点.
It's not that the text field won't let you type a period; it's that is the text field already has "3" in it and when you type a period the text changes to "3.". If you then have an action/notification/delegate method that runs whenever something in the field changes, and you call any of the -typeValue methods, the "3." get formatted as "3" and it updates the cell, erasing the period you just typed.
我的hack方式是避免使用- type Value方法,并窥视NSText
对象以直接获取编辑后的文本,而不会触发-validateEditing
:
My hack was to avoid the -typeValue methods and peek into the NSText
object to get the edited text directly, without triggering -validateEditing
:
// some method that runs every time the field changes...
NSTextField* valueField = self.valueField;
NSNumberFormatter* fieldFormatter = valueField.formatter;
NSText* fieldEditor = valueField.currentEditor;
id newValue = ( fieldEditor!=nil ? [fieldFormatter numberFromString:fieldEditor.string] : valueField.objectValue );
这篇关于NSNumberFormatter不允许输入十进制数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!