MonkeyBonkey
MonkeyBonkey

Reputation: 47911

how to get rootviewcontroller ios storyboard

I have a menu table in the root view of a storyboard that I want reloaded whenever a sub view updates data. How do I get a reference to the root view so that I can call the reloaddata method on it?

Upvotes: 7

Views: 14099

Answers (3)

Alex Zavatone
Alex Zavatone

Reputation: 4323

From the console:

po [[[UIApplication sharedApplication] keyWindow] rootViewController] <UINavigationController: 0x15f076c00>

Upvotes: 0

iphonic
iphonic

Reputation: 12717

You can access it using the below code if the rootViewController is a UIViewController

UIViewController *rootController=(UIViewController *)((AppDelegate *)[[UIApplication sharedApplication] delegate]).window.rootViewController;

But if it's a UINavigationController you can use the code below.

UINavigationController *nav=(UINavigationController *)((AppDelegate *)[[UIApplication sharedApplication] delegate]).window.rootViewController;
UIViewController *rootController=(UIViewController *)[nav.viewControllers objectAtIndex:0];

Upvotes: 17

ingconti
ingconti

Reputation: 11656

Swift:

let rootVC = self.window?.rootViewController

Upvotes: 1

Related Questions