简体   繁体   中英

navigate through textfields

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM