The basic tutorial for using View Control UIWindow in IOS application development _ios

Source: Internet
Author: User
Tags file info

First, a brief introduction

iphone applications usually have only one window, represented as an instance of a UIWindow class. The application creates this window at startup (or from a nib file) and adds one or more views to the window and displays it. After that we rarely need to refer to it again. The UIWindow object is the root of all UIView, the display of managed and coordinated applications. A generic application has only one UIWindow object, and even if there are multiple UIWindow objects, only one UIWindow can accept the user's touchscreen event.
In iOS, UIWindow objects do not have parts such as close boxes or title bars that are common in Windows applications, and ordinary users cannot turn them off or other operations directly. All operation of the window needs to be implemented through its programming interface. The application can use window objects for event delivery. The Window object keeps track of the current first responder object and passes it when the UIApplication object makes a request.
UIWindow is a special kind of uiview, which usually has only one uiwindow in an app.
In Mac OS X, the parent class of Nswindow is Nsresponder, and in the iphone OS, UIWindow's parent is uiview. Therefore, the UIWindow window is also a View object in iOS.
When you create an application window, you should always set its initial border size to the size of the entire screen. If your window is loaded from a nib file, Interface Builder does not allow you to create a window that is smaller than the screen size; however, if your window was created programmatically, you must pass in the desired border rectangle at creation time. There is no reason to pass in other border rectangles other than the screen rectangle.
After the iOS program is started, the first view control created is UIWindow, then creates the controller's view, and then adds the controller's view to the UIWindow, so the controller view is displayed on the screen.

An iOS program can be displayed on the screen simply because it has uiwindow. Also say, no uiwindow, can't see any UI interface

Add: UIWindow is the first view control created (the first object created is UIApplication) as follows:

Add to

First create the UIWindow, then create the controller, create the controller's view, and then add the controller's view to the UIWindow.

Explanation of this section in the document:

Ii. the creation process of UIWindow

1. Simple description

Create an empty project and you can see how the UIWindow came out. Called once after the program has been started, the creation process is as follows:
Tip: After the application starts, create the application, then create its agent, and then create the UIWindow. UIWindow inherits from UIView.

2. Add view to UIWindow

Create a controller that adds view to the UIWindow (there are two ways)

(1) directly to the controller's view added to the UIWindow, ignoring its corresponding controller

Copy Code code as follows:

[Self.window Addsubview:vc.view];

(2) Set UIWindow root controller, automatically add Rootviewcontroller view to window, responsible for managing Rootviewcontroller lifecycle
Copy Code code as follows:

[SELF.WINDOW.ROOTVIEWCONTROLLER=VC];

Two differences of methods:

In future development, it is recommended to use (2). Because the method (1) There are some problems, such as the controller above may be a button, 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, at this time may be an error. Note: After the method is executed, the controller is no longer present.

Problem Description 1: When the view occurs 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 allow the screen to rotate 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 has no root controller, so it cannot be rotated).

Hint: The view can also do not through the controller development, but in the actual development, do not do so, do not directly add view to the UIWindow above. Because, difficult to manage.


3. How was UIWindow created in a storyboard project?

Why create a storyboard and not see the process of creating UIWindow?

It's actually masking the process of creating UIWindow. You can print the value of the UIWindow property of the agent NSLog (@ "window=%p", Self.window); it's really worth printing. The description does create the UIWindow. Not only has UIWindow been created, the UIWindow corresponding controller has also been created by default, and can be printed for viewing. NSLog (@ "%@", Self.window.rootviewcontroller);

The creation process in a storyboard project:

When the user clicks on the application icon, first executes the main function, executes Uiapplicationmain (), creates the application based on its third and fourth arguments, creates the proxy, And the agent set to application (see the project configuration file Info.plist inside the name of the storyboard, according to the name of the corresponding storyboard), open an event loop, when the program loaded, he will invoke the proxy Didfinishlaunchi Ngwithoptions: Method. Before invoking the Didfinishlaunchingwithoptions: method, the storyboard is loaded, a window is created when it is loaded, and then the controller that the arrow points to is created, and the controller is set to the UIWindow root controller. Next, the window is displayed, and you see the interface that is displayed after the run. (Hint: In this section you can view the story initialization document)

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 to the screen.

Because window has makekeyandvisible this method, can let this window appear out of thin air, while other view does not have this method, so it can only depend on Window,window display, View is attached to the window to show it.

[Self.window make keywindow]//makes UIWindow the main window, but does not display it.


2. Get UIWindow

(1) [UIApplication sharedapplication].windows a list of UIWindow opened in this application so that you can contact any one of the UIView objects in the application (usually type the keyboard that pops into the text, is in a new UIWindow.

(2) [UIApplication Sharedapplication].keywindow (Gets the main window of the application) is used to receive uiwindow of message events for keyboards and non-touch classes. And there can only be one uiwindow per moment in the program Keywindow.

Tip: If a text box inside a UIWindow cannot enter text, it may be because this uiwindow is not Keywindow

(3) View.window obtains the UIWindow of a UIView

Diagram of four or four large objects

Five, main window and secondary window description

Code:

Copy Code code as follows:

Once the program is started, it is called once.
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
1. Create UIWindow
Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];
Set the background color of 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 be the main window
[Self.window Makekeywindow];

2. Create a second 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 entry 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 the W2
Uitextfield *tx2 = [[Uitextfield alloc] Initwithframe:cgrectmake (10, 10, 100, 40)];
Tx2.borderstyle = Uitextborderstyleroundedrect;
[Self.w2 ADDSUBVIEW:TX2];


Get the application's main window
NSLog (@ "%@", [uiapplication Sharedapplication].keywindow);
return YES;
}


Code Description:
Create a second window (the difference between the main window and the secondary window)
Local variables, you need to define a window property to hold the variable.
The properties of Windows are defined as strong, so that they are not destroyed.
An application can have only one main window, and two windows are created in the program, who is the main window? The window behind will cover the front window.
Tip: If Uitextfield is not displayed, you can consider setting its style because it is created by default and has no border.
Inside the iOS7, the main window and the secondary window are indistinguishable.
There is a difference before iOS7: which is the main window, and the main window behind it will cover the previous settings. (Only the main window can respond to input events on the keyboard, and if you cannot enter content, you can see if it is displayed on the main window and not on the main window.) )
Vi. Supplementary Notes
In the creation process with storyboard:
First executes the main function, executes Uiapplicationmain (), creates the application based on its third and fourth arguments, creates the proxy, and sets the proxy to application. Depending on the name of the storyboard in the Project Profile Info.plist, locate the corresponding storyboard, create a window, then create its initialization controller (the controller that the arrow points to), automatically set the controller to the UIWindow root controller, and then The window is displayed, and you see the interface that is displayed after you run it.

Note the "Initialize controller properties" on this controller's properties panel.

The creation process in the absence of storyboard:
First executes the main function, executes the Uiapplicationmain (), creates the application based on its third and fourth parameters, creates the proxy, and sets the proxy to the application, opens an event loop, and when the program has finished loading, He will invoke the agent's Didfinishlaunchingwithoptions: method. In this method, you create a window, create a controller, and set the controller to the root controller of the UIWindow, and then display the window again, seeing the interface that is displayed after the run.

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.