iOS Development UI Chapter-uiwindow Brief Introduction

Source: Internet
Author: User
Tags file info

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

V. Description of main window and sub-window

Code:
Once the program is started, it will be called once-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *)    launchoptions{//1. Create UIWindow Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];    Sets the background color of the UIWindow Self.window.backgroundColor = [Uicolor Redcolor];    Let the UIWindow show (let the window become the main window and show it)//an application can have only one main window [Self.window makekeyandvisible];        Let UIWindow become the main window//[Self.window Makekeywindow]; 2.    Create a window UIWindow *w2 = [[UIWindow alloc] initwithframe:cgrectmake (100, 100, 200, 200)];    W2.backgroundcolor = [Uicolor Yellowcolor];    [W2 Makekeyandvisible];            SELF.W2 = W2; 3. Create two text input boxes//3.1 Add a text input box to the window Uitextfield *tx1 = [[Uitextfield alloc] Initwithframe:cgrectmake (10, 10, 200,    40)];    Tx1.borderstyle = Uitextborderstyleroundedrect;        [Self.window ADDSUBVIEW:TX1];    3.2 Add a text input box to w2 uitextfield *tx2 = [[Uitextfield alloc] Initwithframe:cgrectmake (10, 10, 100, 40)]; Tx2.borderstyle = Uitextborderstyleroundedrect;        [Self.w2 ADDSUBVIEW:TX2];    Gets the application's main window NSLog (@ "%@", [uiapplication Sharedapplication].keywindow); return YES;}
Code Description: Then create a window (the difference between the main window and the secondary window) local variables, you need to define a Windows property to save the variable. The properties of window are defined as strong so that they are not destroyed. An application can have only one main window, the program created two window, then who is the main window? The rear window can overwrite the previous window. Tip: If Uitextfield is not displayed, you can consider setting its style, because its creation is dashed by default and has no border. In iOS7, there is no difference between the main window and the secondary window before the iOS7: which is the main window, and the following setting overrides the previous setting. (Only the main window can respond to input events on the keyboard, and if you cannot enter content, see if it is displayed on the main window and not on the main window.) ) Vi. Additional Information in the creation process with storyboard:Execute the main function first, execute Uiapplicationmain (), create the application according to its third and fourth parameters, create the proxy, and set the proxy to application, According to the storyboard name in the project configuration file Info.plist, find the corresponding storyboard, then create a window, then create its initialization controller (which is the arrow pointing to the Controller), automatically set the controller as the UIWindow root controller, and then The window is displayed and you see the interface that appears after the run. Note the "Initialize controller properties" on the Controller properties panel. in the creation process without storyboard:Execute the main function first, execute Uiapplicationmain (), create the application according to its third and fourth parameters, create the proxy, and set the proxy to application, turn on an event loop, and when the program is loaded, He will invoke the proxy's didfinishlaunchingwithoptions: method. In this method, a window is created, a controller is created, and the controller is set to the root controller of the UIWindow, and then the window is displayed, showing the interface that is displayed after the run.

iOS Development UI Chapter-uiwindow Simple Introduction

Related Article

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.