swiftuitextfielduitextfielddelegate

How UITextField move to next TextField When Click on Return Key in Keyboard


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
}

Solution

  • 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.