I have implemented a max character limit on a UITextField. The problem is that when input max number for characters, I am unable to backspace the characters. Can anyone tell me what I am doing wrong? Below is my code:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField == pinCode) {
if ([textField.text length]<=3) {
return YES;
}
else {
return NO;
}
}
else {
return YES;
}
return YES;
}
Change the condition to:
if ([textField.text length]<=3 || string.length == 0) {
...
if the content of the string is control character f.e. backspace, the length will be zero
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With