iOS Learning Note--viewcontroller life cycle detailed

Source: Internet
Author: User

In my previous study notes discussed Viewcontroller, after so long, to it also has a new understanding and experience, Viewcontroller is we in the development process encountered the most friends, today to have a good understanding of it. Viewcontroller is the c,viewcontroller of the MVC pattern in iOS development is the controller,viewcontroller responsibility of the view, which mainly includes managing the load display and uninstallation of each view inside. Also responsible for communication and coordination with other Viewcontroller. In iOS, there are two types of viewcontroller, one for display content, such as Uiviewcontroller, Uitableviewcontroller, etc. You can also customize Viewcontroller that inherit from Uiviewcontroller, and Viewcontroller containers, uinavigationviewcontroller and Uitabbarcontroller, etc. , Uinavigationcontroller is to store and manage the Viewcontroller,uitabbarcontroller in the form of a stack that manages viewcontroller in the form of an array. Like activity in Android, Viewcontroller also has its own life cycle (Lifecycle) in iOS development.

First look at the load process of view, such as:

As you can see, the Loadview method is called first in the view load process, in which the key view initialization is done primarily. such as Uinavigationviewcontroller and Uitabbarcontroller, such as the Viewcontroller of the container class, the next is to load the view, after the successful loading, will then call the Viewdidload method, One thing to keep in mind here is that there is no view before Loadview, that is to say, the view has not been initialized before. After the Viewdidload method is completed, the view is loaded successfully inside the Viewcontroller, as shown in the lower right corner.

There are two ways to create a view in a controller, one that is created through code, one that is created by storyboard or interface builder, which compares the visual configuration of the view's appearance and properties. Storyboard with IOS6 after the launch of AutoLayout, should be apple after the main push of a UI customization solution, later I will specifically introduce a use of AutoLayout for UI production articles. To get to the back, create a view through IB or storyboard, after creating a view in the controller, the view will be manipulated in the controller, the following code appears:

[CPP]View Plaincopy
    1. @interface Myviewcontroller ()
    2. @property (nonatomic) iboutlet ID MyButton;
    3. @property (nonatomic) iboutlet ID MyTextField;
    4. -(Ibaction) Myaction: (ID) sender;
    5. @end

Here, a UIButton and a Uitextfield are marked with Iboutlet, and the UIButton response events are marked with Ibaction, Iboutlet and ibaction are an integer constant used to mark the control. The relationship between them can be clearly seen through a picture:

, Myviewcontroller is a custom Viewcontroller inherited from Uiviewcontroller, which contains two view, one is UIButton, the other is Uitextfield, From the direction of the arrow can be a better understanding of Iboutlet and ibaction. Iboutlet is to tell interface Builder that this instance variable is connected to the View object in the nib file, and that Iboutlet itself does nothing, just a token action. Ibaction is also a tag keyword, and it can only mark methods, which tells IB that the method marked with Ibaction can be triggered by a control.

Create a view programmatically, with the following code:

[CPP]View Plaincopy
    1. -  (void) loadview  
    2. {  
    3.      cgrect applicationframe = [[uiscreen mainscreen] applicationframe];  
    4.     uiview *contentview = [[uiview alloc] initwithframe: applicationframe];  
    5.     contentview.backgroundcolor = [ uicolor blackcolor];  
    6.     self.view = contentView;   
    7.    
    8.     levelview = [[levelview alloc ] initwithframe:applicationframe viewcontroller:self];  
    9.     [ self.view addsubview:levelview];  
    10. }  

The above code first gets the frame of the screen, then generates a contentview based on the frame and specifies that the current Viewcontroller root view is Contentview, It then generates a Levelview custom view and adds it through the Addsubview: method to the current Viewcontroller, completing the view initialization load.

With regard to the rewriting of the Loadview method, there is an obvious note in the official document, as follows:

Note:when overriding loadView the method to create your views programmatically, you should not call super . Doing so initiates the default view-loading behavior and usually just wastes CPU cycles. Your own implementation of the method should do all the work that's loadView needed to create a root view and subviews for Y Our view controller.

This means that when you create your own view by way of code, you should not call super in the Loadview method, if calling Super Loadview will affect CPU performance.

Next we look at how the view in Viewcontroller is unloaded:

As you can see, when the system issues a memory warning, the Didreceivememoerywarning method is called, and if there is currently a view that can be released, the system calls the Viewwillunload method to release the view, and the Viewdidunload method is called after completion. , the view has been uninstalled. The variable that originally points to view is set to nil, and the action is to call Self.mybutton = Nil in the Viewdidunload method;

Summary:

The difference between Loadview and Viewdidload is that when the view is Loadview, the view has been generated, viewdidload, and the Loadview will only be called once, Viewdidload may be called multiple times (view may be loaded multiple times), and Viewwillappear will be called before the view is added to another view, and then Viewdidappear will be called. When view is removed from another view, Viewwilldisappear is called and Viewdiddisappear is called after removal. When view is no longer in use, the Viewcontroller will release the view and point it to nil when the memory warning is received.

The process of implementing each method in the Viewcontroller life cycle is as follows:

init->loadview->viewdidload->viewwillapper->viewdidapper->viewwilldisapper->viewdiddisapper- >viewwillunload->viewdidunload->dealloc

iOS Learning Note--viewcontroller life cycle detailed

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.