Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a recursiveDescription method for the view controller hierarchy?

recursiveDescription is very useful when debugging a hierarchy of views. View controller hierarchies are also very important, is there an equivalent for this?

like image 757
jrturton Avatar asked Jan 07 '13 10:01

jrturton


People also ask

What is a view controller in Xcode?

Overview. You use view controllers to manage your UIKit app's interface. A view controller manages a single root view, which may itself contain any number of subviews. User interactions with that view hierarchy are handled by your view controller, which coordinates with other objects of your app as needed.

What is view and view controller?

Specifically, a view controller manages a view hierarchy and the state information needed to keep those views up-to-date. Every UIKit app relies heavily on view controllers to present content, and you frequently define custom view controllers to manage your views and UI-related logic.

How to see view hierarchy in xcode?

Go back to Xcode and click on the Debug View Hierarchy button in the Debug bar. Alternatively, go to Debug\View Debugging\Capture View Hierarchy.

What is the difference between View and Controller?

The view renders presentation of the model in a particular format. The controller responds to the user input and performs interactions on the data model objects. The controller receives the input, optionally validates it and then passes the input to the model.


3 Answers

To put the answer concisely, I use the command below in Xcode's debugger console to print the view controller hierarchy:

po [[[UIWindow keyWindow] rootViewController] _printHierarchy]

P.S. This works only on ios8 and above and is meant for debugging purposes only.

Link to the article that helped me discover this and many other brilliant debugging techniques is this

Edit 1: In Swift 2 you can print the hierarchy by:

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

Edit 2: In Swift 3 you can print the hierarchy by:

UIApplication.shared.keyWindow?.rootViewController?.value(forKey: "_printHierarchy")
like image 168
jarora Avatar answered Oct 17 '22 06:10

jarora


Update - similar functionality is now available in Apple-supplied form as the _printHierarchy method, so you don't need this category any more.

There is now:

Github: Recursive description category for view controllers.

This adds a recursiveDescription method to UIViewController which prints out the view controller hierarchy. Excellent for checking if you are adding and removing your child view controllers properly.

The code is very simple, included here as well as the GitHub link above:

@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
like image 41
jrturton Avatar answered Oct 17 '22 08:10

jrturton


Fastest method (in lldb/Xcode debugger):

po [UIViewController _printHierarchy]
like image 38
erikprice Avatar answered Oct 17 '22 07:10

erikprice