Hi, I'm Tim

I like to dabble in all things related to programming, and occasionally I write about what I’m up to.

Printing the View Controller Hierarchy on iOS

A few days ago I stumbled across an excellent article on obj.io about debugging, titled Debugging: A Case Study. The article walks us through the workflow of fixing a bug that turned out to be an issue in UIKit, and contains a number of useful debugging tricks. There was one trick that I did not previously know about, and which turned out to be immediately useful - printing the view controller hierarchy.

Similar to how it’s possible to print the view hierarchy using the private method -[UIView recursiveDescription], on iOS 8+ it’s also possible to print the view controller hierarchy using another private method: -[UIViewController _printHierarchy]. By calling this method on a window’s root view controller, we can see the entire view controller hierarchy for a window. This is especially useful in the debugger:

(lldb) po [[[UIWindow keyWindow] rootViewController] _printHierarchy]
<UINavigationController 0x7fe295815c00>, state: appeared, view: <UILayoutContainerView 0x7fe29411a860>
   | <TPAInitialViewController 0x7fe294118150>, state: appeared, view: <UIView 0x7fe29411fc90>

If you have any cool and/or useful debugging tricks of your own, let me know on Twitter.