轉(zhuǎn)載:http://blog.csdn.net/u010070526/article/details/50766679
和http://www.reibang.com/p/aeafd022869c
(1)
一特占、限制只能輸入數(shù)字,但輸入錯(cuò)誤沒(méi)有提示只是不顯示?
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {??
? NSString *result = [textField.text stringByReplacingCharactersInRange:range withString:string];? ?
?if (result.length == 0) return YES;? ?
?NSString *regex = @"^[1-9][0-9]*$";? ??
?NSPredicate *prd = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];? ? return [prd evaluateWithObject:result];
}
二议慰、限制只能輸入數(shù)字灾茁,但輸入錯(cuò)誤會(huì)有提示1.定義常量以備使用?1#define NUMBERS @"0123456789"2甫题、實(shí)現(xiàn)代理方法[html] view plain copy 在CODE上查看代碼片派生到我的代碼片
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string
{
NSCharacterSet*cs;
cs = [[NSCharacterSetcharacterSetWithCharactersInString:NUMBERS] invertedSet];
NSString*filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
BOOLbasicTest = [string isEqualToString:filtered];
if(!basicTest) {
UIAlertView* alert = [[UIAlertViewalloc] initWithTitle:@"提示"
message:@"請(qǐng)輸入數(shù)字"
delegate:nil
cancelButtonTitle:@"確定"
otherButtonTitles:nil];
[alert show];
returnNO;
}
returnYES;
}