I have a question regarding window and viewcontroller in iOS. I just have a look at the app delegate of iOS project that I am working on today and found that it is required to have…
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
in my app.
Here are the questions:
-
Does this means that navigationController is the root view controller of my app? NavigationController is a subclass of UIViewController, but its task is only for providing navigation function at the navigation bar, correct?
-
What is self.window? I think I understand the concept of ‘view’ and ‘viewController’ but I do not quite understand what a ‘window’ is… An iPhone has one screen, but MacPro could have 2 monitors; Are these windows in terms of iOS and OS X?
Possibly. self.window.rootViewController will return the root view controller of the window, I presume, in this case, from the storyboard. The fact that this line casts the object returned to a navigation controller doesn’t make it one — it will be whatever it is in the storyboard (the controller with that left arrow that’s not connected to anything else). Assuming that the cast is correct, this allows you to write things like navigationController.topViewController and not have the compiler complain about it. As for the navigation controller’s function, it does provide the function for the navigation bar, but it also shows the view of its content controllers, with its topViewController’s view being the one that you will see at start up.
A window in iOS, is a UIWindow, which is a subclass of UIView, so it’s not the same as a window in OS X. Look at the Overview section of the UIWindow Class Reference for a discussion of what it does.