在UIView层次结构中选择自定义视图

时间:2016-03-11 09:36:38

标签: ios cocoa-touch uiview-hierarchy

有一种情况应该选择所有自定义视图(不是系统视图类型,如UILabel或UIButton等),如XXButton或XXView。如何迭代UIView的子视图以找出所有自定义视图?换句话说,如何区分未知类自定义视图和Apple系统视图?

2 个答案:

答案 0 :(得分:0)

尝试关注,

for viw in self.view.subviews
{
if viw.classForCoder == yourCustomViewClass
{
     // do your required operation
}
}
  1. 在上面的案例中,我们首先使用in循环来迭代特定视图的所有子视图。

  2. 然后我们从子视图的数组中检查了类的视图

答案 1 :(得分:0)

当您创建XXButtonXXView时,它们基本上分别从UIButtonUIView继承。因此,您必须仅显式检查您的自定义类。

//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.
}