I'm trying to find a way that when the user reach to the maximum characters the text field move to next.
Here is my code:
extension ViewController: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let text = textField.text else {
return true
}
let length = text.count + string.count - range.length
let max = 2
if textField == weTextField {
return length <= max
}
else if textField == theyTextField {
return length <= max
}
return true
}
}
Thanks
You can achieve that by doing something like this:
create a function that will change your first responder when a character limit is reached
@objc func valueDidChange(_ textField: UITextField) { guard let text = textField.text else { return } let maxLength = 2 switch textField { case textField1 where text.count == maxLength: textField2.becomeFirstResponder() case textField2 where text.count == maxLength: textField3.becomeFirstResponder() default: break } }
Add that function to your textfield so it's triggered when the textfield is edited. In the above case, I added it to both textField1 and textField2
textField1.addTarget(self, action: #selector(valueDidChange), for: .editingChanged) textField2.addTarget(self, action: #selector(valueDidChange), for: .editingChanged)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.