2012-11-22 128 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 // 
      }); 
     } 
    } 
}); 
相关问题