Through this question I would like to know if I understand well the notion of Root View Controller.
In iOS application, the Root View Controller (RVC) is the controller whose view gets added to the UIWindow application at startup, isn't true?
[window addSubview:rvcController.View];
[window makeKeyAndVisible];
Now, an UIWindow has also a rootViewController property. When running the previous snippet of code, does that property gets populated with the rvcController or do I have to set it explicitly?
Then, in a UINavigationController it is possible to set a RVC that is different from the previous RVC set for the entry point.
In this case, the first time I add a controller to the navigationController stack (pushing a new controller on it), does the framework set that controller as the RVC for the navigationController or do I have to set it explicitly through initWithRootViewController
method?
firstly you can create A empty project in Xcode. after you add the new file on objectivec class view controller with xiv. now you can add to this code in appdeligate.m and set the rootviewcontroller in appdeligate
NOTE:- ViewController.h import to the appdeligate.m
}
Ya.. when I began iPhone dev.. the rootViewController thing threw me for a loop too. But it’s really straight forward.
when the app starts, I create a UIWindow object in my app delegate class. Also, in that class, I have a property of type UIWindow called window;
I then create a
UIViewController
whoseview
will be the first view in the window hierarchy, this could be called the "root view controller".The confusing part is...that often we create a
UINavigationController
as the "root view controller" and that navigation controller has an init method that asks for a "RootViewController", which is the first viewcontroller it will place on its stack.So, the window gets a "root view controller", which is the
UINavigationController
, which also has a RootViewController, which is the first view controller you want to show.once you sort that out, its all makes sense.. I think :-)
here is some code that does it all.. (taken from a project I have open in front of me)
}
You have to set it explicitly, and if you do, you can remove the
addSubview
line, because that's handled automatically when you set a root view controller.Of course, a navigation controller's root view controller has nothing to do with that of the window.
initWithRootViewController is just a shortcut for initializing an empty navigation controller and pushing the first (root) view controller onto the stack. Note that
rootViewController
is not a property ofUINavigationController
, you would access it via[navController.viewControllers objectAtIndex:0]
.