The creation process for iOS view

Source: Internet
Author: User

The chef sent me a bunch of view creation functions to let me know the process of creating the view.

-(ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) nibbundleornil{self    = [Super Initwithnibname: Nibnameornil Bundle:nibbundleornil];    if (self) {        //Custom initialization    }    return to self;} -(void) Viewwillappear: (bool) animated{}-(void) Viewdidappear: (bool) animated{}-(void) loadview{}-(void) Layoutsublayersoflayer: (Calayer *) layer{}-(void) viewwilllayoutsubviews{}-(void) viewdidload{}

I didn't think much of the function. Create a single View application and then create a viewcontroller to copy the master's function and write it in the appdelegeted.m file.

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions{    Self.window = [[UIWindow alloc]initwithframe:[[uiscreen Mainscreen] bounds]];     Self.window.rootViewController = [[Myviewcontroller alloc]init];        [Self.window makekeyandvisible];    Override point for customization after application launch.    return YES;}

Then run ... It's cold ... An error has occurred;

Start up black screen and then report such an error application windows is expected to having a root view controller at the end of the application launch

2014-08-13 15:16:45.924 Task 1 copy [2629:60b]-[YOUVC loadview]2014-08-13 15:16:45.925 task 1 copy [2629:60b]-[YOUVC Viewdidload] 2014-08-13 15:16:45.925 Task 1 copy [2629:60b]-[YOUVC loadview]2014-08-13 15:16:45.926 task 1 copy [2629:60b]-[YOUVC Viewdidload] 2014-08-13 15:16:45.927 Task 1 copy [2629:60b]-[YOUVC loadview]2014-08-13 15:16:45.927 task 1 copy [2629:60b]-[YOUVC Viewdidload] 2014-08-13 15:16:45.928 Task 1 copy [2629:60b]-[YOUVC loadview]2014-08-13 15:16:45.928 task 1 copy [2629:60b]-[YOUVC Viewdidload] 2014-08-13 15:16:45.929 Task 1 copy [2629:60b] application windows is expected to has a root view controller at the end of APPL Ication Launch



No solution, the Internet search all the programs are wrong. I put the focus on self.window.rootviewcontroller = .... This sentence went up;


I started thinking about why it went wrong, why these methods were called several times, and one-step debugging found that these functions were not just called at the time of Init initialization.

I added a new viewcontroller to add a background color, the result can run, and I tried to put the teacher to the code, and the result was wrong.

I began to try to find out what was wrong, and finally I found out the reason for the-(void) Loadview this function;

I started querying this function again, and finally I got the answer from M-Teacher J:

This function is called when the view is empty each time the view is called, and his role is to create a view, first load xib if there is no xib, automatically creates a blank view of size 320*480;

In the program's

-(void) Loadview added [Super Loadview], after which the program can be run;

-(ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) nibbundleornil{self    = [Super Initwithnibname: Nibnameornil Bundle:nibbundleornil];    if (self) {        //Custom initialization    }    return to self;} -(ID) init{    if (self = [super init])    {        self.view.frame=[[uiscreen mainscreen]bounds];        Self.view.backgroundColor = [Uicolor orangecolor];    }    return self;} -(void) loadview{   //[Super Loadview];    NSLog (@ "%s", __function__);} -(void) viewdidload{    [Super Viewdidload];    NSLog (@ "%s", __function__);    Do any additional setup after loading the view from its nib.} -(void) Viewwillappear: (BOOL) animated{    NSLog (@ "%s", __function__);} -(void) Viewdidappear: (BOOL) animated{    NSLog (@ "%s", __function__);} -(void) Layoutsublayersoflayer: (Calayer *) layer{    NSLog (@ "%s", __function__);    } -(void) viewwilllayoutsubviews{    NSLog (@ "%s", __function__);}

Operation Result:

2014-08-13 15:35:54.783 Task 1 copy [2660:60b]-[YOUVC loadview]2014-08-13 15:35:54.783 task 1 copy [2660:60b]-[YOUVC Viewdidload] 2014-08-13 15:35:54.784 Task 1 copy [2660:60b]-[YOUVC viewwillappear:]2014-08-13 15:35:54.785 task 1 copy [2660:60b]-[youvc viewwilllayoutsubviews]2014-08-13 15:35:54.835 Task 1 copy [2660:60b]-[YOUVC viewdidappear:]

This is the run order of the associated View creation function when creating the view:

M A J teacher's blog http://www.cnblogs.com/mjios/archive/2013/02/26/2933667.html in detail about these functions;

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.