只要按下键盘上的x按钮,如何从一个文本字段后删除值

时间:2017-12-21 13:19:35

标签: ios swift uitextfield

我有4个文本字段,每个字段接受1个数字用于密码。每次我输入一个数字时,它会自动移动到第二个数字的另一个文本字段,直到它到达第4个数字的最后一个文本字段。这很好用。但是,当我点击数字键盘上的删除(x)按钮,更改数字时,它不会删除数字,除非我在第4个文本字段上然后移动并删除第3个然后是第2个然后是第1个文本字段只要按下删除(x)按钮。

这是代码

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    // On inputing value to textfield

    if ((textField.text?.count)! < 1  && string.count > 0){
        if(textField == pinOne){
            pinTwo.becomeFirstResponder()
        }
        if(textField == pinTwo){
            pinThree.becomeFirstResponder()
        }
        if(textField == pinThree){
            pinFour.becomeFirstResponder()
        }
        textField.text = string
        if let actualText = textField.text {
            let pinEntered: Character = Character(actualText)
            concatString += String(pinEntered)

            if concatString.count == 4 {
                checkPin()
            }
        }

        return false

    } else if ((textField.text?.count)! >= 1  && string.count == 0){


      // On Deleting value from Textfield

        if(textField == pinTwo){
            pinOne.becomeFirstResponder()
        }
        if(textField == pinThree){
            pinTwo.becomeFirstResponder()
        }
        if(textField == pinFour) {
            pinThree.becomeFirstResponder()
        }
        textField.text = ""
        return false

    }

}

请帮忙。谢谢!

1 个答案:

答案 0 :(得分:0)

写下关闭按钮的动作: -     @IBAction func CloseAction(_ sender:Any){

    if !(self.pinFour.text?.isEmpty)! || self.tagValue == 4 {

        self.pinFour.text = ""
        self.pinThree.becomeFirstResponder()

    }else if !(self.pinThree.text?.isEmpty)! || self.tagValue == 3{


        self.pinThree.text = ""
        self.pinTwo.becomeFirstResponder()
    }else if !(self.pinTwo.text?.isEmpty)! || self.tagValue == 2{


        self.pinTwo.text = ""
        self.pinOne.becomeFirstResponder()
    }else if !(self.pinOne.text?.isEmpty)! || self.tagValue == 1{


        self.pinOne.text = ""

    }
    self.tagValue -= 1
}