开发者

Backspace not working when implementing shouldChangeCharactersInRange method - iPhone Dev

Problem... I have a string of allowable characters "0123456789." How do I also allow the backspace from the keyboard... when I implement the code from below... the backspace key no longer works... How can I fix this?

- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)str开发者_开发技巧ing { 

    NSCharacterSet *nonNumberSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];

    return ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0);

}


- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *nonNumberSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];

    if (range.length == 1){
        return YES;
    }else{
        return ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0);
    }


}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([string length] == 0) 
        return YES; 

    NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
    string = [[string componentsSeparatedByCharactersInSet:nonNumberSet] componentsJoinedByString:@""]; 
    textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string]; 

    return NO; 
}

This should work correctly with deleting/cutting multiple characters at once, as well as pasting. Corrections welcome. The only known problem is that when you edit in the middle of the text field the cursor gets sent to the end (because it returns NO) -- I guess you have to use a UITextView to fix that.


NSCharacterSet *theNonNumberSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];


        if (range.length == 1){
            return YES;
        }else if(textField.text.length < ZipcodeTextLength)
        {
            return ([string stringByTrimmingCharactersInSet:theNonNumberSet].length > 0);
        }
        else
            return NO;

This will allow Numbers and Backspace and also you can limit the text length.


This is one of my implementations. Maybe it works for you.

-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

for(UIView *view in _chooseUsernameDialog.subviews) {
    if([view isKindOfClass:[UIButton class]]) {
        int realLength;

        const char * _char = [string cStringUsingEncoding:NSUTF8StringEncoding];
        int isBackSpace = strcmp(_char, "\b");

        if (isBackSpace == -8) {
            // is backspace
            realLength = [textField.text length] - 1 ;
        }
        else
        {
            realLength = [textField.text length] + 1;
        }

        NSLog(@"%d", realLength );

        if(realLength < 4)
        {
            //too short

        }
        else{

            //long enough
        }
    }
}

return !([[textField text] length] + (string.length - range.length) > 13);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜