1

I have 4 text fields horizontally. Each textFiled will allow only four characters. If user enters four characters focus should be changed to next text field with out any user interaction. By using below code,i am able to switch to next textFiled, but only after typing any character from the keyboard. My requirement is to get auto focus on next filed after entering four characters. Please help me in this. Thanks in Advance.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

if ( range.location  >= 4 ) {
  textField.text = string;        
    UIResponder* nextResponder = [textField.superview viewWithTag:(textField.tag + 1)];
    if (nextResponder) {           
        [textField resignFirstResponder];            
        [nextResponder becomeFirstResponder];          
    }        
    return NO;
}    
return YES;

}

Anil Kumar
  • 1,065
  • 1
  • 13
  • 24

4 Answers4

1

You can use something like this in this method:

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {

    if(theTextField == firstField)
    {
        [secondField becomeFirstResponder];
        return YES;
    }

    if(theTextField == secondField)
    {
        [thirdField becomeFirstResponder];
        [theTextField resignFirstResponder];
        return YES;
    }

    if(theTextField == thirdField)
    {
        [thirdField becomeFirstResponder];
        [theTextField resignFirstResponder];
        return YES;
    }
    [theTextField resignFirstResponder];
    return YES;
}
Tripti Kumar
  • 1,559
  • 14
  • 28
  • Hi, thanks for your quick reply. By using my code i am able to switch between textFields after entering the 4 letters. The problem is after clicking the fifth letter focus changed to second textFiled, but fifth letter not displayed in second text textField, because it took as a user event to switch to next. my requirement is like, if user click fifth letter it should display in the second textField. Thank u. – Anil Kumar Jun 15 '12 at 06:35
  • I think Abhishek's answer is right.That was the next answer that occured to me. – Tripti Kumar Jun 15 '12 at 08:20
0

I think you need something like this, https://github.com/lashad/PTPasscodeViewController

Dhruv
  • 2,153
  • 3
  • 21
  • 45
0

try replacing this

if ( range.location  >= 4 )

with

if ( range.location  >= 3 )
Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115
0
//try to this code 


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  if ([[textField text] length] + [string length] - range.length > 4) {

    textField.text = string;        
   UIResponder* nextResponder = [textField.superview viewWithTag:(textField.tag + 1)];
   if (nextResponder) {           
      [textField resignFirstResponder];            
      [nextResponder becomeFirstResponder];          
  }   

    return YES; 
}

hope this will help you

Abhishek
  • 2,255
  • 1
  • 13
  • 21
  • Thanks for all replies.. But sorry not working. @iApple yes your right i required in same way with textFields. – Anil Kumar Jun 15 '12 at 07:08
  • @Kumar do one thing.. you just remove return No from your code... try and please let me know.... – Abhishek Jun 15 '12 at 07:13
  • thanks for your suggestion. It's started working but i am facing one more problem like textField.tag values are 0,1,2,3. But while typing using keyboard textField navigation happening like 0->2->3(1 is missing 0->2). I dint understood the reason behind this..Thanks. – Anil Kumar Jun 15 '12 at 09:38
  • 1
    don't set the tag 0 its use like null try to start it from 1.2. and so on, i going to edit the upper code use just take it upper vote and right may be it will be useful for other one.... – Abhishek Jun 15 '12 at 09:43