有一种情况应该选择所有自定义视图(不是系统视图类型,如UILabel或UIButton等),如XXButton或XXView。如何迭代UIView的子视图以找出所有自定义视图?换句话说,如何区分未知类自定义视图和Apple系统视图?
答案 0 :(得分:0)
尝试关注,
for viw in self.view.subviews
{
if viw.classForCoder == yourCustomViewClass
{
// do your required operation
}
}
在上面的案例中,我们首先使用in循环来迭代特定视图的所有子视图。
然后我们从子视图的数组中检查了类的视图
答案 1 :(得分:0)
当您创建XXButton
或XXView
时,它们基本上分别从UIButton
和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.
}