【问题标题】:iOS: Find all controls with the same tagiOS:查找具有相同标签的所有控件
【发布时间】:2012-11-22 07:50:48
【问题描述】:

我创建了一个包含 12 个按钮、12 个较小按钮和 12 个标签的故事板。

是这样的:

btnBig1.tag = 1
btnSmall1.tag = 1
lbl1.tag = 1

btnBig2.tag = 2
btnSmall2.tag = 2
lbl2.tag = 2

等等……

现在当一个过程被调用时

- (IBAction)processButtonUpInside:(id)sender
{
     UIButton *nButton = (UIButton*)sender;
     int nInt =  nButton.tag;
}

...我想对所有 3 个控件(大按钮、小按钮和标签)进行操作。

它应该看起来像这样(伪代码):

- (IBAction)processButtonUpInside:(id)sender
{
     UIButton *nButton = (UIButton*)sender;
     int nInt =  nButton.tag;

     UIButton *nButtonBig (UIButton*)CastFromTagID(nInt)
     //do something with the big button

     UIButton *nButtonSmall (UIButton*)CastFromTagID(nInt)
     //do something with the small button

     UILabel *nLabel (UILabel*)CastFromTagID(nInt)
     //do something with the label

}

如您所见,CastFromTagID 是我的“自己的发明”。我不知道我应该怎么做。

有人可以帮忙吗? 非常感谢。

【问题讨论】:

    标签: iphone ios storyboard


    【解决方案1】:

    您可以为每个按钮系列使用 3 个不同的起点:

    enum {
        kTagFirstBigButton = 1000,
        kTagFirstSmallButton = 2000,
        kTagFirstLabel = 3000,
    }
    

    使用它们分配标签:

    btnBig1.tag = kTagFirstBigButton + 1;
    btnSmall1.tag = kTagFirstSmallButton + 1;
    lbl1.tag = kTagFirstLabel + 1;
    
    btnBig2.tag = kTagFirstBigButton + 2;
    btnSmall2.tag = kTagFirstSmallButton + 2;
    lbl2.tag = kTagFirstLabel + 2;
    ...
    

    现在很容易找到任何东西:

    - (IBAction)processButtonUpInside:(id)sender
    {
         UIButton *nButton = (UIButton*)sender;
         /* I'm not sure what button is `sender` here
            If it's a big or small one you can guess 
            comparing its tag with the first tag 
         */
         int offset =  nButton.tag;
    
         UIButton *nButtonBig = (UIButton*)[view viewWithTag:kTagFirstBigButton + offset];
         //do something with the big button
    
         UIButton *nButtonSmall = (UIButton*)[view viewWithTag:kTagFirstSmallButton + offset];
         //do something with the small button
    
         UILabel *nLabel = (UILabel*)[view viewWithTag:kTagFirstLabel + offset];
         //do something with the label
    }
    

    【讨论】:

    • 我不知道这是如何工作的:假设按下 btnBig1,然后将 offset = 1001 和 [view viewWithTag:1000 + 1001] 分配给 nButtonBig,导致按钮错误。跨度>
    • @Michele Percich 你是对的,它不应该工作,你只需要获得 offsetviewWithTag
    • 查看我在代码中的注释;我不知道“何时调用程序”是否意味着点击了一个大/小按钮或只是一个不同的按钮。如果sender 是大/小按钮之一,您可以通过将其标签与不同的第一标签进行比较来轻松猜出哪个。一旦弄清楚了,就可以通过减去其对应的第一个标签基数来计算偏移量。
    【解决方案2】:

    您不应将相同的标签 ID 分配给不同的视图。

    确实,做这样的事情:

    btnBig1.tag = 11 btnSmall1.tag = 12 lbl1.tag = 13;
    btnBig2.tag = 21 btnSmall2.tag = 22 lbl2.tag = 23;
    

    然后考虑标签id的最后一位:

    UIView *nView = (UIView *)sender;
    if (lastDigit(sender.tag) == 3)
    // this is a label 
    {
        UIButton *nButtonBig = [nView.superview viewWithTag:nInt-2];
        UIButton *nButtonSmall = [nView.superview viewWithTag:nInt-1];
        UILabel *nLabel = (UILabel *)sender;
    }
    else if (lastDigit(sender.tag) == 2)
    .....
    

    其中 lastDigit(sender.tag) 是一个返回给定整数的最后一位数字的函数。

    【讨论】:

      【解决方案3】:

      在我没有引用要在同一标签下编辑的子视图的情况下,我只需获取给定视图的所有子视图,然后遍历所有子视图并检查标签,如果标签匹配我执行一些代码。比如……

      #define kSameViewTag 9500001
      
      for (int i = 0; i < 10; i++) // add a bunch of same tag views // 
      {
        UIView *someview = [UIView new];
        someview.tag = kSameViewTag;
        [YourParent addSubview:someview];
      }
      

      然后在以后或当您需要循环浏览您的视图时,您可以这样做。

      NSArray *subviews = [[YourParent subviews] copy];
      for (UIView *v in subviews)
      {
        if (v.tag == kSameViewTag)
        {
          // Do some code //
        }
      }
      

      现在,如果您有很多子视图,这可能会成为性能问题,因此您始终可以在后台线程上运行它,然后跳转到主线程进行 UI 更新。例如:

      NSArray *subviews = [[YourParent subviews] copy];
      
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
          for (UIView *v in subviews)
          {
              if (v.tag == kSameViewTag)
              {
                  dispatch_async(dispatch_get_main_queue(), ^{
                      // Do Some UI Stuff Here //
                  });
              }
          }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-17
        • 2021-07-10
        • 2010-10-07
        • 1970-01-01
        • 2013-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多