I'm designing an iOS app and I want that when the return key is pressed in my iPhone it directs me to the next following text field.
I have found a couple of similar questions, with excellent answers around but they all just happen to be in Objective-C and I'm looking for Swift code, now this is what I have up until now:
func textFieldShouldReturn(emaillabel: UITextField) -> Bool{
return true
}
It's placed in the file that's connected and controller to the UIView that contains the text fields, but I'm not sure if thats the right place.
I'm basically new to swift, so explain every little step or i'll manage to mess it up somehow. Also I'm using the latest version of Xcode if that makes any difference.
okay, so I tried this out and got this error:
//could not find an overload for '!=' that accepts the supplied arguments
func textFieldShouldReturn(textField: UITextField) -> Bool {
let nextTag: NSInteger = textField.tag + 1;
// Try to find next responder
let nextResponder: UIResponder = textField.superview!.viewWithTag(nextTag)!
if (nextResponder != nil) {//could not find an overload for '!=' that accepts the supplied arguments
// Found next responder, so set it.
nextResponder.becomeFirstResponder()
} else {
// Not found, so remove keyboard.
textField.resignFirstResponder()
}
return false; // We do not want UITextField to insert line-breaks.
}
Thanks in advance pals!!
Swift 3.0 (check edit history for previous versions):
Make sure your
UITextField
delegates are set and the tags are incremented properly. This can also be done through the Interface Builder.Here's a link to an Obj-C post I found: How to navigate through textfields (Next / Done Buttons)
I have tried many codes and finally this worked for me in Swift 3.0 Latest [March 2017]
The "ViewController" class should inherited the "UITextFieldDelegate" for making this code working.
Add the Text field with the Proper Tag nuber 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 as "Next" you also have other options as "Join/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 incrementation
This approach needs some changes in table views and collection views, but it's okay for simple forms I guess.
Connect your
textFields
to oneIBOutletCollection
, sort it by its y coordinate and intextFieldShouldReturn(_:)
just jump to the next textfield until you reach the end:Or just look at sample project (xcode 7 beta 4)
An alternative method for purists who don't like using tags, and wants the UITextField delegate to be the cell to keep the components separated or uni-directional...
Create a new protocol to link the Cell's and the TableViewController.
Add the protocol to your cell, where your TextField Delegate is also the cell (I do this in the Storyboard).
Make your TableViewController conform to the CellResponder protocol (i.e.
class MyTableViewController: UITableViewController, CellResponder
) and implement the method as you wish. I.e. if you have different cell types then you could do this, likewise you could pass in the IndexPath, use a tag, etc.. Don't forget to setcell.responder = self
in cellForRow..the easiest way to change to next text Field is this no need for long code
Swift 4
You can easily switch to another TextField.
UITextFieldDelegate
and addtextFieldShouldReturn(_:)
method in ViewControllerdelegate
property. Note : Do this for all TextFieldCreate an
IBOutlet
for all TextFields