2013-01-07 1 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

이 간결하게 대답을 넣어, 내가보기 컨트롤러 계층 구조를 인쇄 엑스 코드의 디버거 콘솔에서 아래의 명령을 사용

po [[[UIWindow keyWindow] rootViewController] _printHierarchy] 

추신 이것은 ios8 이상에서만 작동하며 디버깅 목적으로 만 사용됩니다. 나에게 이것과 다른 많은 훌륭한 디버깅 기술을 발견 도움이 기사에

링크는 this

편집 1 : 당신에 의해 계층 구조를 인쇄 할 수 있습니다 스위프트 2에서 :

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

편집 2 : 스위프트 3에서는 다음과 같이 계층을 인쇄 할 수 있습니다.

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

Swift :'UIApplication.sharedApplication(). keyWindow? .rootViewController? .valueForKey ("_ printHierarchy")' –

+0

고마워요. 나는 그것을 답에 추가 할 것이다. – jarora

5

빠른 방법 (lldb/엑스 코드 디버거)

po [UIViewController _printHierarchy] 
0

_printHierarchy은 VC의 관점 서브 뷰 컴포넌트에 대한 재귀 정보를 제공하지 않는다.

접근법 1 : lldb 명령을 사용하여 전체보기 계층을 얻습니다.

po [[[UIApplication sharedApplication] keyWindow] recursiveDescription] 

접근법 2 : 가장 좋은 방법은 엑스 코드 디버거에서 "디버그보기 계층 구조"버튼을 사용하여 모든 정보를 얻을 수있다.

enter image description here

+0

Erm, 멋지 네요. 특히 질문에 recursiveDescription이 언급되어 있고 View Controller 계층 구조에 해당하는 것이 필요합니다. – jrturton

관련 문제