0

There is a situation that should select all custom view(not system view type such as UILabel or UIButton etc.) like XXButton or XXView. How can I iterate a UIView's subviews to figure out all the custom views? In other words, how to distinguish between unknown class custom views and Apple system views?

musixlemon
  • 61
  • 2
  • 5
  • where did these custom views come from and why do you need them? interrogating the view hierarchy is probably not the correct solution... – Wain Mar 11 '16 at 09:38
  • @Wain customviews maybe from a bundle by third-party or whatever – musixlemon Mar 11 '16 at 10:10
  • then you have a problem because you don't know how to tell the difference between Apple public, Apple private, random 3rd party and your own view classes... – Wain Mar 11 '16 at 10:13
  • `-isMemberOfClass:` method might help on you. – holex Mar 11 '16 at 11:15

2 Answers2

0

Try Following,

for viw in self.view.subviews
{
if viw.classForCoder == yourCustomViewClass
{
     // do your required operation
}
}
  1. In above case first we have used for in loop to iterate on all sub views of particular view.

  2. Then we have checked the class for the view from the subview's array

Harshal Bhavsar
  • 1,598
  • 15
  • 35
0

When you created a XXButton or XXView, they basically inherited from the UIButton and UIView respectively. Thus you have to explicitly checks for your custom class only.

//Loop through all the views in your superview.
for(UIView *anyView in self.view.subviews) {

    if([anyView isKindOfClass:[XXButton class]]) {

        // It's a XXButton. Need to cast it.
        XXButton *btn = (XXButton *)anyView;

    } else if([anyView isKindOfClass:[XXView class]]) {

        // It's a XXView. Need to cast it.
        XXView *view = (XXView *)anyView;

    }

    // You can multiple else if conditions for your custom UI classes.
}
Hemang
  • 26,840
  • 19
  • 119
  • 186