Current view controller from AppDelegate?

aoakenfo picture aoakenfo · Jan 25, 2012 · Viewed 67.6k times · Source

Is there a way to get the current view controller from the AppDelegate? I know there is rootViewController, but that's not what I'm looking for.

Answer

devios1 picture devios1 · Sep 3, 2013

If your app's root view controller is a UINavigationController you can do this:

((UINavigationController*)appDelegate.window.rootViewController).visibleViewController;

Similarly, if it's a UITabBarController you can do this:

((UITabBarController*)appDelegate.window.rootViewController).selectedViewController;

Of course, explicit casting like this is dirty. Better would be to capture the reference yourself using strong types.