jrturton jrturton - 5 months ago 21
iOS Question

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?

Answer

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
Comments