The problem here is that anything after the first return
is not executed.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 10) ? NO : YES;
// unreachable!
So you are just checking the length but not whether the input is numerical. Change this line:
return (newLength > 10) ? NO : YES;
with this one:
if (newLength > 10) return NO;
and it should work. You can also optionally change this:
[NSCharacterSet characterSetWithCharactersInString:@"0123456789"]
with this:
[NSCharacterSet decimalDigitCharacterSet]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…