1

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

是这样的:

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

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

ETC...

现在当一个过程被调用时

- (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是我的“自己的发明”。我不知道我应该怎么做。

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

4

3 回答 3

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
}
于 2012-11-22T10:36:01.357 回答
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)是返回给定整数的最后一位的函数。

于 2012-11-22T09:43:21.747 回答
-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 //
            });
        }
    }
});
于 2015-10-14T14:15:43.697 回答