How views are created for view controllers

Source: Internet
Author: User

There are three ways to create Uiviewcontrller:

1. Created directly via Alloc init.

2. Create from the story version.

3. Through the Xib file description.

This is APPDELEGATE.M's content.

  //The color of window is greenSelf.window =[[UIWindow Alloc]initwithframe:[uiscreen mainscreen].bounds]; Self.window.backgroundColor=[Uicolor Greencolor]; /** First: Directly alloc init to create a controller, the controller will automatically create a view, there is nothing*/   //Xbviewcontroller * VC = [[Xbviewcontroller alloc]init];        /** Second: Create a controller from the story version, when the controller view is the story version above the view described above the story version of the controller to be associated with this class*/Uistoryboard* StoryBoard = [Uistoryboard storyboardwithname:@"XBVC"Bundle:nil]; Xbviewcontroller*VC = [StoryBoard instantiateviewcontrollerwithidentifier:@"XBVC"]; Self.window.rootViewController= VC;//Assigning a value to the root controller        /** Third: Create steps by Xib files: 1.xib The file owner wants to set the cost class, and requires manual 2. To associate views on the View property of this class with a xib*/Xbviewcontroller* VC = [[Xbviewcontroller alloc]initwithnibname:@"Xbviewcontroller"Bundle:nil]; /*When the name of the Xib is the same as the name of the view controller itself, the direct call to Init is also created by default Xib*/Xbviewcontroller* VC =[[Xbviewcontroller alloc]init]; /*when there is no Xib file with the same name as the controller, it is possible to have XbView type .*/Xbviewcontroller* VC =[[Xbviewcontroller alloc]init]; Self.window.rootViewController=VC; [Self.window makekeyandvisible];//render the root controller

Xbviewcontroller.m

/** * The Loadview method is used to create the view you need, and can be defined here when you need to extend the load view. Just rewrite this method, whether the controller is created from the story or Xib.  are not valid. This method appears before Viewdidload, where Self.view is empty, needs to be created and must be created, otherwise it will fall into a dead loop!!  Unless the [Super Loadview] is called; After this method is finished, you must ensure that Self.view is not empty, otherwise you will call yourself*/- (void) loadview{//[Super Loadview];//do not write this code and do not create objects for Self.view will panic////Self.view = [[UIView alloc]init];//Self.view.backgroundColor = [Uicolor bluecolor];//UILabel * label = [[UILabel alloc]initwithframe:cgrectmake (+, +)];//[email protected] "Now the view is created inside the Loadview";//[Self.view Addsubview:label];NSLog (@"Loadview");}//This method is called when the view is loaded, whether it is the default view generated by the system itself, or the story version, Xib. - (void) viewdidload {[Super viewdidload]; UILabel* label = [[UILabel alloc]initwithframe:cgrectmake ( -, -, -, -)]; Label.text=@"here Viewdidload inside the view";    [Self.view Addsubview:label]; NSLog (@"View load Complete");}

How the controller view is created:

Life cycle diagram of the controller

How views are created for view controllers

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.