0

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

4

2 回答 2

0

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

//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.
}
于 2016-11-30T06:58:50.663 回答
0

尝试关注,

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

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

于 2016-03-11T09:41:49.433 回答