First, Brief introduction
UIWindow is a special kind of uiview that usually has only one uiwindow in an app.
After the iOS program starts, the first view control created is UIWindow, then the controller's view is created, the controller's view is added to the UIWindow, and the controller's view is displayed on the screen.
An iOS program can be displayed on the screen entirely because it has uiwindow. It says that without UIWindow, you can't see any UI interface.
Add: UIWindow is the first view control created (the first object created is uiapplication) such as:
Add to
Create a UIWindow, create a controller, create a view of the controller, and then add the view of the controller to the UIWindow.
The explanation for this section in the documentation:
Second, UIWindow the creation process
1. Brief description
Create an empty project and you can see how the UIWindow came out. Once the program is started, it is called once, and the creation process is as follows:
Tip: After the application starts, create the application, create its proxy, and then create the UIWindow. UIWindow inherits from UIView.
2. Add view to UIWindow
Create a controller to add the view to the UIWindow (in two ways)
(1) Directly add the controller's view to the UIWindow, ignoring its corresponding controller
[Self.window Addsubview:vc.view];
(2) Set UIWindow root controller, automatically add Rootviewcontroller view to window, responsible for managing Rootviewcontroller lifecycle
[SELF.WINDOW.ROOTVIEWCONTROLLER=VC];
The difference between the two methods:
In future development, it is recommended to use (2). Because the method (1) has some problems, such as the controller above may be by the button, the need to listen to the button click event, if it is 1, then the button event should be managed by the Controller. But the controller is a local variable, the controller does not exist at this time, but the controller's view is still in, at this time may be error. Note: The controller does not exist until the method is executed.
Problem Description 1: When the view has some events, notify the controller, but the controller has been destroyed, so there may be an unknown error.
Problem Description 2: Add a switch button to rotate the screen 360 degrees (the effect is different). When a screen rotation event occurs, the UIApplication object passes the rotation event to Uiwindow,uiwindow and passes the rotation event to its root controller, which is determined by the root controller whether it needs to be rotated
Uiapplication->uiwindow-> root controller (the first method does not have a root controller, so it cannot be rotated).
Hint: The view that does not pass the controller can also do the development, but in the actual development, do not do so, do not directly add view to UIWindow above go. Because it is difficult to manage.
3. How is UIWindow created in a project with storyboard?
Why create a storyboard, not see the process of creating UIWindow?
It is actually the process of creating the UIWindow to shield up. The value of the UIWindow property of the agent can be printed out NSLog (@ "window=%p", Self.window); The description does create a uiwindow. Not only did you create a uiwindow, but you also created the controller for UIWindow, and you can print to view it. NSLog (@ "%@", Self.window.rootviewcontroller);
The creation process in a project with storyboard:
When the user clicks on the application icon, first executes the main function, executes Uiapplicationmain (), creates the application according to its third and fourth parameters, creates the proxy, and set the proxy to application (see the project configuration file Info.plist inside the storyboard name, according to this name to find the corresponding storyboard), open an event loop, when the program is loaded, he will call the agent Didfinishlaunchi Ngwithoptions: Method. Before calling the Didfinishlaunchingwithoptions: method, the storyboard is loaded, a window is created at the time of loading, the controller that the arrow points to is created, the controller is set to UIWindow's root controller, The window is then displayed, which shows the interface after the run. (Tip: For this section you can view the document's initialization documentation)
Third, how to get window?
1. main window and Secondary window
"Self.window makekeyandvisible" makes the window the main window and displays it. This is the way to display the information on the screen.
Because window has makekeyandvisible this method, can let this window show out of thin air, and the other view does not have this method, so it can only rely on Window,window display, The view is attached to the window to show it.
"Self.window make Keywindow"//Let UIWindow be the main window, but not displayed.
2. Get UIWindow
(1) [UIApplication sharedapplication].windows the UIWindow list opened in this app, so that you can touch any of the UIView objects in the app (the keyboard that normally pops up in the text, In a new UIWindow)
(2) [UIApplication Sharedapplication].keywindow (Get the application's main window) is used to receive keyboard and non-touch class message Event UIWindow, And only one uiwindow at a time in the program is Keywindow.
Tip: If a text box inside a UIWindow cannot enter text, it may be because this uiwindow is not Keywindow
(3) View.window get the UIWindow of a UIView
Four or four diagram of large objects
Transferred from: http://www.cnblogs.com/wendingding/p/3770052.html
iOS Development UI Chapter-uiwindow Brief Introduction