【问题标题】:UITextField jump to previousUITextField 跳转到上一个
【发布时间】:2011-06-06 19:07:37
【问题描述】:

我的视图中有两个 UITextField,让用户输入格式为 1234AA 的邮政编码

  • UITextField 1:用于带数字键盘的数字部分
  • UITextField 2:用于字母(常规键盘)

当用户输入四位数字时,我能够自动跳转到第二个文本字段。

我为此定制了这个方法:

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

现在我的问题是,当用户在文本字段 2 为空时按下删除键时,有没有办法自动从文本字段 2 跳回到文本字段 1。

我无法让它工作,因为当文本字段 2 为空时,不会调用上述方法。

有人知道怎么做吗?

【问题讨论】:

    标签: iphone ios uitextfield


    【解决方案1】:

    使用 textField.tag 来识别您的文本字段。例如,如果您分别为 texFields 1 和 2 分配标签 0 和 1。

    // You could do this in the textFieldDidBeginEditing method //
    
    If (textField.tag == 0)
       {
      //Now the first textField must be active//
      // do the method you had posted above//
        }
    
    If (textField.tag ==1)
                       {
      //Now the second textField must be active//
    
        }
    

    下面的链接提供了检查键盘事件背后的逻辑。或者,您也可以使用 NSNotification 检查键盘事件。

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldChanged:)name:UITextFieldTextDidChangeNotification
            object:searchTextField];
    

    检查 Stack Overflow 上的这个问题,以检测 Empty UITextField 上的 BackSpace Detect backspace in UITextField

    您还可以使用本教程来检测按下的键并检查 BackSpace 并将控制权转移回 textField 1。 http://www.cocos2d-iphone.org/forum/topic/11725

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-04
      • 2021-03-02
      • 1970-01-01
      相关资源
      最近更新 更多