Walker acting in layman's--Principles of program Initiation (under) (Explore UIWindow)

Source: Internet
Author: User
Tags file info

The previous section mainly explains the start-up principle of the program uiapplication, and the execution of the main function, which now mainly explains the loading of the interface and the execution part. UIWindow

Let's take a look at Apple's official documentation

Apple official documentation

UIWindow inherited from Uiview,uiwindow is a special uiview, usually only one uiwindow in a program, but you can manually create multiple UIWindow and add it to the program. Even if there are multiple UIWindow objects, only one UIWindow can accept the user's touchscreen event (that is, the main window).

After the iOS program starts, create a application, create its proxy, create UIWindow (the first object created is UIApplication), create a view of the controller, and finally add the view of the controller to the UIWindow. The controller's view is then displayed on the screen.

An iOS program can be displayed on the screen entirely because it has uiwindow. It also says that without UIWindow, you can't see any UI interface.

main window and Secondary window
【self.window makekeyandvisible】让窗口成为主窗口,并且显示出来。有这个方法,才能把信息显示到屏幕上。

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】//让uiwindow成为主窗口,但不显示。

Secondary window, you need to define a window property to hold the variable. The properties of window are defined as strong, so that an application can have only one main window for it to not destroy. Only the main window can respond to keyboard input events, and if you cannot enter content, you may be able to see if it is displayed on the main window and not on the main window.

Windowlevel

UIWindow has three levels, namely Normal,statusbar,alert. The normal level is the lowest, StatusBar is at the middle level and the alert level is the highest. And usually our program interface is at the normal level, the system at the top of the status bar should be at the statusbar level, Uiactionsheet and uialertview these are usually used to interrupt the normal process, remind users and other operations, It is therefore located at the alert level.

According to the principle of window display level first, the high level will be shown above, the lower level below, our program normal display of the view at the bottom.

When level levels are the same, only the first setting is displayed for Keywindow, and the Keywindow of the subsequent siblings is not displayed. UIWindow in the display of the time is regardless of who keywindow, are level priority, that is, the highest level is always displayed at the front.

UIWindow is the starting point of the display

The 1.UIWindow object is the root of all uiview, managing and coordinating the display of the application.
The 2.UIViewController object is responsible for managing the hierarchy of all uiview and responding to changes in the direction of the device.
A 3.UIView object defines a rectangular area on a screen that handles drawing and touch-screen events for that area. You can draw graphics and text within this area, and you can also receive user actions. An instance of UIView can contain and manage several sub-uiview.

The role of UIWindow in the program

1. As a container, contains all the views that the app will display
2. Pass the touch message to the program view and other objects
3. Work with Uiviewcontroller to facilitate equipment orientation rotation support

Storyboard the creation process in a project

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.

The difference between Rootviewcontroller and Addsubview

Create a controller, add view to UIWindow there are two ways
1.rootViewController
Rootviewcontroller is a traversal method of UIWindow, by setting this property to add a view corresponding to the Viewcontroller,uiwindow will automatically add its view to the current window, Also responsible for Viewcontroller and view life cycle maintenance to prevent premature release

2.addSubview
The view is added directly to the window by Addsubview, the program is responsible for maintaining the view's life cycle and refreshing, but not for the viewcontroller of the view, so after adding the view to the window in this way, We also need to maintain the validity of the viewcontroller of the view and not release it prematurely.

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. In future development, we recommend the use of Rootviewcontroller.

UIView Methods for layer layout

A UIView can contain many Subview (other UIView), and these Subview have a hierarchical relationship with each other.
1. New Subview

 [UIView addSubview:Subview];     //替UIView增加一个Subview

2. Move a layer
Move the Subview forward or backward in the UIView, moving forward will cover the Subview of the lower layer, and the next move will be covered by the upper Subview.

[UIView bringSubviewToFront:Subview];       //将Subview往前移动一个图层(与它的前一个图层对调位置)[UIView sendSubviewToBack:Subview];      //将Subview往后移动一个图层(与它的后一个图层对调位置)

3. Swap two subview layer levels with each other

[UIView exchangeSubviewAtIndex:indexA withSubviewAtIndex:indexB];    //交换两个图层

4. Get the index value of the child view in UIView (index)

NSInteger index = [[UIView subviews] indexOfObject:Subview名称];       //取得Index(Note: If there is no UIWindow, the interface is black by default, although you can create an interface and change its color, so that the main interface has a color display, but this interface is not interactive, not part of the controller's view, specifically, create a button on this view can not click). UIWindow

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.

A program can be displayed on the screen completely because it has uiwindow, no UIWindow, no UI interface;

There are two common ways to add UIView to UIWindow:

The 1> directly adds the view to the UIWindow, but does not ignore the view Uiviewcontroller

?
1 - (void)addSubview:(UIView *)view;

2> automatically adds Rootviewcontroller view to UIWindow, which manages the life cycle of the Rootviewcontroller;

?
1 @property(nonatomic,retain) UIViewController *rootViewController;

Common methods:

?
12  - (void)makeKeyWindow;  //让当前UIWindow变成keyWindow(主窗口) - (void)makeKeyAndVisible;  //让当前UIWindow变成keyWindow,并显示出来

UIWindow's acquisition:?
1 [UIApplication sharedApplication].windows

The UIWindow list opens in this app so you can touch any of the UIView objects in your app (the keyboard that pops up in a new UIWindow when you normally enter text).

?
1 [UIApplication sharedApplication].keyWindow

The UIWindow used to receive message events for keyboards and non-touch classes, and only one uiwindow at a time in a program is Keywindow. If a text box inside a UIWindow cannot enter text, Maybe it's because this uiwindow is not keywindow.

?
1 view.window

Get the UIWindow where a uiview is located.

Walker acting in layman's--Principles of program Initiation (under) (Explore UIWindow)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.