I have to handle navigate through multiple UITextField
s using (Next / Done Button) and now I have to allow only one text/number in each UITextField
how could we do that in UITextField
as shown in image below
I have used following code recently and was able to achieve too but got problem that When I first enter the text/number in UITextField
it is being entered in UITextField
and when I enter the next text/number for second time it is being used only to push to next UITextField
. I want to achieve is that when i enter the text/number for second time it must be pushed to next UITextField
as well as value must be entered in next UITextField
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.tag<10) {
if ((textField.text.length >= 1) && (string.length > 0))
{
NSInteger nextText = textField.tag + 1;
// Try to find next responder
UIResponder* nextResponder = [textField.superview viewWithTag:nextText];
if (! nextResponder)
[textField resignFirstResponder];
// nextResponder = [textField.superview viewWithTag:1];
if (nextResponder){
// Found next responder, so set it.
[nextResponder becomeFirstResponder];
return NO;
}
}
}
return YES;
}
I solve my problem using following code but I got a one more problem left. For the last UITextField i need it to resignFirstResponder immediately after it is filled up. But in current when it is fill up and user click next text then only resignFirstResponder is happening
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.tag<10) {
if ((textField.text.length >= 1) && (string.length > 0))
{
NSInteger nextText = textField.tag + 1;
UIResponder* nextResponder = [textField.superview viewWithTag:nextText];
if (! nextResponder)
[textField resignFirstResponder];
if (nextResponder){
[nextResponder becomeFirstResponder];
UITextField* nextTextfield= (UITextField*) [textField.superview viewWithTag:nextText];
if ((nextTextfield.text.length < 1)){
[nextTextfield setText:string];
}
return NO;
}
}
}
return YES;
}