views:

233

answers:

1

I set maxlength to my textField, when I entered maximum characters in the textField the backspace is not working. Due to that I am unable to change the textFields content. This happens when I test my application on iPhone sdk 3.0 but it works properly in iPhone sdk 2.2.

Here is my code,

- (BOOL)textField:(UITextField *)txtField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (txtField.text.length >= 15 && range.length == 0)
    {
        return NO; 
    }
    else
    {
        return YES;
    }
}

why it happens in iPhone sdk 3.0?

A: 

Off the top of my head, the range.length == 0 would return true for the backspace character, so you're explicitly returning NO when the user taps backspace on a full text field. I'd suggest changing the first condition to read:

if ( txtField.text.length + range.length > 15 )
    return ( NO );

…this way you're testing whether the modified string would be too large to fit (rather than just checking the individual sizes of the existing value and the addition), and you'll not fall afoul of the new paste code in OS 3.0 which could add more than a single character to the text field at a time.

Jim Dovey