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.
I had about 10+ UITextField in my story board and the way I enabled next functionality was by creating an array of UITextField and making the next UITextField the firstResponder. Here's the implementation file:
This worked for me in Xamarin.iOS / Monotouch. Change the keyboard button to Next, pass the control to the next UITextField and hide the keyboard after the last UITextField.
Inside the ViewDidLoad you'll have:
If your TextFields haven't a Tag set it now:
and just the call
I have tried many codes and finally, this worked for me in Swift 3.0 Latest [March 2017]
The
ViewController
class should be inherited theUITextFieldDelegate
for making this code working.Add the Text field with the Proper Tag number and this tag number is used to take the control to appropriate text field based on incremental tag number assigned to it.
In the above code, the
returnKeyType = UIReturnKeyType.next
where will make the Key pad return key to display asNext
you also have other options asJoin/Go
etc, based on your application change the values.This
textFieldShouldReturn
is a method of UITextFieldDelegate controlled and here we have next field selection based on the Tag value incrementationThis is an old post, but has a high page rank so I'll chime in with my solution.
I had a similar issue and ended up creating a subclass of
UIToolbar
to manage the next/previous/done functionality in a dynamic tableView with sections: https://github.com/jday001/DataEntryToolbarYou set the toolbar as inputAccessoryView of your text fields and add them to its dictionary. This allows you to cycle through them forwards and backwards, even with dynamic content. There are delegate methods if you want to trigger your own functionality when textField navigation happens, but you don't have to deal with managing any tags or first responder status.
There are code snippets & an example app at the GitHub link to help with the implementation details. You will need your own data model to keep track of the values inside the fields.
I tried to solve this problem using a more sophisticated approach based on assigning each cell (or
UITextField
) in aUITableView
a unique tag value that can be later retrieved: activate-next-uitextfield-in-uitableview-iosI hope this helps!
I like the OO solutions that have already been suggested by Anth0 and Answerbot. However, I was working on a quick and small POC, so I didn't want to clutter things with subclasses and categories.
Another simple solution is to create an NSArray of fields and lookup the next field when you press next. Not an OO solution, but quick, simple, and easy to implement. Also, you can see and modify the ordering at a glance.
Here's my code (built upon other answers in this thread):