2016-08-12 132 views
1

我有一个UIViewController在运行时我添加视图到该视图控制器编程。现在我想从任何其他视图控制器打印该视图控制器的视图层次结构。我想知道哪个确切的视图添加。例如,如果我已经添加视图这样如何从任何其他视图控制器获取视图控制器的详细视图层次结构?

UIView *main_view=[UiView alloc]initwithFrame:CGRectMake(20,20,34,80)]; 

然后我想它打印确切名称view.It只打印这一观点&帧的details.Please建议我的地址,我该怎么办呢

+0

什么名字?视图没有名称。 – rmaddy

+0

当你想'确切的名字',你必须'确切'指定你的意思是 – DAXaholic

+0

就像我做了一个UIView main_view的实例,那么它应该打印该实例的名字main_view – Techiee

回答

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]; 
    } 
} 
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"); 
} 
相关问题