iOS: Find all controls with the same tag

心不动则不痛 提交于 2019-12-11 10:38:58

问题


I have created a storyboard with 12 buttons, 12 smaller buttons and 12 labels.

It is like that:

btnBig1.tag = 1
btnSmall1.tag = 1
lbl1.tag = 1

btnBig2.tag = 2
btnSmall2.tag = 2
lbl2.tag = 2

etc...

Now when a procedure is called

- (IBAction)processButtonUpInside:(id)sender
{
     UIButton *nButton = (UIButton*)sender;
     int nInt =  nButton.tag;
}

... I would like to do something with all 3 controls (big button, small button and label).

It should look like this (pseudo-code):

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

}

As you can see, the CastFromTagID is my "own invention". I don't know how I should actually do this.

Can somebody help? Thank you very much.


回答1:


You can use 3 a different starting point for each button family:

enum {
    kTagFirstBigButton = 1000,
    kTagFirstSmallButton = 2000,
    kTagFirstLabel = 3000,
}

Assign the tags using them:

btnBig1.tag = kTagFirstBigButton + 1;
btnSmall1.tag = kTagFirstSmallButton + 1;
lbl1.tag = kTagFirstLabel + 1;

btnBig2.tag = kTagFirstBigButton + 2;
btnSmall2.tag = kTagFirstSmallButton + 2;
lbl2.tag = kTagFirstLabel + 2;
...

Now it's easy to find anything:

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



回答2:


You shouldn't assign the same tag id to different views.

Indeed, do something like this:

btnBig1.tag = 11 btnSmall1.tag = 12 lbl1.tag = 13;
btnBig2.tag = 21 btnSmall2.tag = 22 lbl2.tag = 23;

Then consider the last digit of the tag 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)
.....

where lastDigit(sender.tag) is a function that returns the last digit of a given integer.




回答3:


In my case where I don't have a reference to the subviews that I want to edit under the same tag I just grab all of the subviews for a given view then loop through all of them and check the tag, if the tag match I execute some code. For example..

#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];
}

Then later on or whenever you need to loop through your views you can do.

NSArray *subviews = [[YourParent subviews] copy];
for (UIView *v in subviews)
{
  if (v.tag == kSameViewTag)
  {
    // Do some code //
  }
}

Now this may become an performance issue if you have a lot of subviews so you can always run it on the background thread then jump into the main thread to do UI updates. For Example:

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


来源:https://stackoverflow.com/questions/13508033/ios-find-all-controls-with-the-same-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!