iOS:查找具有相同标记的所有控件

时间:2012-11-22 07:50:49

标签: iphone ios storyboard

我创建了一个包含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是我自己的“发明”。我不知道我应该怎么做到这一点。

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

3 个答案:

答案 0 :(得分:2)

您可以为每个按钮系列使用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
}

答案 1 :(得分:1)

您不应将相同的标记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)是一个返回给定整数的最后一位的函数。

答案 2 :(得分:-1)

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

#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 //
            });
        }
    }
});