当前位置: 代码迷 >> Iphone >> iPhone How-to:怎么递归dump UIView
  详细解决方案

iPhone How-to:怎么递归dump UIView

热度:279   发布时间:2016-04-25 06:36:13.0
iPhone How-to:如何递归dump UIView
在学习和调试iPhone 界面或者某个控件的过程中,递归地dump出UIView是一个了解界面结构很有效的方法。这样不仅可以更好地发现问题,还可以定制某些系统的标准控件。要解决这类问题,很自然会想到递归函数。实现如下:
void dumpViews(UIView* view, NSString *label, NSString *indent)  {
    Class klass = [view class];
    NSString *classDescription = getFullClassDescription(klass);   
    if ([label compare:@""] == NSOrderedSame)
        NSLog(@"%@ %@", classDescription, NSStringFromCGRect(view.frame));
    else
        NSLog(@"%@ %@ %@", label, classDescription, NSStringFromCGRect(view.frame));
    for (NSUInteger i = 0; i < [view.subviews count]; i++)
    {
        UIView *subView = [view.subviews objectAtIndex:i];
        NSString *newIndent = [[NSString alloc] initWithFormat:@"  %@", indent];
        NSString *newLabel = [[NSString alloc] initWithFormat:@"%@%d:", newIndent, i];
        dumpViews(subView, newLabel, newIndent);
        [newLabel release];
        [newIndent release];
    }
}

其中getFullClassDescription是用来获取某个类的继承关系描述:
NSString* getFullClassDescription(Class klass) {
    NSString *description = [klass description];
    while ([klass superclass])
    {
        klass = [klass superclass];
        description = [description stringByAppendingFormat:@":%@", [klass description]];
    }
    return description;
}

利用dumpViews对某应用的主窗体进行“偷窥”后输出结果如下:
UIWindow:UIView:UIResponder:NSObject {{0, 0}, {320, 480}}
  0: UILayoutContainerView:UIView:UIResponder:NSObject {{0, 0}, {320, 480}}
    0: UINavigationTransitionView:UIView:UIResponder:NSObject {{0, 0}, {320, 480}}
    1: UINavigationBar:UIView:UIResponder:NSObject {{0, 0}, {320, 44}}
       0: UINavigationItemView:UIView:UIResponder:NSObject {{160, 8}, {0, 27}}
1 楼 hotfm 2011-10-24  
好东西,!0.0
  相关解决方案