Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 841
MonkeyBonkey Avatar asked Jun 14 '14 15:06

MonkeyBonkey


People also ask

How do I create a root view controller from a storyboard?

First, you register the storyboard with the AppDelegate by passing in the storyboard's name, MainStoryboard. Next, you tell the application to instantiate an initial view controller from the storyboard by calling InstantiateInitialViewController on the storyboard, and you set that view controller as the application's root view controller.

Is there a way to access the root view controller?

But it's easy to do. The root view controller is simply the view controller that sits at the bottom of the navigation stack. You can access the navigation controller's array of view controllers through its viewControllers property. To access the root view controller, we ask for the first item of the array of view controllers.

How do I add a controller to a storyboard in Xcode?

In Xcode, open the Library (via View > Show Library or Shift + Command + L) to show a list of objects that can be added to the Storyboard. Add a Navigation Controller to the Storyboard by dragging the object from the list onto the Storyboard. By default, the Navigation Controller will provide two screens.

How is a storyboard stored in iOS development?

The content of a storyboard is stored as an XML file. At build time, any .storyboard files are compiled into binary files known as nibs. At run time, these nibs are initialized and instantiated to create new views. A Segue, or Segue Object, is used in iOS development to represent a transition between scenes.


1 Answers

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];
like image 123
iphonic Avatar answered Oct 04 '22 11:10

iphonic