Reputation: 235
I am using this code to move UITextField and it is working but I am not happy with this code and I want (when I click return key Then Cursor move to next UITextField) this function in my registration or login form anybody can suggest me.
func textFieldDidBeginEditing(_ textField: UITextField) {
switch textField {
case txtFldSponsorID:
moveTextfield(textfield: txtFldSponsorID, moveDistance: 0, up: true)
case txtFldFullName:
moveTextfield(textfield: txtFldFullName, moveDistance: -10, up: true)
case txtFldEmail:
moveTextfield(textfield: txtFldEmail, moveDistance: -10, up: true)
case txtFldMobile:
moveTextfield(textfield: txtFldMobile, moveDistance: -10, up: true)
case txtFldAddress:
moveTextfield(textfield: txtFldAddress, moveDistance: -80, up: true)
case txtFldCity:
moveTextfield(textfield: txtFldCity, moveDistance: -80, up: true)
default:
break
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
switch textField {
case txtFldSponsorID:
moveTextfield(textfield: txtFldSponsorID, moveDistance: 0, up: true)
case txtFldFullName:
moveTextfield(textfield: txtFldFullName, moveDistance: 10, up: true)
case txtFldEmail:
moveTextfield(textfield: txtFldEmail, moveDistance: 10, up: true)
case txtFldMobile:
moveTextfield(textfield: txtFldMobile, moveDistance: 10, up: true)
case txtFldAddress:
moveTextfield(textfield: txtFldAddress, moveDistance: 80, up: true)
case txtFldCity:
moveTextfield(textfield: txtFldCity, moveDistance: 80, up: true)
default:
break
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
Upvotes: 3
Views: 3805
Reputation: 470
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == txtFldSponsorID {
txtFldFullName.becomeFirstResponder()
} else if textField == txtFldFullName {
txtFldEmail.becomeFirstResponder()
} else if textField == txtFldEmail {
txtFldMobile.becomeFirstResponder()
} else if textField == txtFldMobile {
txtFldAddress.becomeFirstResponder()
} else {
txtFldCity.resignFirstResponder()
}
return true
}
You can use this above UITextField Delegate method to jump to next UItextField.
Upvotes: 4
Reputation: 182
1- Put a tag number for each textField in storyboard
2- Implement the textField delegate function:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField.tag == 1 { //say this is txtFldSponsorID
txtFldFullName.becomeFirstResponder()
}
return true
}
3- Change return key type in storyboard to "next" instead of "return", or with code:
txtFldSponsorID.returnKeyType = .next
4- Don't forget to set delegate = self
Upvotes: 0
Reputation: 2783
In order to move the cursor automatically to the next data entry field when the user presses Next on the keyboard you need to resignFirstResponder from the current field and assign it to the next field using becomeFirstResponder
if self.emaillabel.isEqual(self.anotherTextField)
{
self.anotherTextField.becomeFirstResponder()
}
Upvotes: 0