iphone 使用 Tag 检查 UITextField 是否存在

发布于 2024-12-16 10:35:04 字数 646 浏览 0 评论 0原文

有没有办法使用 Tag 属性来检测 UITextField 是否存在?本质上,我有许多动态创建的文本字段,我想使用键盘上的返回键来浏览这些字段。

我正在尝试使用另一篇文章中的以下代码,但“textField.superview”返回 null。我正在以编程方式创建文本字段。

-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
  NSInteger nextTag = textField.tag + 1;
  // Try to find next responder
  UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
  if (nextResponder) {
    // Found next responder, so set it.
    [nextResponder becomeFirstResponder];
  } else {
    // Not found, so remove keyboard.
     [textField resignFirstResponder];
  }
  return NO; // We do not want UITextField to insert line-breaks.
}

Is there a way to detect if a UITextField exists using the Tag property? Essentially i have a number of textfields created dynamically and I want to tab through the fields using the return key on the keypad.

I am trying to use the below code form another post but 'textField.superview' returns null. I am creating the textfields programatically.

-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
  NSInteger nextTag = textField.tag + 1;
  // Try to find next responder
  UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
  if (nextResponder) {
    // Found next responder, so set it.
    [nextResponder becomeFirstResponder];
  } else {
    // Not found, so remove keyboard.
     [textField resignFirstResponder];
  }
  return NO; // We do not want UITextField to insert line-breaks.
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雪落纷纷 2024-12-23 10:35:04

您可以直接像这样检查

       -(BOOL)textFieldShouldReturn:(UITextField*)textField {
             if (textField.tag == 1) {
             //Do the stuff what you want here
             }
        }

设置标签

        textFieldName.tag = 1;

You can directly check like this

       -(BOOL)textFieldShouldReturn:(UITextField*)textField {
             if (textField.tag == 1) {
             //Do the stuff what you want here
             }
        }

For setting the tag

        textFieldName.tag = 1;
沉默的熊 2024-12-23 10:35:04

您可以使用 isKindofClass: 方法。您可以这样做

 if ([[textField.superview viewWithTag:nextTag] isKindOfClass:[UITextField class]])
  {
    // Found next responder, so set it.
    [(UITextField *)[textField.superview viewWithTag:nextTag] becomeFirstResponder];
  } else {
    // Not found, so remove keyboard.
     [textField resignFirstResponder];
  }
  return NO; // We do not want UITextField to insert line-breaks.
}

You can use isKindofClass: method.You can do like this

 if ([[textField.superview viewWithTag:nextTag] isKindOfClass:[UITextField class]])
  {
    // Found next responder, so set it.
    [(UITextField *)[textField.superview viewWithTag:nextTag] becomeFirstResponder];
  } else {
    // Not found, so remove keyboard.
     [textField resignFirstResponder];
  }
  return NO; // We do not want UITextField to insert line-breaks.
}
浅黛梨妆こ 2024-12-23 10:35:04

您是否设置了文本字段的标签?标签字段不会自动设置,您必须在创建文本字段时自行设置,以便稍后识别。如果你不设置它,它们都默认为 0 - 所以你永远不会找到任何带有文本 > 的字段。 0。

Did you set the Tag of the text fields? The tag field is not set automatically, you have to set it yourself when you create the text field so you can identify it later. If you don't set it, they all default to 0 - so you will never find any field with a text > 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文