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
}

You can use
textFieldShouldBeginEditingto check and allow/discard textField to begin editing like thisEdit
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
Thanks.