2013-01-07 58 views

回答

17

更新 - 类似的功能现在可在Apple提供的表格中作为_printHierarchy方法使用,因此您不再需要此类别。

现在有:

Github: Recursive description category for view controllers

这增加了一个recursiveDescription方法UIViewController打印出视图控制器层次结构。非常适合检查您是否正确添加和移除您的子视图控制器。

代码很简单,这里包括以及GitHub的链接上面:

@implementation UIViewController (RecursiveDescription) 

-(NSString*)recursiveDescription 
{ 
    NSMutableString *description = [NSMutableString stringWithFormat:@"\n"]; 
    [self addDescriptionToString:description indentLevel:0]; 
    return description; 
} 

-(void)addDescriptionToString:(NSMutableString*)string indentLevel:(NSInteger)indentLevel 
{ 
    NSString *padding = [@"" stringByPaddingToLength:indentLevel withString:@" " startingAtIndex:0]; 
    [string appendString:padding]; 
    [string appendFormat:@"%@, %@",[self debugDescription],NSStringFromCGRect(self.view.frame)]; 

    for (UIViewController *childController in self.childViewControllers) 
    { 
     [string appendFormat:@"\n%@>",padding]; 
     [childController addDescriptionToString:string indentLevel:indentLevel + 1]; 
    } 
} 

@end 
+1

+1.5发挥得好,先生。 – Caleb

23

简明把答案,我用命令低于Xcode的调试器控制台打印视图控制器层次:

po [[[UIWindow keyWindow] rootViewController] _printHierarchy] 

PS这仅适用于ios8及更高版本,仅用于调试目的。

的文章链接,帮助我发现这和其他许多辉煌的调试技术是this

编辑1: 在斯威夫特2,你可以通过打印层次:

UIApplication.sharedApplication().keyWindow?.rootViewController?.valueForKey("_‌​printHierarchy") 

编辑2: 在Swift 3中,您可以通过以下方式打印层次结构:

UIApplication.shared.keyWindow?.rootViewController?.value(forKey: "_printHierarchy") 
+0

在Swift中:'UIApplication.sharedApplication().keyWindow?.rootViewController?.valueForKey(“_ printHierarchy”)' –

+0

谢谢。我会将其添加到答案中。 – jarora

5

最快方法(在LLDB/Xcode调试):

po [UIViewController _printHierarchy] 
0

_printHierarchy不提供用于VC的视图的子视图分量递归信息。

方法1:使用lldb命令获取完整的视图层次结构。

po [[[UIApplication sharedApplication] keyWindow] recursiveDescription] 

方法二:利用Xcode调试“调试视图层次”按钮最好的办法让所有的信息。

enter image description here

+0

嗯,这很好,但问题明确提到recursiveDescription,我正在寻找一个等效的视图控制器层次结构。 – jrturton

相关问题