使用 numberFormatter 检查您输入的号码,如下所示:
NSNumber * myNumber = [numberFormatter numberFromString:yourTextField.text];
if(myNumber)
//valid entered number
else
//invalid entered number
您应该为此使用 UITextField 的委托方法:
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSNumberFormatter *numberFormatter = [NSNumberFormatter alloc]init]
[numberFormatter setNumberStyle:NSNumberFormatterNoStyle];
[numberFormatter setMaximumIntegerDigits:5];
[numberFormatter setMaximumFractionDigits:1];
NSNumber * myNumber = [numberFormatter numberFromString:string];
[numberFormatter release];
if(myNumber)
return YES;
else
return NO;
}