码迷,mamicode.com
首页 > 其他好文 > 详细

遍历输出所有子视图(View)

时间:2016-08-16 00:05:59      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:

传入一个View,可以获取传入视图的所有子视图,写入桌面,可以在火狐浏览器下查看

/**
 *  程序获得了焦点就会自动调用这个方法(只要程序获得了焦点,所有控件才能接收触摸事件)
 */
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    NSString *xml = [self digView:self.window];
    [xml writeToFile:@"/Users/aplle/Documents/window.xml" atomically:YES];
}


- (NSString *)digView:(UIView *)view
{
    if ([view isKindOfClass:[UITableViewCell class]]) return @"";
    // 1.初始化
    NSMutableString *xml = [NSMutableString string];
    
    // 2.标签开头
    [xml appendFormat:@"<%@ frame=\"%@\"", view.class, NSStringFromCGRect(view.frame)];
    if (!CGPointEqualToPoint(view.bounds.origin, CGPointZero)) {
        [xml appendFormat:@" bounds=\"%@\"", NSStringFromCGRect(view.bounds)];
    }
    
    if ([view isKindOfClass:[UIScrollView class]]) {
        UIScrollView *scroll = (UIScrollView *)view;
        if (!UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, scroll.contentInset)) {
            [xml appendFormat:@" contentInset=\"%@\"", NSStringFromUIEdgeInsets(scroll.contentInset)];
        }
    }
    
    // 3.判断是否要结束
    if (view.subviews.count == 0) {
        [xml appendString:@" />"];
        return xml;
    } else {
        [xml appendString:@">"];
    }
    
    // 4.遍历所有的子控件
    for (UIView *child in view.subviews) {
        NSString *childXml = [self digView:child];
        [xml appendString:childXml];
    }
    
    // 5.标签结尾
    [xml appendFormat:@"</%@>", view.class];
    
    return xml;
}

 

遍历输出所有子视图(View)

标签:

原文地址:http://www.cnblogs.com/GJ-ios/p/5774510.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!