How can I make text field-1 move to another text field-2 by Swift when I write two numbers?

When I write 22, I want to automatically move to another text field-2.
I tried this but it doesn't work:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if let text = textField.text {
let newStr = (text as NSString)
.stringByReplacingCharactersInRange(range, withString: string)
if newStr.isEmpty {
return true
}
let intvalue = Int(newStr)
if textField.tag == 101 { print("101") // only 2 integer
return (intvalue >= 0 && intvalue <= 99) ? true : false
}
else if textField.tag == 102 { print("102") // only 4 integer
return (intvalue >= 0 && intvalue <= 9999) ? true : false
}
}
return true
}
textField.addTarget(self, action: #selector(textDidChanged(textField:)), for: .editingChanged)
func textDidChanged(textField: UITextField)
{
if (textField.text) == "22"
{
newTextField.becomeFirstResponder()
}
}
add delegate to both the textfields also add UITextFieldDelegate to your viewcontroller
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With