I have a UITextField
and I have it set up so you can only enter 8 characters into it. The problem is, once the 8th character is entered, the user can not use the delete key. How do I allow the delete key on the keyboard to be used while I have 8 characters entered in my textfield?
Here is my code:
func textField(textField: UITextField!,shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool {
var shouldChange = false
if countString(textField.text) < 8 {
shouldChange = true
}
return shouldChange
}
Note: countString
is a private function that counts the number of characters in a string
What you need is to not check if there are less than 8 chars, but whether or not your existing string plus the input length is larger than 8.
func textField(textField: UITextField!,shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool {
NSUInteger newLength = countString(textField.text)+ countString(string) - range.length;
return !(newLength > 8)
}
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.