我有三个文本字段可输入电话号码。我试图将每个文本字段的字符限制设置为三个字符,并且一旦达到此字符切换到新的文本字段。达到字符数限制时切换到下一个UITextField
我在网上看到使用此代码来限制字符:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let currentCharacterCount = textField.text?.characters.count ?? 0
if (range.length + range.location > currentCharacterCount){
return false
}
let newLength = currentCharacterCount + string.characters.count - range.length
return newLength <= 25
}
,并用它来切换到输入一个新的文本框:
.didbecomefirstresponder()
,但我不知道如何将文本字段限制为3个字符,然后切换到下一个字段。