NumberPad restrict numeric input
I have some simple UITextFields t开发者_如何学Pythonhat bring up the Numberpad.
I would like to restrict the control the user has over input.
For example, one particular textField should not been allowed any value over 32.
Which parameter do I use with <32 to enable this?
Thanks
I would implement this UITextFieldDelegate method:
- (BOOL) textField: (UITextField *) textField shouldChangeCharactersInRange: (NSRange) range replacementString: (NSString *) string
{
NSString *result = [textField.text stringByReplacingCharactersInRange: range withString: string];
if ( [result intValue] < 0 || [result intValue] > 32 )
return NO;
return YES;
}
For consistency, you may also want to implement -textFieldShouldEndEditing: to return false when the value is invalid.
You could use
[aTextfield.text intValue] < 32
to compare the text in the Textfield to 32. ;-)
精彩评论