iPad - 如何在文本字段中仅输入 0~9?

发布于 2024-11-05 19:58:58 字数 150 浏览 1 评论 0原文

你好
我知道ipad键盘不像iphone可以设置“UIKeyboardTypeNumberPad”!!
但如果我想要它只能在文本字段上输入并显示数字 0 到 9。
如何比较用户在文本字段中键入的内容是否是数字?

提前致谢。

小型的

Hello
I know ipad keyboard doesn't like iphone can set "UIKeyboardTypeNumberPad"!!
But if I wanna it only can type and show number 0 to 9 on textfield.
How to compare what user key in on textfield are numbers or not ??

Thank in advance.

Mini

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

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

发布评论

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

评论(4

脸赞 2024-11-12 19:58:58

不要在显示后比较数字,而是在 shouldChangeCharactersInRange 中进行,

一定要声明委托 UITextFieldDelegate,而我总是忘记的事情是,确保 textField 本身的委托指向其中包含代码的类。

//---------------------------------------------------------------------------
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([string length] == 0 && range.length > 0)
    {
        textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
        return NO;
    }

    NSCharacterSet *nonNumberSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
    if ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0)return YES;

    return NO;
}

instead of comparing a figure after it is displayed, do it in the shouldChangeCharactersInRange

be sure to declare the delegate UITextFieldDelegate, and something i always forget, make sure the delegate of the textField itself is pointing at the class that has the code in it.

//---------------------------------------------------------------------------
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([string length] == 0 && range.length > 0)
    {
        textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
        return NO;
    }

    NSCharacterSet *nonNumberSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
    if ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0)return YES;

    return NO;
}
终难愈 2024-11-12 19:58:58

查看线程。它解决了我的类似问题。

Take a look at this thread. It solved my similar problem.

梦里南柯 2024-11-12 19:58:58

如何使用返回键关闭 UITextView 的键盘怎么样??

这个想法是,每次用户按下某个键时,您都会进行检查,如果是数字,则让它通过。否则忽略它。

让您的控制器支持 UITextViewDelegate 协议并实现 textView:shouldChangeTextInRange:replacementText: 方法。

How about How to dismiss keyboard for UITextView with return key??

The idea is you check every time the user hits a key, and if it is a number let it through. Otherwise ignore it.

Make your Controller supports the UITextViewDelegate protocol and implement the textView:shouldChangeTextInRange:replacementText: method.

⊕婉儿 2024-11-12 19:58:58
(BOOL) textField: (UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString: (NSString *)string {

    NSNumberFormatter * nf = [[NSNumberFormatter alloc] init];
    [nf setNumberStyle:NSNumberFormatterNoStyle];

    NSString * newString = [NSString stringWithFormat:@"%@%@",textField.text,string];
    NSNumber * number = [nf numberFromString:newString];

    if (number) {
         return YES;

    } else
        return NO;
}
(BOOL) textField: (UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString: (NSString *)string {

    NSNumberFormatter * nf = [[NSNumberFormatter alloc] init];
    [nf setNumberStyle:NSNumberFormatterNoStyle];

    NSString * newString = [NSString stringWithFormat:@"%@%@",textField.text,string];
    NSNumber * number = [nf numberFromString:newString];

    if (number) {
         return YES;

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