【问题标题】:Detect backspace press in iOS在 iOS 中检测退格键
【发布时间】:2013-02-20 13:31:56
【问题描述】:

我正在尝试验证在 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;
}

对此有什么想法吗?

非常感谢。

【问题讨论】:

  • 投票关闭,因为上述问题中的解决方案确实有效。但是,您实际上并不想实现退格,您只想检查文本字段的内容。
  • 你为什么不让用户输入他想要的数字,然后在编辑结束应用你的格式后修改它?
  • 我不得不问:“但它们似乎都不起作用”是什么意思?您的代码的最后一部分看起来很合理。您看到的问题是什么?
  • 是的,apascua 建议的解决方法似乎对我有用。

标签: ios uitextfield


【解决方案1】:

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

例子:

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

}

【讨论】:

    【解决方案2】:

    我遇到了类似的问题:

    我知道你需要 - 里面的数字像 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;
        }
    }
    

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

    【讨论】:

    • 这正是apascua 所说的。非常感谢。但是我最初的问题仍然没有答案。有什么方法可以阻止用户删除 - 符号?
    【解决方案3】:

    试试这个。我基本上在这里做的是检查表示退格的空键单击。然后,我们检查最后两个字符,看它是否包含“-”,如果是,我们将两者都删除。然后返回 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;
    }
    

    【讨论】:

      【解决方案4】:

      这段代码可能会提供一些线索:

      - (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; }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-18
        • 2010-12-30
        • 2018-05-08
        • 2023-03-13
        • 2019-02-07
        • 1970-01-01
        • 2012-03-16
        • 1970-01-01
        相关资源
        最近更新 更多