Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot delete text in UITextField

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;

}

like image 503
Hassan Zaheer Avatar asked May 17 '26 16:05

Hassan Zaheer


1 Answers

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

like image 117
Daniel Alexandrov Avatar answered May 21 '26 01:05

Daniel Alexandrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!