I am building an iOS app which requires user to input pin which consists of 4 digits. So, I created 4 TextField separately in order to accept one number for each TextField. It is working fine when user inputs each textfield and move forward from textfield to another textfield smoothly.
But the problem is that I want user be able to delete by clicking on clear button icon that provided by iOS built-in keyboard. When user clicks on that icon button it should let textfield move to previous textfield but it does not work for me.
I have found lots of resources online on stackoverflow and it is still not working for me. That is why I created my own question.
This is how my code looks likes! I created textfield programmatically!
self.coverView.addSubview(self.paymentView.firstDigit)
self.coverView.addSubview(self.paymentView.secondDigit)
self.coverView.addSubview(self.paymentView.thirdDigit)
self.coverView.addSubview(self.paymentView.fourthDigit)
self.view.addSubview(self.overlayView)
self.overlayView.snp.makeConstraints { (make) in
make.top.width.height.centerX.equalTo(self.coverView)
}
self.paymentView.firstDigit.becomeFirstResponder()
self.paymentView.firstDigit.snp.makeConstraints { (make) in
make.top.equalTo(self.coverView)
make.width.height.equalTo(21)
make.leading.equalTo(self.coverView)
}
self.paymentView.secondDigit.snp.makeConstraints { (make) in
make.top.equalTo(self.coverView)
make.leading.equalTo(self.paymentView.firstDigit.snp.trailing).offset(8)
make.width.height.equalTo(21)
}
self.paymentView.thirdDigit.snp.makeConstraints { (make) in
make.top.equalTo(self.coverView)
make.leading.equalTo(self.paymentView.secondDigit.snp.trailing).offset(8)
make.width.height.equalTo(21)
}
self.paymentView.fourthDigit.snp.makeConstraints { (make) in
make.top.equalTo(self.coverView)
make.leading.equalTo(self.paymentView.thirdDigit.snp.trailing).offset(8)
make.width.height.equalTo(21)
}
self.paymentView.firstDigit.delegate = self
self.paymentView.secondDigit.delegate = self
self.paymentView.thirdDigit.delegate = self
self.paymentView.fourthDigit.delegate = self
The code above included delegate of TextField. And below is how I setup target for each TextField
self.paymentView.firstDigit.addTarget(self, action: #selector(textfieldDidChange(textField:)), for: .editingChanged)
self.paymentView.secondDigit.addTarget(self, action: #selector(textfieldDidChange(textField:)), for: .editingChanged)
self.paymentView.thirdDigit.addTarget(self, action: #selector(textfieldDidChange(textField:)), for: .editingChanged)
self.paymentView.fourthDigit.addTarget(self, action: #selector(textfieldDidChange(textField:)), for: .editingChanged)
And below is the function of textfieldDidChange
@objc func textfieldDidChange(textField: UITextField) {
let text = textField.text
print("This is an amount of text ", text?.count)
if text?.count == 1 {
switch textField {
case self.paymentView.firstDigit:
self.paymentView.firstDigit.textColor = BaseColor.colorPrimary
self.paymentView.firstDigit.backgroundColor = BaseColor.colorPrimary
self.paymentView.secondDigit.becomeFirstResponder()
case self.paymentView.secondDigit:
self.paymentView.secondDigit.textColor = BaseColor.colorPrimary
self.paymentView.thirdDigit.becomeFirstResponder()
self.paymentView.secondDigit.backgroundColor = BaseColor.colorPrimary
case self.paymentView.thirdDigit:
self.paymentView.thirdDigit.textColor = BaseColor.colorPrimary
self.paymentView.fourthDigit.becomeFirstResponder()
self.paymentView.thirdDigit.backgroundColor = BaseColor.colorPrimary
case self.paymentView.fourthDigit:
self.paymentView.fourthDigit.textColor = BaseColor.colorPrimary
self.paymentView.fourthDigit.backgroundColor = BaseColor.colorPrimary
self.paymentView.fourthDigit.resignFirstResponder()
self.view.endEditing(true)
default:
break
}
}
if text?.count == 0 {
switch textField {
case self.paymentView.firstDigit:
self.paymentView.firstDigit.becomeFirstResponder()
self.paymentView.firstDigit.backgroundColor = .red
case self.paymentView.secondDigit:
self.paymentView.firstDigit.becomeFirstResponder()
self.paymentView.firstDigit.backgroundColor = .red
case self.paymentView.thirdDigit:
self.paymentView.secondDigit.becomeFirstResponder()
self.paymentView.secondDigit.backgroundColor = .red
case self.paymentView.fourthDigit:
self.paymentView.thirdDigit.becomeFirstResponder()
self.paymentView.thirdDigit.backgroundColor = .red
default:
break
}
}
}
The above is how I tried to make textfield move to previous textfield when clear button of built-in keyboard iOS is clicked but it did not move. And the code above I copied from the source How to move cursor from one text field to another automatically in swift ios programmatically?