Obj-C,如何确保用户在 UITextField 中输入 1 或以上?

发布于 2024-10-30 07:34:00 字数 610 浏览 0 评论 0原文

过去,我曾设法限制 shouldChangeCharactersInRange 事件中文本字段的长度,并应用货币格式。

然而这一次,我需要确保用户输入1或以上。

所以 0001 是不可接受的,因为零需要是 1 到 1000000。

我该怎么做?

这就是我到目前为止所拥有的

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:
   (NSRange)range replacementString:(NSString *)string {

    BOOL res = TRUE;

    NSString *newString = [textField.text stringByReplacingCharactersInRange:
      range withString:string];
    newString = [NSString stringWithFormat:@"%d", [newString intValue]];
    res = !([newString length] > 8);

    return res;
}

In the past, I've managed to limit the length of a textfield in the shouldChangeCharactersInRange event and also apply currency formatting.

However this time, I need to ensure that the user enters 1 or above.

So 0001 would be unacceptable as would zero it needs to be 1 through 1000000.

How would I do this ?

This is what I have so far

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:
   (NSRange)range replacementString:(NSString *)string {

    BOOL res = TRUE;

    NSString *newString = [textField.text stringByReplacingCharactersInRange:
      range withString:string];
    newString = [NSString stringWithFormat:@"%d", [newString intValue]];
    res = !([newString length] > 8);

    return res;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烂柯人 2024-11-06 07:34:00

UI 的一个好规则是:“在你做的事情上保持自由
接受,并且在发送的内容上保守”*。

不要因为不符合应用程序想要的格式的输入而惩罚用户,而是接受任何可以转换为正确格式的内容。如果您想要一个介于 1 和 1 之间的整数包含百万,0001 是一个奇怪但完全有效的输入,我建议这个解决方案:

// Only check the value when the user is _done_ editing.
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {

    NSInteger intVal = [textField.text integerValue];
    // Check whether the input, whatever it is, 
    // can be changed into an acceptable value
    if( (intVal <= 1000000) && (intVal >= 1) ){
        // If so, display the format we want so the 
        // user learns for next time
        textField.text = [[NSNumber numberWithInteger:intVal] stringValue];
        return YES;
    }

    // Else show a small error message describing 
    // the problem and how to remedy it
    return NO;
}

*:最初由 John Postel 制定为 “稳健性原则” 可能有更具体的 UI 说明,但我现在不记得了。;

A good rule for UI is: "Be liberal in what you
accept, and conservative in what you send"*.

Rather than punish the user for input which doesn't fit the format your app would like, accept anything that can be transformed into the proper format. If you want an integer between one and one million inclusive, 0001 is a weird but perfectly valid input. I suggest this solution:

// Only check the value when the user is _done_ editing.
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {

    NSInteger intVal = [textField.text integerValue];
    // Check whether the input, whatever it is, 
    // can be changed into an acceptable value
    if( (intVal <= 1000000) && (intVal >= 1) ){
        // If so, display the format we want so the 
        // user learns for next time
        textField.text = [[NSNumber numberWithInteger:intVal] stringValue];
        return YES;
    }

    // Else show a small error message describing 
    // the problem and how to remedy it
    return NO;
}

*: Originally formulated by John Postel as the "Robustness Principle"; there may be a more UI-specific statement of it, but I can't recall at the moment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文