我創建了一個包含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