首页 > 代码库 > iPad开发中--控制UITextField只允许数字和点的输入
iPad开发中--控制UITextField只允许数字和点的输入
说明:iPhone开发中如果需要控制UITextField只允许数字和点的话,只需要弹出UIKeyboardTypeDecimalPad样式的键盘即可,但是在iPad中这样不行,需要通过下面方法控制
#define NUMBERS @"0123456789."
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// return NO to not change text
if (textField.tag == NumberEdit.tag || textField.tag == UnitsPriceEdit.tag) { NSCharacterSet*cs;
cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet];
NSString*filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
BOOL basicTest = [string isEqualToString:filtered];
if(!basicTest) {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"请输入数字或点"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
return NO;
}
}
return YES;
}
iPad开发中--控制UITextField只允许数字和点的输入