How to get a certain subview from UIView by tag

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:.


Example with UILabel:

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

good luck!


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