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!!
Caleb's version in Swift 4.0
P.S.
textField.superview?
not working for meJust use
becomeFirstResponder()
method of UIResponder class in yourtextFieldShouldReturn
method. EveryUIView
objects areUIResponder
's subclasses.You can find more information about
becomeFirstResponder()
method at Apple Doc's in here.No any special, here is my currently using to change the textFiled. So the code in ViewController looks good :). #Swift4
I suggest that you should use switch statement in
textFieldShouldReturn(_:)
.