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've setup the IB the Buttons (Next / Done) but now I'm stuck.
I implemented the textFieldShouldReturn action but now the Next and Done Buttons close the Keyboard.
Here's one without delegation:
ObjC:
Works using the (mostly unknown)
UIControlEventEditingDidEndOnExit
UITextField
action.You can also easily hook this up in the storyboard, so no delegation or code is required.
Edit: actually I cannot figure out how to hook this up in storyboard.
becomeFirstResponder
does not seem to be a offered action for this control-event, which is a pity. Still, you can hook all your textfields up to a single action in your ViewController which then determines which textField tobecomeFirstResponder
based on the sender (though then it is not as elegant as the above programmatic solution so IMO do it with the above code inviewDidLoad
).I have added to PeyloW's answer in case you're looking to implement a previous/next button functionality:
Where you subclass the UIView like this:
Hi to everyone please see this one
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)
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..
I am surprised by how many answers here fail to understand one simple concept: navigating through controls in your app is not something the views themselves should do. It's the controller's job to decide which control to make the next first responder.
Also most answers only applied to navigating forward, but users may also want to go backwards.
So here's what I've come up with. Your form should be managed by a view controller, and view controllers are part of the responder chain. So you're perfectly free to implement the following methods:
Additional logic for scrolling offscreen responders visible beforehand may apply.
Another advantage of this approach is that you don't need to subclass all kinds of controls you may want to display (like
UITextField
s) but can instead manage the logic at controller level, where, let's be honest, is the right place to do so.