UIWindow in IOS

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">When you create a new project with xcode, Xcode automatically creates a new file with Appdelegate.h,appdelegate.m,viewcontroller.h,viewcontroller.m,main.storyboard. One of the objects in AppDelegate.h is uiwindow, and the code is as Follows:</span></p></p><pre><pre>@property (strong, Nonatomic) UIWindow *window;</pre></pre><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">so, What is the role of this window?</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">In fact, an app can be displayed on the screen because of the UIWindow Object. Only UIWindow can actively display, other view can be added to uiwindow, so that other view will be displayed on the Screen.</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">Although it is not possible to create a new project without storyboard in Xcode7, we can test UIWindow by modifying the Program's main interface. Here's How:</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">Click Project->general to change main Interface to Empty. Such as:</span></p></p><p><p></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">Then run the program, you will find the entire screen black screen, such as:</span></p></p><p><p></p></p><p><p></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">The reason for the black screen is because UIWindow is not implemented.</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">now, manually implement the next UIWINDOW.</span></p></p><p><p></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">Add the Code for window in appdelegate.m:</span></p></p><pre><pre>-(BOOL) application: (uiapplication *) application didfinishlaunchingwithoptions: (nsdictionary *<span style="color: #000000;">) launchOptions { </span> <span style="color: #008000;">//</span> <span style="color: #008000;">Override point for customization after application Launch.</span> Self.window =<span style="color: #000000;"> [[uiwindow alloc] initwithframe:[uiscreen mainscreen].bounds]; </span> =<span style="color: #000000;"> [uicolor whitecolor]; [self.window makekeyandvisible]; </span> <span style="color: #0000ff;">return</span> <span style="color: #000000;">YES;}</span></pre></pre><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">At this point, the running program will find an error, the error is as Follows:</span></p></p><pre><pre><span style="color: #800000;">'</span> <span style="color: #800000;">nsinternalinconsistencyexception</span> <span style="color: #800000;">'</span> <span style="color: #800000;">'</span> <span style="color: #800000;">application windows is expected to has a root view controller at the end of application launch</span> <span style="color: #800000;">'</span></pre></pre><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">Hint No rootviewcontroller. That is, you need to set the Rootviewcontroller of the UIWindow Object.</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">The Rootviewcontroller code for setting UIWindow is as Follows:</span></p></p><pre><pre>-(BOOL) application: (uiapplication *) application didfinishlaunchingwithoptions: (nsdictionary *<span style=" Color: #000000; ">) launchoptions {</span> <span style="color: #008000;">//</span> <span style="color: #008000;"> Override point for Customization after application Launch. </span> Self.window =<span style="color: #000000;"> [[uiwindow alloc] Initwithframe:[uiscreen mainscreen].bounds]; Self.window.backgroundColor </span> =<span style="color: #000000;"> [uicolor whitecolor]; [self.window makekeyandvisible]; </span> <span style="color: #008000;">//</span> <span style="color: #008000;"> set Rootviewcontroller </span> Viewcontroller *controller =<span style="color: #000000;"> [[viewcontroller alloc] init]; Self.window.rootViewController </span> =<span style="color: #000000;"> controller; </span> <span style="color: #0000ff;">return </span> <span style="color: #000000;"> yes;} </span> </pre></pre><p><p><span style="font-size: 14px; font-family: ‘Microsoft YaHei‘;">Run the program and you will see the same as without modifying the main Interface. </span></p></p><p><p><span style="font-size: 14px; font-family: ‘Microsoft YaHei‘;">We know that when the app starts, it's called immediately</span></p></p><pre><pre><span style="font-size: 14px; font-family: ‘Microsoft YaHei‘;"></span><br><span style="font-size: 14px; font-family: ‘Microsoft YaHei‘;">method, in which the Rootviewcontroller of UIWindow and UIWindow can be set. This way, the entire app is displayed on the Screen.</span></pre></pre><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">When you create a new project in xcode, you typically include the storyboard file, and you do not need to set UIWindow in the method Above. so, in the case of a storyboard file, how is the UIWindow set up?</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">In fact, when the program loads the primary storyboard (that is, the storyboard that main Interface points to), the UIWindow of UIWindow and Rootviewcontroller is Set.</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">Summarize the start-up process for the next app:</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">1: Call the Main method</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">2: call the Uiapplicationmain method, and do the following two things in the method:</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">(1) Create a UIApplication object</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">(2) creating a UIApplication Delegate object</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">3.1 In the absence of storyboard, the delegate object begins to listen for system events:</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">(1) the Application:didfinishlaunchingwithoptions method is called when the program is Started.</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">(2) Create a UIWindow in the Application:didfinishlaunchingwithoptions method</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">(3) Create and set UIWindow Rootviewcontroller</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">(4) show the creation of the library</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">3.2 In the case of storyboard, according to Info.plist to obtain the most important storyboard file name, load the main storyboard:</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">(1) Create UIWindow</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">(2) Create and set UIWindow Rootviewcontroller</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">(3) Display window</span></p></p><pre><pre><span><span style="font-size: 14px; font-family: ‘Microsoft YaHei‘;"><br></span><br></span></pre></pre><p><p> UIWindow in IOS </p> </p></span>

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.