iOS:find所有具有相同标签的控件

我创造了12个button,12个小button和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个控件(大button,小button和标签)做一些事情。

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

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

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

您可以为每个button系列使用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; ... 

现在很容易find任何东西:

 - (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 } 

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

在我的情况下,我没有参考我想要在同一个标​​签下编辑的子视图,我只是抓住给定视图的所有子视图,然后遍历所有的子视图,然后检查标签,如果标签匹配I执行一些代码。 例如..

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