我创建了一个包含12个按钮,12个小按钮和12个标签的故事板。iOS:查找具有相同标记的所有控件
它就是这样:
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
是我“自己的发明”。我不知道我该怎么做。
有人可以帮忙吗? 非常感谢。
我不能找出如何这可能是工作:假设btnBig1被按下,则偏移量= 1001和[视图viewWithTag:1000 + 1001]被分配给nButtonBig,在一个错误的按钮risulting。 –
@Michele Percich你是对的,它不应该工作,你需要_offWithTag_与_offset_只有 – Saliom
看到我的代码中的评论;我不知道“何时调用某个过程”是指敲击一个大/小按钮,或者只是一个不同的按钮。如果'sender'是一个大/小按钮,你可以通过比较它的标签和不同的第一个标签来轻松地猜出哪一个。一旦你弄清楚了,你可以通过减去相应的第一个标签基数来计算偏移量。 – djromero