我在运行时有一个 UIViewController 我以编程方式将视图添加到该视图控制器。现在我想从任何其他视图控制器打印该视图控制器的视图层次结构。我想知道哪个确切的视图添加。例如,如果我添加了这样的视图
UIView *main_view=[UiView alloc]initwithFrame:CGRectMake(20,20,34,80)];
然后我希望它打印该视图的确切名称。它只打印该视图的地址&框架细节。请告诉我,我该怎么做?
答案 0 :(得分:0)
您必须递归迭代子视图。
- (void)listSubviewsOfView:(UIView *)view {
// Get the subviews of the view
NSArray *subviews = [view subviews];
// Return if there are no subviews
if ([subviews count] == 0) return; // COUNT CHECK LINE
for (UIView *subview in subviews) {
// Do what you want to do with the subview
NSLog(@"%@", subview);
// List the subviews of subview
[self listSubviewsOfView:subview];
}
}
答案 1 :(得分:0)
//You can set unique tag to each subview and then when you fetch view the compare view tag and if it match with the particule tag, that will be the view for which you are looking for.
UIView *main_view=[UiView alloc]initwithFrame:CGRectMake(20,20,34,80)];
main_view.tag = 1000;
[self.view addSubView:main_view];
// fetch view
UIView *view = [self.view viewWithTag:1000];
if ( view.tag == 1000 ){
NSLog("This is view - main_view");
}