UIWindow in iOS

Source: Internet
Author: User

The role of UIWindow

UIWindow has two main functions:

1 as the topmost container of the UIView view, containing all the UIView to be displayed

2 pass-through touch, non-touch, keyboard event, when passing non-touch and keyboard events, UIWindow must be Keywindow (after experiment, passing touch event does not need UIWindow is Keywindow, but must be visible)

Create UIWindow

When creating a uiview, you need to use Addsubview to add the newly created view to the hierarchy of views, while creating uiwindow is not necessary because UIWindow is the topmost container of the view, as long as it is directly set to Hidden = NO, or call makekeyandvisible to show it. The newly created window is added to the Windows properties of UIApplication, Windows is an array, the entire array is sorted by windowlevel from low to high, i.e. the windowlevel of the No. 0 window is definitely the lowest, The windowlevel of the last window is the highest, if there are two window windowlevel the same, then their order is indeterminate (in this case, not necessarily in the Order of creation)

The Rootviewcontroller property of UIWindow

1 When adding a child view for UIWindow, you can also set the Rootviewcontroller property of the window in addition to using Addsubview. After setting the Rootviewcontroller, the view on the Rootviewcontroller is automatically added to the window, and when the Rootviewcontroller is set, the window has a child view. Then these sub-views will be removed first.

2 If you are in (BOOL) application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) The Launchoptions method returns a window before it is returned, you must set the Rootviewcontroller property for it, otherwise it will report "Application Windows is expected to a root view Controller at the end of application launch "error.

Special if you are in (BOOL) application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) The Launchoptions method creates a Window1 and sets the RootViewController1, and then creates a viewdidload in this rootViewController1 window2, However, without setting the Rootviewcontroller property for it, the program will report the error when it is run, because the RootViewController1 viewdidload is called, (BOOL) Application: ( UIApplication *) Application didfinishlaunchingwithoptions: (Nsdictionary *) The Launchoptions method has not been returned.

In fact, this problem occurs whether the code that creates the Window2 is added to the RootViewController1 Loadview or Viewwillappear, because (BOOL) application: (uiapplication *) Application Didfinishlaunchingwithoptions: (Nsdictionary *) Launchoptions will continue to run after the Viewwillappear call. The solution to the above problem is to put the code that created Window2 in Viewdidappear, at this point-(BOOL) Application: (UIApplication *) application Didfinishlaunchingwithoptions: (Nsdictionary *) The Launchoptions method has finished running.

UIWindow's Windowlevel

The larger the windowlevel, the more visually the window is ahead, and the lower windowlevel window does not obscure the high Windowlevel window. If the windowlevel of the two window are the same, then their order is indeterminate (not necessarily according to the Order of creation), if you want to place a window of the same level at the front of its sibling window, call Makekeyandvisible ( Call Makekeywindow does not play this effect)

UIWindow in iOS

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.