I have a Verification ViewController
, I get 4 digit verification code by SMS and I need to enter those code to login, I have created the ViewController
Swift 4
when you input a number from the pin code into a text field, you should let a number be shown and then the next text field will become the first responder, so change the first responder after the code was in the text view
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let textField = textField as? PinCodeTextField else { return true }
if string == "" {// when the backward clicked, let it go :)
return true
}
// when textfield is not empty, well, next number
if textField.pinCode.count == textField.maxCount {
becomeFirstResponder(after: textField)
return false
}
if string.count > textField.maxCharacterCount {// the max character count should be 1
return false
}
return true
}
// now the text field has been filled with a number
func textFieldCotentDidChange(_ textField: UITextField) {
print("didchange")
guard let textField = textField as? PinCodeTextField else { return }
if textField.pinCode.count == 0 {
becomeFirstResponder(before: textField)
}
// when textfield has been filled, ok! next!
if textField.pinCode.count == textField.maxCharacterCount {
becomeFirstResponder(after: textField)
}
}
for more details and the simple demo, see this link