获取到某控件(视图)其subviews中某个控件的所有对象
参考地址:http://www.glimsoft.com/01/07/how-to-inspect-subviews-hierarchy-of-any-uiview/
下面以获取到视图上所有按钮(使用中可以作为参数)为例:
1.创建UIView 的category
import "UIView+SearchButtonAndTextInSubViews.h"
2.//获取子视图中的所有button、这里数组需要传进来空数组
- (NSArray *)searchButtonInSubViewsWithArray:(NSMutableArray *)array
{
if (!array)
{
array = [[NSMutableArray alloc] init];
}
NSArray *subViews = [self subviews];
for (int i = 0; i < subViews.count; i++)
{
id currentSubView = [subViews objectAtIndex:i];
//是button的话
if ([currentSubView isKindOfClass:[UIButton class]])
{
UIButton *button = (UIButton *)currentSubView;
[array addObject:button];
}
//不是的话去subviews找
if ([currentSubView isKindOfClass:[UIView class]])
{
[currentSubView searchButtonInSubViewsWithArray:array];
}
}
return array;
}