У меня есть четыре текстовых поля для otp
, и когда я нажимаю первый textfield
, он переходит к следующему textfeild
, а когда я нажимаю backspace
на втором textfeild
, он должен возвращаться к первому textfeild
назад.а оно не идет?
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
{
if textField == otpNumberOne || (otpNumberTwo != nil) || (otpNumberThree != nil) || (otpNumberFour != nil)
{
guard NSCharacterSet(charactersIn: "0123456789").isSuperset(of: CharacterSet(charactersIn: string)) else
{
return false
}
let maxLength = 1
let currentString: NSString = textField.text! as NSString
let newString: NSString =
currentString.replacingCharacters(in: range, with: string) as NSString
if (range.length == 0){
if textField == otpNumberOne {
otpNumberTwo?.becomeFirstResponder()
}
if textField == otpNumberTwo {
otpNumberThree?.becomeFirstResponder()
}
if textField == otpNumberThree {
otpNumberFour?.becomeFirstResponder()
}
if textField == otpNumberFour {
otpNumberFour?.resignFirstResponder()
}
textField.text? = string
otp = "\(otpNumberOne.text!)\(otpNumberTwo.text!)\(otpNumberThree.text!)\(otpNumberFour.text!)"
if otpNumberOne.hasText && otpNumberTwo.hasText && otpNumberThree.hasText && otpNumberFour.hasText
{
if vcode == otp
{
if InternetStatusClass.sharedInstance.isConnected == true
{
self.hud = MBProgressHUD.showAdded(to: UIApplication.shared.keyWindow!, animated: true)
self.hud.label.text = "Loading..."
executeVerifyMobileServerAPI()
}
else
{
let alertController = UIAlertController(title: "Error", message: "No connection", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
}
}
return false
}else if (range.length == 1) {
if textField == otpNumberFour {
otpNumberThree?.becomeFirstResponder()
}
if textField == otpNumberThree {
otpNumberTwo?.becomeFirstResponder()
}
if textField == otpNumberTwo {
otpNumberOne?.becomeFirstResponder()
}
if textField == otpNumberOne {
otpNumberOne?.resignFirstResponder()
}
textField.text? = ""
return false
}
return newString.length <= maxLength
}
return true
}