How to list out all the subviews in a uiviewcontroller in iOS?

user403015 picture user403015 · Aug 30, 2011 · Viewed 94.8k times · Source

I want to list out all the subviews in a UIViewController. I tried self.view.subviews, but not all of the subviews are listed out, for instance, the subviews in the UITableViewCell are not found. Any idea?

Answer

EmptyStack picture EmptyStack · Aug 30, 2011

You have to recursively iterate the sub views.

- (void)listSubviewsOfView:(UIView *)view {

    // Get the subviews of the view
    NSArray *subviews = [view subviews];

    // Return if there are no subviews
    if ([subviews count] == 0) return; // COUNT CHECK LINE

    for (UIView *subview in subviews) {

        // Do what you want to do with the subview
        NSLog(@"%@", subview);

        // List the subviews of subview
        [self listSubviewsOfView:subview];
    }
}

As commented by @Greg Meletic, you can skip the COUNT CHECK LINE above.