How to get a certain subview from UIView by tag

57,373

Solution 1

You can get your subviews with for loop iteration

for (UIView *i in self.view.subviews){
      if([i isKindOfClass:[UILabel class]]){
            UILabel *newLbl = (UILabel *)i;
            if(newLbl.tag == 1){
                /// Write your code
            }
      }
}

Swift

let label:UILabel = self.view.viewWithTag(1) as! UILabel

Solution 2

Example with UILabel:

UILabel *label = (UILabel *)[self.view viewWithTag:1];

good luck!

Solution 3

You can get the subview with the code which others have mentioned, just like

UILabel *tagLabel = (UILabel*)[view viewWithTag:1];

But an important point to remember,

  • Make sure the parent view doesn't have the same tag value as of the subview. Otherwise the viewWithTag: method will return the receiver view (on which you are calling viewWithTag:) instead of returning the actual subview you want.

So keep the parent view and child views tags distinct whenever you need to use viewWithTag:.

Solution 4

Swift 3, Swift 4 and Swift 5

if let subLabel: UILabel = primaryView.viewWithTag(123) as? UILabel {
    subLabel.text = "do things here :-D"
}

Solution 5

You could use the viewWithTag: method.

Share:
57,373
user3797431
Author by

user3797431

Updated on May 04, 2021

Comments

  • user3797431
    user3797431 about 3 years

    I'm a noob in Objective-C and I have one question.

    I have one UILabel object that I adding to one UIView with this code:

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,10,self.view.frame.size.width-15-70, 30)];
    label.tag = 1;
    label.font = [PublicObject fontTexts:17];
    label.textAlignment = NSTextAlignmentRight;
    label.textColor = [UIColor whiteColor];
    [cell setBackgroundColor:[UIColor clearColor]];
    
     UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
     view.backgroundColor = [PublicObject colorWithHexString:@"cd4110"];
     label.text = [filterData objectAtIndex:indexPath.row];
     view addSubview:label];
    

    Now I want get one subview in my view where this subview has tag = 1 and save it on another object like this:

    UILabel *tagLabel;
    tagLabel = // I want get one subview in view where tag = 1 
    

    Please help me understand how to do this.

  • user3797431
    user3797431 almost 10 years
    my friend I want get UILabel with tag!!! if were many label in my view I can't use this ... please guide me with tag for any label
  • Hiren
    Hiren almost 10 years
    Set the unique tag values to all views and access the object using tag.
  • AhabLives
    AhabLives almost 9 years
    MAKE SURE YOUR PARENT VIEW DOES NOT HAVE THE SAME TAG AS YOUR PARENT VIEW !!! I was on the verge of losing my mind. Thank You. Your comment saved my life.
  • saraman
    saraman over 7 years
    Fastest code is not taht but it is that: UILabel tagLabel = (UILabel)[view viewWithTag:1];
  • Baxissimo
    Baxissimo over 6 years
    [view viewWithTag:x] also searches hierarchically, not just the immediate children. If you want to avoid searching subviews of subviews this might be a good answer, but that's kind of a niche use case. Or if you want to avoid finding matching tags that aren't UILabel at all cost, then this manual approach might also be called for.
  • Christian Ayscue
    Christian Ayscue over 4 years
    Use [UIView viewWithTag:]