0

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.

Community
  • 1
  • 1
lolol
  • 4,287
  • 3
  • 35
  • 54

1 Answers1

2

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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • How do I do that? I'm very new to iOS. – lolol Oct 10 '12 at 16:02
  • 1
    see here: http://stackoverflow.com/questions/10287131/textfieldshouldreturn-is-not-called – vikingosegundo Oct 10 '12 at 16:03
  • and of course: [Apple's docs](https://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/ManageTextFieldTextViews/ManageTextFieldTextViews.html#//apple_ref/doc/uid/TP40009542-CH10-SW1) – vikingosegundo Oct 10 '12 at 16:06
  • now It's working. Thank you for the tip and the piece of code. And I think you are right, keeping the controls in a array uses more memory but it's more maintainable. – lolol Oct 10 '12 at 16:08
  • If we had to worry about the memory consumption of an array, we all were doomed… – vikingosegundo Oct 10 '12 at 16:11