Difference Between Window and ViewController
What I am trying to learn is what is the difference between the window, and viewcontroller. I know that you can only have one window per application. But you can have multiple viewcontrollers. When I create a project thats based off of the window-based application template I get only a window. I can create my own viewcontrollers, but I also know that I can put things directly onto the window in a window-based application (I think correct me if I am wrong). But with a view-based application I of course get a view that I can add things to. Can an开发者_Python百科yone clarify any of this for me if I make any sense?
Basically you have one instance of UIWindow
that's hosting all your UIViewControllers
and UIViews
as part of the view hierarchy. That's why UIApplication
has a call [UIApplication sharedApplication].keyWindow
, to access the "root" view.
So for example you can have the following stack:
[UIApplication sharedApplication].keyWindow
->
MyView
->
MyOtherViewController
For more information, read up the Developer Documentation on UIWindow and UIViewController which provide a great explanation on the differences.
They're two completely different things. A window is a view that's special mainly in that it doesn't have a superview; it's the container that holds all other views. Because of its position at the root of the view containment tree, a window helps in the process of dispatching events to the proper views and redrawing as needed. Other than that, the window doesn't worry too much about the views it contains.
A view controller is not a view at all. It's a controller that manages a view and all its subviews. A view controller typically responds to user input from controls, populates views with the data they need, manages visibility of subviews, etc. The view that a view controller manages is installed in a window when that view controller is active, but the view controller generally doesn't keep a direct reference to the window itself.
精彩评论