0

I want to iterate through all of the subviews in my view, and do something for each UIButton. This is the code i'm using (in the end of the viewDidLoad):

for(UIView* v in self.view.subviews)
{
    if([v isKindOfClass:[UIButton class]])
    {
        NSLog(@"This is a button");
        //DO SOMETHING FOR EACH BUTTOn
    }
}

But the problem is that the "This is a button" line is never reached...

Why is that?

YogevSitton
  • 10,068
  • 11
  • 62
  • 95

2 Answers2

1
for (UIButton *eachButton in self.view.subviews)
{     
     [eachButton methodWhichYouNeedToAssign];
}

Hope this helps :-)

Charles D'Monte
  • 370
  • 4
  • 16
0

self.view.subviews will return only the subviews of that view.

For eg. View A->View B ->view C

Subviews of View A will list only View B,View C will be only available as the subview of view B

So try some recursive method to drilldown into all the subviews

Lithu T.V
  • 19,955
  • 12
  • 56
  • 101