0

I have a OTP screen with 6 textfields and I need the cursor to not to move to the particular textField when I tap on the particular textField.

I already implemented the code to automatically move the cursor from one text field to another but if I touch on a particular text field (let's say 4th) the cursor is moving there.

Please help me with this problem and thanks in advance.

My OTP screen:

My Code:

func setDelegateAndTargetForAllTextFields() {

    textField1.delegate = self
    textField2.delegate = self
    textField3.delegate = self
    textField4.delegate = self
    textField5.delegate = self
    textField6.delegate = self

    textField1.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: UIControl.Event.editingChanged)
    textField2.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: UIControl.Event.editingChanged)
    textField3.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: UIControl.Event.editingChanged)
    textField4.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: UIControl.Event.editingChanged)
    textField5.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: UIControl.Event.editingChanged)
    textField6.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: UIControl.Event.editingChanged)



}

@IBAction func otpBtnTapped(_ sender: UIButton) {

    if let text1 = textField1.text, let text2 = textField2.text, let text3 = textField3.text, let text4 = textField4.text,let text5 = textField5.text,let text6 = textField6.text {

        if text1+text2+text3+text4+text5+text6 == ""{

            showAlert(message: "Otp Cannot be empty!.")

            textField1.text = ""
            textField2.text = ""
            textField3.text = ""
            textField4.text = ""
            textField5.text = ""
            textField6.text = ""
            textFieldIntermediateEditingDisabled()

        }
        else{

            let setPinVC = self.storyboard?.instantiateViewController(withIdentifier: "pinVC") as! SetPinVC
            self.navigationController?.pushViewController(setPinVC, animated: true)




        }

    }

}

@objc func textFieldDidChange(textField: UITextField) {

    let text = textField.text

    if text?.count == 1 {

        switch textField{

        case textField1:
            //if textField2.isEnabled == false{
                //textField2.isEnabled = true
            //}
            textField2.becomeFirstResponder()
        case textField2:
            //if textField3.isEnabled == false{
               // textField3.isEnabled = true
           // }
            textField3.becomeFirstResponder()
        case textField3:
            //if textField4.isEnabled == false{
                //textField4.isEnabled = true
            //}
            textField4.becomeFirstResponder()
        case textField4:
            //if textField5.isEnabled == false{
                //textField5.isEnabled = true
            //}
            textField5.becomeFirstResponder()
        case textField5:
           // if textField6.isEnabled == false{
                //textField6.isEnabled = true
            //}
            textField6.becomeFirstResponder()
        default:
            break

        }

    }

    if text?.count == 0  {

        switch textField {

        case textField1:
            //if textField1.isEnabled == false{
                //textField1.isEnabled = true
            //}
            textField1.becomeFirstResponder()
        case textField2:
           // if textField1.isEnabled == false{
                //textField1.isEnabled = true
            //}
            textField1.becomeFirstResponder()
        case textField3:
            //if textField2.isEnabled == false{
                //textField2.isEnabled = true
            //}
            textField2.becomeFirstResponder()
        case textField4:
            //if textField3.isEnabled == false{
                //textField3.isEnabled = true
            //}
            textField3.becomeFirstResponder()
        case textField5:
            //if textField4.isEnabled == false{
               // textField4.isEnabled = true
            //}
            textField4.becomeFirstResponder()
        case textField6:
            //if textField5.isEnabled == false{
                //textField5.isEnabled = true
            //}
            textField5.becomeFirstResponder()
        default:
            break

        }
    }

}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {

    textField.resignFirstResponder()

    return true

}


func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {


    let maxLength = 1
    let currentString: NSString = textField.text! as NSString
    let newString: NSString =
        currentString.replacingCharacters(in: range, with: string) as NSString
    return newString.length <= maxLength

}

enter image description here

Ankit Jayaswal
  • 5,549
  • 3
  • 16
  • 36

1 Answers1

1

You can use textFieldShouldBeginEditingto check and allow/discard textField to begin editing like this

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {         
    return true // edit the condition according to your requirement
}

Edit

If you want to check and allow if the previous textfield have value or not you can do something like this just put all the textfield into an array with there sequence from one to so on

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    let textfields : [UITextField] = [textfield1,textfield2,textfield3] // just to demonstrate
    if (textField == textfield1) { return true }
    var previousTextfield : UITextField?
    for textfield in textfields{
        if let pTextfiled = previousTextfield,let value = pTextfiled.text{
            return value.isEmpty ? false : (textfield == textField)
        }
        previousTextfield = textfield
    }
    return false
}

Note: I didn't tested this code just added the answer according to the requirement let me know if this is what you wanted or something else.

Thanks.

tryKuldeepTanwar
  • 3,490
  • 2
  • 19
  • 49
  • my requirement is just that the cursor should not be on the intermediate textfields.So can u say the condition please.. – vivekananda Feb 04 '19 at 06:31
  • It is working only if there is no text in textfield1 but if we have some text in textField2 and if i click on textfield4 it is moving there – vivekananda Feb 05 '19 at 06:10
  • now it is stopping at second textField itself when i was entering the otp for the first time itself – vivekananda Feb 05 '19 at 10:42
  • there is something wrong with your code or you are doing something different "now it is stopping at second textField itself when i was entering the otp for the first time itself" this case cannot produce according to my solution. – tryKuldeepTanwar Feb 05 '19 at 11:07