Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move from one text field to another

Tags:

xcode

ios

swift

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

enter image description here

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
}
like image 497
X.BOZO Avatar asked Dec 05 '25 18:12

X.BOZO


1 Answers

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

like image 97
Developer Avatar answered Dec 08 '25 11:12

Developer