Skip to content

Instantly share code, notes, and snippets.

@kmdarshan
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmdarshan/02b0b662eb2e1012777d to your computer and use it in GitHub Desktop.
Save kmdarshan/02b0b662eb2e1012777d to your computer and use it in GitHub Desktop.
Recursively printing all subviews within a view given a tag
- (void)printSubViews:(UIView *)view withTag:(NSInteger)tag
{
if (view == nil) {
return;
}
if ( [view tag] == tag) {
NSLog(@"%@", view);
return;
}
for (UIView *subview in [view subviews])
{
[self printSubViews:subview withTag:tag];
}
}
-(void) findAllViews {
UIView *baseView = [UIView new];
[baseView setTag:1];
UIView *secondRowA = [UIView new];
[secondRowA setTag:2];
UIView *secondRowB = [UIView new];
[secondRowB setTag:3];
UIView *thirdRowA = [UIView new];
[thirdRowA setTag:4];
UIView *thirdRowB = [UIView new];
[thirdRowB setTag:5];
[secondRowA addSubview:thirdRowA];
[secondRowB addSubview:thirdRowB];
[baseView addSubview:secondRowA];
[baseView addSubview:secondRowB];
[self printSubViews:baseView withTag:6];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment