Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Current view controller from AppDelegate?

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.

like image 977
aoakenfo Avatar asked Jan 25 '12 20:01

aoakenfo


People also ask

How do I get current ViewController?

You can now call visibleViewController on UIWindow and this will get you the visible view controller by searching down the controller hierarchy. This works if you are using navigation and/or tab bar controller. If you have another type of controller to suggest please let me know and I can add it.

Is Appdelegate a controller?

The application delegate is a controller object. By default, it is the owner and controller of the main window -- which is a view -- in an iOS app.


1 Answers

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.

like image 94
devios1 Avatar answered Oct 11 '22 14:10

devios1