I have 4 UITextFields
in one UIView
like this:
each UITextField
limited to 4 characters. i want implement a auto switching between UITextFields
with count characters of each UITextField
.
i use shouldChangeCharactersIn
for limitation characters:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let maxLength = 4
var newString = String()
let currentString: NSString = textField.text! as NSString
newString = currentString.replacingCharacters(in: range, with: string)
return newString.length <= maxLength
}
and this is my switching implement:
func textFieldDidChange(_ textField: UITextField){
let text = textField.text
if text?.utf16.count == 4 {
switch textField {
case firstPartTextField:
secondPartTextField.becomeFirstResponder()
case secondPartTextField:
thirdPartTextField.becomeFirstResponder()
case thirdPartTextField:
fourthPartTextField.becomeFirstResponder()
default:
break
}
}
}
my problem is switch to previous UITextField
in deleting texts, i can not detect backSpace event when UITextField
is empty.