如何在iOS中的uiviewcontroller中列出所有子视图? [英] How to list out all the subviews in a uiviewcontroller in iOS?

查看:995
本文介绍了如何在iOS中的uiviewcontroller中列出所有子视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 UIViewController 中列出所有子视图。我尝试了 self.view.subviews ,但并未列出所有子视图,例如 UITableViewCell 找不到。有什么想法?

I want to list out all the subviews in a UIViewController. I tried self.view.subviews, but not all of the subviews are listed out, for instance, the subviews in the UITableViewCell are not found. Any idea?

推荐答案

你必须递归迭代子视图。

You have to recursively iterate the sub views.

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






正如@Greg Meletic评论的那样,你可以跳过上面的COUNT CHECK LINE。


As commented by @Greg Meletic, you can skip the COUNT CHECK LINE above.

这篇关于如何在iOS中的uiviewcontroller中列出所有子视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆