UITextField只接受iphone中的数字 [英] UITextField Should accept numbers only in iphone

查看:72
本文介绍了UITextField只接受iphone中的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UITextField只能通过使用UITableView接受这些数字。

UITextField have to accept the numbers only by using UITableView.

推荐答案

你必须执行两个步骤:


  1. 提供键盘类型为 UIKeyboardTypeNumberPad ,如其他人所述。

  1. Provide keyboard type as UIKeyboardTypeNumberPad, as mentioned by others.
  2. In

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

方法检查输入的字符串是否为数字。您可以使用以下方法检查数值:

method check that entered string in numeric or not.You can use following method to check numeric value:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if([self isNumeric:string])
        return TRUE;
    else
        return FALSE;
}

-(BOOL)isNumeric:(NSString*)inputString
{
    NSCharacterSet *cs=[[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
    NSString *filtered;
    filtered = [[inputString componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    return [inputString isEqualToString:filtered];
}


这篇关于UITextField只接受iphone中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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