tags:

views:

43

answers:

2

Hi,

I have some simple UITextFields that 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

+1  A: 

You could use

[aTextfield.text intValue] < 32

to compare the text in the Textfield to 32. ;-)

Sandro Meier
Thanks Sandro. Would that actually restrict the user typing any value over 32?
No that's only to compare the value in the TextField. But you could show a message when he tries to end the editing... ;-)
Sandro Meier
+2  A: 

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.

Costique
Cheers Costique. I'll check this later.