将 NSTextField 输入限制为仅数字?NSNumberformatter [英] Restrict NSTextField input to numeric only? NSNumberformatter
问题描述
我刚刚开始使用 Mac 应用程序开发,到目前为止一切都很好,我只是在尝试让 NSTextField 只接受输入数字时遇到问题.
I'm just getting started up with Mac App Development and so far everything is good well, I'm just having problems trying to get a NSTextField to only accept numbers for the input.
到目前为止,我已经向 NSTextField 添加了一个格式化程序并将其设置为现在只允许十进制,当我在字段中输入字母时它仍然允许我但不允许我在完成后单击单元格.
So far I have added a formatter to the NSTextField and set it to only allow decimal now when I enter letters in the field it still allows me but does not allow my to click out of the cell when done.
理想情况下,我希望应用程序不要让用户输入任何字母,然后只发出哔哔声或什么都不做.
Ideally I would like the app not to let the user type in any letters and just beep or do nothing.
关于此的任何提示或指示都会很棒
Any tips or pointers on this would be great
推荐答案
Subclass NSNumberFormatter
并实现这个方法:
Subclass NSNumberFormatter
and implement this method:
- (BOOL)isPartialStringValid:(NSString **)partialStringPtr
proposedSelectedRange:(NSRangePointer)proposedSelRangePtr
originalString:(NSString *)origString
originalSelectedRange:(NSRange)origSelRange
errorDescription:(NSString **)error
这篇关于将 NSTextField 输入限制为仅数字?NSNumberformatter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!