在iOS中检测退格按

时间:2013-02-20 13:31:57

标签: ios uitextfield

我正在尝试验证在UITextfield中输入的10位数电话号码。其实我需要格式为xxx-xxx-xxxx的数字。所以我不希望用户删除 - 符号。

我尝试使用这里提到的各种方法:Detect backspace in UITextField,但它们似乎都没有用。

我目前的做法是:

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

    if (range.location == 12) {
        UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Invalid Input" message:@"Phone number can contain only 10 digits." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [testTextField resignFirstResponder];
        return NO;
    }

    if (range.length == 0 && [blockedCharacters characterIsMember:[string characterAtIndex:0]]) {
        UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Invalid Input" message:@"Please enter only numbers.\nTry again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        return NO;
    }

    if (range.length == 0 &&
        (range.location == 3 || range.location == 7)) {
        textField.text = [NSString stringWithFormat:@"%@-%@", textField.text, string];
        return NO;
    }

    if (range.length == 1 &&
        (range.location == 4 || range.location == 8))  {
        range.location--;
        range.length = 2;
        textField.text = [textField.text stringByReplacingCharactersInRange:range withString:@""];
        return NO;
    }

    return YES;
}

对此有何想法?

非常感谢。

4 个答案:

答案 0 :(得分:4)

子类UITextField类并覆盖- (void)deleteBackward 然后你会得到键盘上每个退格按键的消息。并且不要忘记在函数开头调用 super

示例:

- (void)deleteBackward
{
    [super deleteBackward];
    [self.delegate deleteTapped:self];

}

答案 1 :(得分:1)

我遇到了类似的问题:

我知道你需要 - 数字内部就像xxx-xxx-xxxx。

这就是我解决它的方法:

-(void)textFieldDidEndEditing:(UITextField *)textField{
     if (self.tf == textField) {
        NSMutableString *stringtf = [NSMutableString stringWithString:self.tf.text];
        [stringtf insertString:@"-" atIndex:2];
        [stringtf insertString:@"-" atIndex:5];
        tf.text = stringDID;
    }
}

因此,一旦用户完成编辑号码,我就为他们添加 - 。

答案 2 :(得分:1)

试一试。我在这里基本上做的是检查空键击键,表示退格。然后我们检查最后两个字符以查看它是否包含' - '如果是这样我们删除它们。然后返回NO,因为我们正在处理自己的退格。我实际上没有运行代码但应该在理论上工作。这意味着像'123-456-7'这样的数字最终会像'123-456'一样。您可以调整逻辑以满足您的需求。欢呼声。

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if([string isEqualToString:@""] && [textField.text length] > 3)
    {
        NSString *lastChars = [textField.text substringFromIndex:[textField.text length] - 2];

        if([lastChars rangeOfString:@"-"].location != NSNotFound)
        {
            NSString *newString = [textField.text substringToIndex:[textField.text length] - 2];
            [textField setText:newString];

            return NO;
        }
    }

    return YES;
}

答案 3 :(得分:0)

此代码可能提供一些线索:

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

if (textField.tag==txtYourTextField.tag) {

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

    if (isBackSpace == -8) {
        NSLog(@"isBackSpace");
        return YES; // is backspace
    }
    else if (textField.text.length == 10) {
        return YES;
    }
}

return NO; }