返回顶部

收藏

查询子视图

更多

[view subViews]方法只能返回给定视图的直接子视图。而视图的每个子视图又能包含自己的子视图。这份代码提供几个函数,能查询给定视图的所有直接和间接子视图。

// Return an exhaustive descent of the view's subviews 
NSArray *allSubviews(UIView *aView)
{
    NSArray *results = [aView subviews];
    for (UIView *eachView in [aView subviews]) {
        NSArray *riz = allSubviews(eachView);
        if (riz) results = [results arrayByAddingObjectsFromArray:riz]; 
    }
    return results; 
}

// Return all views throughout the application 
NSArray *allApplicationViews()
{
    NSArray *results = [[UIApplication sharedApplication] windows]; 
    for (UIWindow *window in [[UIApplication sharedApplication]windows]) {
        NSArray *riz = allSubviews(window);
        if (riz) results = [results arrayByAddingObjectsFromArray:riz];
    }
    return results; 
}

// Return an array of parent views from the window down to the view 
NSArray *pathToView(UIView *aView)
{
    NSMutableArray *array = [NSMutableArray arrayWithObject:aView]; 
    UIView *view = aView;
    UIWindow *window = aView.window;
    while (view != window)
    {
        view = [view superview];
        [array insertObject:view atIndex:0];
    }
    return array; 
}
//该片段来自于http://outofmemory.cn

标签:objective-c,手机开发

收藏

0人收藏

支持

0

反对

0

»更多 您可能感兴趣的代码
  1. 2014-08-21 21:07:19iOS5和iOS6横竖屏同时支持 by panzaitang
  2. 2014-10-25 10:28:48禁止某几个文件用ARC模式编译 by lihuibng
  3. 2014-03-09 13:55:34IOS从背景图中取色 by 凤歌
  4. 2014-04-20 13:32:43视图翻转切换效果 by 牛哥
  5. 2014-06-15 10:54:58IOS画圆 by ccyuan
  6. 2014-11-16 09:54:02利用UIView绘制矩形 by Foyon
  7. 2014-04-20 13:38:10在App中发送SMS短信 by 云香水识
  8. 2014-05-09 10:31:08app桌面图标显示数字 by 一个搞IT的人
  9. 2014-11-21 09:13:33在桌面图标右上角显示数字 by 落叶随风
  10. 2014-03-15 19:28:10如果“documents_theme”路径不存在,则创建 by Eric_Lee
  11. 2014-04-20 14:16:35iOS本地推送 by Sycro

发表评论