iOS app "next" key won't go to the next text field
The return key doesn't do anything special in a text field by default. You need to explicitly change the first responder:
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if (theTextField == self.textPassword) {
[theTextField resignFirstResponder];
} else if (theTextField == self.textUsername) {
[self.textPassword becomeFirstResponder];
}
return YES;
}
I use this code that allows me to control the form behavior in the storyboard:
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
if(textField.returnKeyType==UIReturnKeyNext) {
UIView *next = [[textField superview] viewWithTag:textField.tag+1];
[next becomeFirstResponder];
} else if (textField.returnKeyType==UIReturnKeyDone) {
[textField resignFirstResponder];
}
return YES;
}
All you need is to assign the order value as tag
, and returnkey
according to Next or Done on each input control.
for those who want to use Swift language:
class MyClass: UIViewController, UITextFieldDelegate {
@IBOutlet weak var text1: UITextField!
@IBOutlet weak var text2: UITextField!
@IBOutlet weak var text3: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
text1.delegate = self
text2.delegate = self
text3.delegate = self
}
//....
func textFieldShouldReturn(textField: UITextField) -> Bool{
if textField == self.text1 {
self.text2.becomeFirstResponder()
}else if textField == self.text2{
self.text3.becomeFirstResponder()
}else{
self.text1.becomeFirstResponder()
}
return true
}
//...
}
:-)