how to get rootviewcontroller ios storyboard

14,001

Solution 1

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];

Solution 2

Swift:

let rootVC = self.window?.rootViewController

Share:
14,001
MonkeyBonkey
Author by

MonkeyBonkey

CTO of Pictorious.com, a mobile app for turning photo sharing into a fun meme-game.

Updated on June 15, 2022

Comments

  • MonkeyBonkey
    MonkeyBonkey almost 2 years

    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?