UITextField应该只接受数字值 [英] UITextField Should accept number only values

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

问题描述

我有UITexfields我希望它只接受输入数值的其他显示警报的数字。
我希望motionSicknessTextFiled只接受数字

I have UITexfields i want that it should accept only number other shows alert that enter a numeric value. I want that motionSicknessTextFiled should only accept number

NSString*dogswithMotionSickness=motionSicknessTextField.text;
NSString*valueOne=cereniaTextField.text;
NSString*valueTwo=prescriptionTextField.text;
NSString*valueThree=otherMeansTextField.text;
NSString*valueFour=overtheCounterTextField.text;


推荐答案

无论你从哪个UITextField获取这些值,您可以指定当某人触摸文本字段时您想要显示的键盘类型。

In whatever UITextField you're getting these values from, you can specify the kind of keyboard you want to appear when somebody touches inside the text field.

EG一个纯数字键盘。

E.G. a numeric-only keyboard.

喜欢这个截图:

使用时可以轻松设置XIB和Interface Builder内置于Xcode中,但是如果你想以编程方式理解它,请看一下Apple的 UITextInputTraits 协议参考页面,特别是 keyboardType 属性信息。

This is easily set when working with the XIB and the Interface Builder built into Xcode, but if you want to understand this programmatically, take a look at Apple's UITextInputTraits protocol reference page, specifically the keyboardType property information.

要过滤掉标点符号,请设置文本字段的委托并设置shouldChangeCharactersInRange方法:

To filter out punctuations, set the textfield's delegate and set up the shouldChangeCharactersInRange method:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *numbersOnly = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    NSCharacterSet *characterSetFromTextField = [NSCharacterSet characterSetWithCharactersInString:textField.text];

    BOOL stringIsValid = [numbersOnly isSupersetOfSet:characterSetFromTextField];
    return stringIsValid;
}

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

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