You can add an observer to UITextFieldTextDidChangeNotification
// add this line to viewDidLoad
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(limitTextField) name:UITextFieldTextDidChangeNotification object:nil];
// method for limiting text characters
-(void)limitTextField { textField.text = [textField.text substringToIndex:3]; }
// or set a text field delegate and use this
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if([textField.text length]<=3) return YES; else return NO; }
and set the keyBoardType text field as UIKeyboardTypeNumberPad to get only numbers. I did not test or compile the code, I just wrote the logic. Hope this helps :)
sanjaymathad
source share