1

I want the firstResponder to go to the previous TextField when the back or return key is pressed. But the textFieldShouldReturn does not get launched. Many thanks guys!

Tried this but it doesn't seem to work.

MyViewController.h

@interface MyViewController : UIViewController <UITextFieldDelegate>

@property (strong, nonatomic) IBOutlet UITextField *firstCodeField;

MyViewController.m

@synthesize firstCodeField;

- (void)viewDidLoad
{
[super viewDidLoad];
firstCodeField.delegate = self;
secondCodeField.delegate = self;
thirdCodeField.delegate = self;
fourthCodeField.delegate = self;
fifthCodeField.delegate = self;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSUInteger newLength = [textField.text length] + [string length] - range.length;

    if (string.length == 0 && textField.text.length)
    {
        if(textField==fifthCodeField) {
            NSLog(@"It goes to fourth textfield but doesn't clear the number in the cell.");
            [textField resignFirstResponder];
            [fourthCodeField becomeFirstResponder];
            return NO;
        }
    }

    return (newLength > 1) ? NO : YES;
}

enter image description here

Community
  • 1
  • 1
Jim Clermonts
  • 1,694
  • 8
  • 39
  • 94

1 Answers1

0

Back space has to be recognized in textField :shouldChangeCharactersInRange:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
      {
        if (string.length == 0 //BackSpace
            && textField.text.length)//No Characters in text field
        {
            [textField resignFirstResponder];
            return NO;
        }
        return YES;
    }

Updated answer. When the fifth field is empty, clicking back space moves to fourth field and clears the last character in fourth

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        NSUInteger newLength = [textField.text length] + [string length] - range.length;

        if (string.length == 0 && textField.text.length)
        {
            if(textField==fifthCodeField) {
                NSLog(@"It goes to fourth textfield but doesn't clear the number in the cell.");
                [textField resignFirstResponder];

                //Character range for last character
                NSRange fourthFieldRange;
                range.length = 1;
                range.location = field.text.length - 1;

                NSString *text = [fourthCodeField.text stringByReplacingCharactersInRange:fourthFieldRange withString:@""];
                 [fourthCodeField becomeFirstResponder];
                return NO;
            }
        }

        return (newLength > 1) ? NO : YES;
    }
nprd
  • 1,942
  • 1
  • 13
  • 16
  • Hi, tried your code. It goes from the fifth textfield to the fourth. But that's it. It doesn't go to third, second, first. – Jim Clermonts May 31 '14 at 15:46
  • you have to add delegate for all the text fields as self. If you put a break point in this method, does that stop for third and second textfields? – nprd May 31 '14 at 15:51
  • Hi @Naveen Prasad R I'm almost there. With your code it goes to fourth textfield but doesn't clear the number in the cell. – Jim Clermonts Jun 01 '14 at 10:16
  • What do you want to do, when the user clicks back button in fifth cell (if fifth cell has no text), you want it to move to fourth cell and clear the text in the fourth cell? – nprd Jun 01 '14 at 14:58
  • If you are expecting the same, please find the updated answer. – nprd Jun 01 '14 at 15:11