user403015 user403015 - 7 months ago 53
Objective-C Question

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

I want to list out all the subviews in a

. I tried
, but not of the subviews are listed out, for instance, the subviews in the
are not found. Any idea?


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.