textfield inputs range in interface builder
I have multiple textfields in my nib file. I want to decide the input range in my one textfield to 6-16 digits and I don't want to change any other textfield input. For that I made a method called tflimit as below.
-(IBAction)tflimit:(id)sender
{
if([textfields1.text length]>=15 )
{
[textfields1 resignFirstR开发者_开发百科esponder];
}
}
With this method I can input only 16 digits input. How can I decide the range(6-16) of an input in the textfield without changing other codes.
You can filter user input in textField:shouldChangeCharactersInRange:replacementString:
method in text field delegate:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if (textField == textfields1){// Apply logic only to required field
NSString* newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
return [newString length] < 16 && [newString length] > 5;
}
return YES;
}
Note that to work correctly this method require textfield to be pre-populated with text at least 5 characters long.
精彩评论