【问题标题】:How to get view hierarchy with detail of a view controller from any other view controller?如何从任何其他视图控制器获取具有视图控制器详细信息的视图层次结构?
【发布时间】:2016-08-12 04:40:47
【问题描述】:

我有一个 UIViewController 在运行时我以编程方式将视图添加到该视图控制器中。现在我想从任何其他视图控制器打印该视图控制器的视图层次结构。我想知道哪个确切的视图已添加。例如,如果我添加了这样的视图

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

然后我希望它打印该视图的确切名称。它只打印该视图的地址和框架详细信息。请建议我该怎么做?

【问题讨论】:

  • 什么名字?视图没有名称。
  • 当你想要一个“确切的名字”时,你必须“准确地”指定你的意思
  • 就像我创建了一个 UIView main_view 的实例,那么它应该打印该实例名称 main_view
  • main_view 是当前分配的内存的指针,所以你不能得到那个东西
  • 为什么需要该名称,您可以将标签设置为您的视图并从层次结构中从标签中获取该视图并比较 if (self.main_view == foundView)

标签: ios objective-c uiview


【解决方案1】:

你必须递归地迭代子视图。

- (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];
    }
}

【讨论】:

    【解决方案2】:
    //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");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多