在快速删除表单一个文本字段到另一个文本字段时向后移动光标

时间:2018-03-08 04:07:15

标签: ios swift uitextfield

我已经使用这些代码将光标从一个移动到另一个UITextField,即

tField1 -> tField2 -> tField3 -> tField4

但我想在删除textField上反向移动

tField4(deleted)-> tField3(move cursor without clear text)
tField3(deletd)-> tField2(move cursor without clear text)

依旧...... 当我使用 becomeFirstResponder()时,它会清除文字

override func viewDidLoad() {
    super.viewDidLoad()
    tField1.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: UIControlEvents.editingChanged)
    tField2.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: UIControlEvents.editingChanged)
    tField3.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: UIControlEvents.editingChanged)
    tField4.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: UIControlEvents.editingChanged)
}


override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    tField1.becomeFirstResponder()
}

@objc func textFieldDidChange(textField: UITextField) {
    let text = textField.text
    if text?.utf16.count == 1{
        switch textField{

        case tField1:
            tField2.becomeFirstResponder()

        case tField2:
            tField3.becomeFirstResponder()

        case tField3:
            tField4.becomeFirstResponder()

        case tField4:
            tField1.becomeFirstResponder()

        default: break
        }
    }
}

0 个答案:

没有答案