0
votes

I'm trying to use the @PeyloW code that I found here How to navigate through textfields (Next / Done Buttons) but when I press the keyboard return button nothing happens. My tags are ok.

Header:

- (BOOL)textFieldShouldReturn:(UITextField*)textField;

Implementation:

- (BOOL)textFieldShouldReturn:(UITextField*)textField {
    NSInteger nextTag = textField.tag + 1;
    UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];

    if (nextResponder) {
        [nextResponder becomeFirstResponder];
    } else {
        [textField resignFirstResponder];
    }
    return NO;
}

What am I missing? My keyboard doesn't have next done, only return. Keep in mind that I'm very new to iOS.

EDIT:

I tried to debug the code by adding a breakpoint to it and the code isn't being triggered.

1

1 Answers

2
votes

I don't like solutions that incorporate the tag. Instead I would put all inputfileds in the desired order into an array and in -textFieldShouldReturn: use the given textfield to get it's index from in the array. Then I would get the object at that index.

- (BOOL)textFieldShouldReturn:(UITextField*)textField {
    NSUInteger nextIndex = [arrayWithResponders indexOfObject:textField]+1 % [arrayWithResponders count];
    UIResponder* nextResponder = [arrayWithTextFields objectAtIndex: nextIndex];

    if (nextResponder) {
        [nextResponder becomeFirstResponder];
    } else {
        [textField resignFirstResponder];
    }
    return NO;
}

You just added, that the breakpoints aren't triggered, so most likely you didn't set up the delegate.