About when the Loadview in Uiviewcontroller will be called

Source: Internet
Author: User

Each time you access a Uiviewcontroller view (such as Controller.view, Self.view) and the view is Nil,loadview the method is called.

The premise is that the uiviewcontroller of the view is not empty.

Role:

The Loadview method is used to create a uiviewcontroller view

That is, we can use the view from the definition VC

If we do not overload this method. It calls "Super Loadview"; it returns a view. The inside implementation is:

1> It first looks for the Xib file associated with Uiviewcontroller, and creates Uiviewcontroller view by loading the Xib file.

    • If the Xib file name is specified in the initialization uiviewcontroller, the corresponding xib file is loaded according to the Xib file name passed in
      1. [[Mjviewcontroller alloc] initwithnibname:@"Mjviewcontroller" bundle:nil];
    • If the Xib file name is not clearly transmitted, the Xib file with the same name as Uiviewcontroller will be loaded
      1. [[Mjviewcontroller alloc] init]; //Load Mjviewcontroller.xib

2> if the associated xib file is not found, a blank uiview is created and then assigned to the Uiviewcontroller view property, roughly as follows

    1. Self.view = [[[UIView alloc] Initwithframe:[uiscreen mainscreen].applicationframe] autorelease];
    2. The value of the Applicationframe is: {{x = 0, y = +}, {width = +, height = 460}}

[Super Loadview] the contents of 1> and 2> are roughly completed.

We all know that Uiviewcontroller view can be created by xib files, but in some cases xib is not so flexible, so sometimes we want to create uiview through code, such as:

    1. Self.view = [[[UIWebView alloc] Initwithframe:[uiscreen mainscreen].applicationframe] autorelease];

If you want to create a Uiviewcontroller view by code, rewrite the Loadview method, and do not need to call [Super Loadview], because it is mentioned in the 3rd: if there is no Xib file, [Super Loadview] A blank UIView is created by default. Since we are going to customize UIView through code, there is no need to create a blank uiview in advance to save unnecessary overhead. The right approach should be this:

    1. -(void) Loadview {
    2. Self.view = [[[UIWebView alloc] Initwithframe:[uiscreen mainscreen].applicationframe] autorelease];
    3. }

There is no need to call [Super Loadview], and you can call it without error, just cause some unnecessary overhead.

To summarize, Apple's design method is to give us a custom Uiviewcontroller view.

About when Loadview in Uiviewcontroller is called

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.