3 ways to create a controller with iOS Dev-73, in-depth understanding of the creation and loading order of the view

Source: Internet
Author: User

(1) 3 ways to create a controller

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {    Self.window=[[uiwindow Alloc]initwithframe:[uiscreen Mainscreen].bounds];    [Self.window makekeyandvisible];    About the main window//before iOS6, the text box inside cannot be entered text Self.window.backgroundcolor=[uicolor Redcolor] without becoming the main window;    The following can bring up the main window of the application and the window array//[uiapplication sharedapplication].windows;        [UIApplication Sharedapplication].keywindow; 1th: Direct alloc and INIT create Controller// 2nd: Created by Storyboard's Instantiateinitialviewcontroller or instantiateviewcontrollerwithidentifier//Because we set the main    Storyboard, so the system automatically generated the following code Uistoryboard *storyboard=[uistoryboard storyboardwithname:@ "Main" bundle:nil];        Self.window.rootviewcontroller=[storyboard Instantiateinitialviewcontroller];    If we create a storyboard ourselves, then load with Uistoryboard *two=[uistoryboard storyboardwithname:@ "one" bundle:nil];        Self.window.rootviewcontroller=[two Instantiateinitialviewcontroller]; And if there are two controllers in the two files, then the load will depend onStorynoard ID uistoryboard *three=[uistoryboard storyboardwithname:@ "" "Bundle:nil];        Self.window.rootviewcontroller=[three instantiateviewcontrollerwithidentifier:@ "Hello"]; The 3rd type: When the controller is created, give it a xib file as a view//need to create a xib file, and then change the Xib file's owner to this view controller, and put the view controller's attempt to connect to the created view, so the following code is used/ /note, do not mistakenly create a controller Oneviewcontroller *onevc=[[oneviewcontroller alloc]initwithnibname:@ "Onexib" bundle when creating a view in the Xib file:    NIL];        SELF.WINDOW.ROOTVIEWCONTROLLER=ONEVC;    Override point for customization after application launch. return YES;}

The third way to create a onexib file:

(2) View creation process

In fact, the above in the creation of the controller has created a lot of views view.

--The simplest is to drag and drop a controller in the storyboard, which comes with a view.

--The simplest and most useful code alloc after init a view controller, there will also be a. View property to describe the view.

--and we can also load this view by creating a view in the Xib file and connecting the view to a controller.

(3) So the question is: If so many views are present, which view does the controller load first?

In fact, the big Boss is not the above, But the Loadview view is the highest priority (that is, as long as the view controller. m file in this method to customize the view, then the first load this view, as to what storyboard and xib inside the view is a cloud) has the following code, then the view is green.

-(void) loadview{    self.view=[[uiview Alloc]init];    Self.view.backgroundcolor=[uicolor Greencolor];}

So the order in which the views are created or loaded is:

1, there is a Loadview method, loading the view in the Loadview, this method is used to customize the view.

2, if there is no loadview, then first see if there is no storyboard, if there is storyboard, according to the name of storyboard to load the inside view.

3, if there is no storyboard, then load the xib inside the view. The order at this point is:

3.1. If the name of the xib is specified, the view inside the response is loaded;

3.2, if the name is not specified, then the controller name prefix is loaded with the same xib view, such as the controller name is Wpviewcontroller, then the first load is called Wpview xib file in the view.

3.2. Second, if there is no Xib file called Wpview, load the view in the Xib with the same name as the controller, that is, load the views in the Wpviewcontroller.

3.3, if not, then load a blank view.

(4) Phenomenon: Loadview only call once, that is, the 1th time to open the program when found no view, then go to find Loadview, if there are words to create a view, if there is no other way to find him. After loading, the 2nd time no longer calls Loadview. So the code below is a dead loop because it's always looking for a view.

-(void) loadview{    self.view.backgroundcolor=[uicolor Greencolor];}

(5) Remember: The controller's view is also delayed loading, that is, when used to load, that is, call the Viewdidload method, you can use the print input to verify.

(6) Another way to create a xib is to create a xib file while creating the controller, the name of the Xib file and the name of the view controller, which also verifies the description of the Xib name when we load the view above. So the name is still fastidious, can not be casually named.

3 ways to create a controller with iOS Dev-73, in-depth understanding of the creation and loading order of the view

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.