我有 4 个文本字段,每个字段接受 1 位密码。每次我输入一个数字时,它都会自动移动到第二个数字的另一个文本字段,直到它到达第四个数字的最后一个文本字段。这工作正常。但是,当我单击数字键盘上的删除 (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
}
}
请帮忙。谢谢!
Best Answer-推荐答案 strong>
编写关闭按钮的这个 Action :-
@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
}
关于ios - 只要按下键盘上的 x 按钮,如何从一个文本字段中删除值,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/47925891/
|