2012-11-22 116 views
1

我創建了一個包含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是我「自己的發明」。我不知道我該怎麼做。

有人可以幫忙嗎? 非常感謝。

回答

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 
} 
+0

我不能找出如何這可能是工作:假設btnBig1被按下,則偏移量= 1001和[視圖viewWithTag:1000 + 1001]被分配給nButtonBig,在一個錯誤的按鈕risulting。 –

+0

@Michele Percich你是對的,它不應該工作,你需要_offWithTag_與_offset_只有 – Saliom

+0

看到我的代碼中的評論;我不知道「何時調用某個過程」是指敲擊一個大/小按鈕,或者只是一個不同的按鈕。如果'sender'是一個大/小按鈕,你可以通過比較它的標籤和不同的第一個標籤來輕鬆地猜出哪一個。一旦你弄清楚了,你可以通過減去相應的第一個標籤基數來計算偏移量。 – djromero

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)是返回一個函數給定整數的最後一位數字。

-1

在我的情況下,我沒有參考我想在相同標籤下編輯的子視圖,我只是抓住了所有的t他爲給定的視圖子視圖然後循環遍歷所有視圖並檢查標記,如果標記匹配,我執行一些代碼。例如..

#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 // 
      }); 
     } 
    } 
}); 
相關問題