简体   繁体   English

如何强制NSTextField仅允许数字?

[英]How can I force NSTextField to only allow numbers?

In Interface Builder I've created a textfield and stuck an NSNumberFormatter into the cell. 在Interface Builder中,我创建了一个文本字段并将NSNumberFormatter粘贴到单元格中。 However, the user can still type text into the textfield. 但是,用户仍然可以在文本字段中键入文本。 Is there any way to restrict the input to numbers only using interface builder? 有什么方法可以仅使用界面生成器将输入限制为数字吗? I thought that was the point of the NSNumberFormatter. 我认为那是NSNumberFormatter的重点。

Every formatter has this method: 每个格式化程序都有以下方法:

- (BOOL) getObjectValue: (id*) object forString: (NSString*) string 
    errorDescription: (NSError**) error;

This is valid for every formatter. 这对于每个格式化程序均有效。
If the string is not valid it returns false and the object passed as argument (dereferenced) will be nil. 如果字符串无效,则返回false,并且作为参数传递的对象(取消引用)将为nil。
So instead of dropping a number formatter to the text field, use your own formatter as instance variable.Observe the text field implementing the delegate protocol so that at every change of the text you can be notified. 因此,不要将数字格式化程序放在文本字段中,而是使用自己的格式化程序作为实例变量。观察实现委托协议的文本字段,以便在每次更改文本时都可以得到通知。
Then invoke this method: 然后调用此方法:

NSNumber* number;
BOOL success=[formatter getObjectValue: &number forString: [myTextField stringValue] errorDescription:nil];

At this point if success is false (or check if number is nil), there is an invalid string in the text field, so do the action that is more appropriate for you (maybe delete the entire string, or display 0.0 as value). 此时,如果成功为假(或检查number是否为nil),则在文本字段中有无效的字符串,因此执行更适合您的操作(可能删除整个字符串,或将0.0显示为值)。

There is also another method: 还有另一种方法:

- (BOOL) isPartialStringValid : (NSString*) partialString: (NSString*) partialString
    errorDescription: (NSString*) error;

This way you can know if a partial string is valid.For example with the scientific notation "1e" is not valid, but is partially valid because the user may add 1 and it will become "1e1". 这样,您就可以知道部分字符串是否有效。例如,科学符号“ 1e”无效,但部分有效,因为用户可以加1并将其变为“ 1e1”。

Create an NSNumberFormatter subclass and put this in the implementation. 创建一个NSNumberFormatter子类,并将其放入实现中。 In your code, set the YKKNumberFormatter as the formatter for the NSTextField/UITextField. 在您的代码中,将YKKNumberFormatter设置为NSTextField / UITextField的格式化程序。

@implementation YKKNumberFormatter

- (BOOL)isPartialStringValid:(NSString *)partialString newEditingString:(NSString **)newString errorDescription:(NSString **)error {
    // Make sure we clear newString and error to ensure old values aren't being used
    if (newString) { *newString = nil;}
    if (error) {*error = nil;}

    static NSCharacterSet *nonDecimalCharacters = nil;
    if (nonDecimalCharacters == nil) {
        nonDecimalCharacters = [[[NSCharacterSet decimalDigitCharacterSet] invertedSet] retain];
    }

    if ([partialString length] == 0) {
        return YES; // The empty string is okay (the user might just be deleting everything and starting over)
    } else if ([partialString rangeOfCharacterFromSet:nonDecimalCharacters].location != NSNotFound) {
        return NO; // Non-decimal characters aren't cool!
    }

    return YES;
}

@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM