NSNumberFormatter 不允许输入十进制数字 [英] NSNumberFormatter doesn't allow typing decimal numbers

查看:27
本文介绍了NSNumberFormatter 不允许输入十进制数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用 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 方法在字段中的某些内容发生变化时运行,并且您调用任何 -typeValue 方法,则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.

我的技巧是避免使用 -typeValue 方法并查看 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆