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.
This is a simple solution in swift, with no tag using, no storyboard tricks...
Just use this extension :
And call it like this (for example) with your textfield delegate:
you can use IQKeyboardManager library to do this. it handle every thing, you don't need any additional setup.IQKeyboardManager is available through CocoaPods, to install it simply add the following line to your Podfile:
or Just drag and drop IQKeyBoardManager directory from demo project to your project. That's it. you can find IQKeyBoardManager directory from https://github.com/hackiftekhar/IQKeyboardManager
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 iOS devices do not have a keyboard, only touch, this concept has not survived the transition to Cocoa Touch.
This can be easily done anyway, with two assumptions:
UITextField
s are on the same parent view.Assuming this you can override textFieldShouldReturn: as this:
Add some more code, and the assumptions can be ignored as well.
Swift 4.0
If the superview of the text field will be a UITableViewCell then next responder will be
A swift extension that applies mxcl's answer to make this particularly easy (adapted to swift 2.3 by Traveler):
It's easy to use:
The extension will set the return button to "Next" for all but the last field and to "Done" for the last field, and shift focus / dismiss the keyboard when these are tapped.
Swift < 2.3
SWIFT 3: use like this -
I've just created new Pod when dealing with this stuff GNTextFieldsCollectionManager. It automatically handles next/last textField problem and is very easy to use:
Grabs all textfields sorted by appearing in view hierarchy (or by tags), or you can specify your own array of textFields.
I rather prefer to:
In the NIB file I hook the textFields in the desired order into this inputFields array. After that I do a simple test for the index of the UITextField that reports that the user tapped return: