Need help understanding UIViewController and UIWindow initialization

1k views Asked by At

I was wondering if anyone could explain the following block of code because I don't really understand it.

self.viewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
self.navigationController = [[ UINavigationController alloc ] initWithRootViewController:self.viewController ];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];

Then when you want to present a new vc you can do this:

OtherViewController *ovc = [[ OtherViewController alloc ] initWithNibName:@"OtherViewController" bundle:nil ];
[ self.navigationController pushViewController:ovc animated:YES ];

To go back do this:

[ self.navigationController popViewControllerAnimated:YES ];
2

There are 2 answers

0
Duncan C On BEST ANSWER

A navigation controller needs a "root" view controller, which is the bottom view controller in the stack of view controllers it manages.

#1 self.viewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
#2 self.navigationController = [[ UINavigationController alloc ] initWithRootViewController:self.viewController ];
#3 self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
#4 [self.window makeKeyAndVisible];

Line 1 creates a view controller of class "RootViewController" (which must be a custom view controller class.) It loads the view controller's views from a nibfile of the same name. This is similar to using instantiateViewControllerWithIdentifier to load a view controller from a storyboard, except that you have to specify the class of view controller you're creating, and the nibfile you're loading the

Line 2 creates a navigation controller with the newly created "RootViewController" as it's root view controller

Line 3 installs the navigation controller as the root view controller of the application window.

Line 4 makes the app window the active window.

0
Syed Qamar Abbas On

Explanation in simple words. Every iOS Application has at least 1 UIWindow which always needs a UIViewController object to set as a root which means to set as a initial ViewController of the application that will be visible to the user on screen. while UINavigationController is stack container which push ViewControllers inside it and the top ViewController in this stall will only be visible to screen by default. But initially it needs a UIViewController to set as a root view controller in both UIWindow and UINavigationController's root view controller they need a starting point. Both works differently like UIWindow root view controller can be change at any time but UINavigationController doesn't allow us to change root view controller.

Now in your code let me explain you what is going on.

self.viewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
//In Above Line you are loading a UIViewController from a Xib file name RootViewController.xib into viewController property
self.navigationController = [[ UINavigationController alloc ] initWithRootViewController:self.viewController ];
//In Above Line You are allocating a new navigation controller programatically with a root/initial view controller and you are passing your previously loaded view controller to be set as root view controller of this navigation.
self.window.rootViewController = self.navigationController;
//In Above Line You are assigning your navigationController to UIWindow object this means you want your view controllers to be managed in a stack so that if you push a view controller you can snap back easily with a single line of code.
[ self.navigationController popViewControllerAnimated:YES ];
//In This Line you are removing your Top view Controller from a navigation stack Like the Back button does in Setting>General to Setting in iPhone/iPad