Assuming your textField
s and button
s have a common sequence of tags, you can basically do something like the following:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
let nextView = textField.superview?.viewWithTag(textField.tag + 1)
if let nextTextField = nextView as? UITextField {
//next view is a textField so make next textField first responder
nextTextField.becomeFirstResponder()
}
//just this extra case is required
else if let nextButton = nextView as? UIButton {
//next view is a button so call it's associated action
//(assuming your button's @IBAction is on the Touch Up Inside event)
nextButton.sendActions(for: .touchUpInside)
/*
a button's action will be performed so if you want to
perform the return action on the textField then either
return true/false. Decide that as per your requirement
*/
return true
}
else {
//...
}
//...
}
The above logic is enough to answer your question but works only when the user is on a textField
and has tapped on the keyboard's Return
button.
Now... the thing is that on the end of a button actions, if you want to continue to the next view; textField
or button
, you can either1 explicitly code to make the next view active.
Example:
- After
ISD Code
is done you could make the mobile textField
as first responder
- After
Gender
is done, you could call the button action for Nationality
Or2...
We can modify the solution to handle textField
as well as button
s alike with a common helper function, that we will call goNext(from:)
to be implemented in textFieldShouldReturn(_:)
as well as after the button is to complete it's intended logic flow.
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
let nextView = goNext(from: textField)
if let nextTextField = nextView as? UITextField {
//Next field was a textField so keyboard should stay
return false
}
textField.resignFirstResponder()
return true
}
@discardableResult func goNext(from sender: UIView) -> UIView? {
let nextView = sender.superview?.viewWithTag(sender.tag + 1)
print(nextView?.tag ?? "No view with tag \(sender.tag + 1)")
if let nextTextField = nextView as? UITextField {
nextTextField.becomeFirstResponder()
}
else if let nextButton = nextView as? UIButton {
nextButton.sendActions(for: .touchUpInside)
}
else {
print("Done")
signupScrollView.setContentOffset(CGPoint(x: 0, y: 0),
animated: true)
sender.resignFirstResponder()
}
return nextView
}
Now for the button part, you need to perform goNext(from: button)
whenever the associated button has completed it's logic.
Example: