UITextField应该只有正数 [英] UITextField should have only positive number
本文介绍了UITextField应该只有正数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要验证 UITextField
。我只想在其中输入正整数。我怎样才能实现这一目标?
I need to validate a UITextField
. I want only positive integers to be entered in it. How can I achieve that?
推荐答案
添加UITextFieldDelegate,并将此代码添加到您的班级
Add UITextFieldDelegate,and add this code to your class
#define LEAGELNUM @"0123456789"
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:LEAGELNUM] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
BOOL basicTest = [string isEqualToString:filtered];
return basicTest;
}
LEAGELNUM表示你只能写这些单词
LEAGELNUM means you can only write these words
这篇关于UITextField应该只有正数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文