views:

1982

answers:

5

Hey all,

how can i navigate through all my text fields with the "Next" Button on the iPhone Keyboard.

The last text field should close the Keyboard.

I setup in the IB the Buttons (Next / Done) but now im stuck.

I implemented the textFieldShouldReturn action but now the Next and Done Buttons close the Keyboard.

Thanks for your help!

A: 

in textFieldShouldReturn you should check that the textfield you are currently on is not the last one when they click next and if its n ot dont dismiss the keyboard..

Daniel
Hey,ok this works, but how can i jump to the next textfield with the "Next" Button?
phx
good question...
Daniel
+2  A: 

After you exit from one text field, you call [otherTextField becomeFirstResponder] and the next field gets focus.

This can actually be a tricky problem to deal with since often you'll also want to scroll the screen or otherwise adjust the position of the text field so it's easy to see when editing. Just make sure to do a lot of testing with coming into and out of the text fields in different ways and also leaving early (always give the user an option to dismiss the keyboard instead of going to the next field, usually with "Done" in the nav bar)

Kendall Helmstetter Gelner
+8  A: 

In Cocoa for Mac OS X you have the next responder chain, where you can ask the text field what control should have focus next. This is what makes tabbing between text fields work. But since iPhone do not have a key board, only touch, this concept has not survived the transition to Cocoa Touch.

This can be easily done anyway, with two assumptions:

  1. All "tabbable" UITextFields are on the same parent view.
  2. Their "tab-order" is defined by the tag property.

Assuming this you can override textFieldShouldReturn: as this:

-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
  NSInteger nextTag = textField.tag + 1;
  // Try to find next responder
  UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
  if (nextResponder) {
    // Found next responder, so set it.
    [nextResponder becomeFirstResponder];
  } else {
    // Not found, so remove keyboard.
    [textField resignFirstResponder];
  }
  return NO; // We do not want UITextField to insert line-breaks.
}

Add some more code, and the assumptions can be ignored as well.

PeyloW
Thanks! Works like it should (after i set the tag property :) )
phx
Sometimes there is are "next" and "previous" buttons on top of the keyboard. At least in the browser.
Tim Büthe
A: 

Thank you!!! :)

DAVID HALL
A: 

what if the next text field by tag number is in another cell or another cell in another section of s table view?