The life cycle of IOS view and Viewcontroller

Source: Internet
Author: User

Transferred from: http://blog.sina.com.cn/s/blog_801997310101a39w.html

I. Duties of Viewcontroller

Internal management of the associated view, external and other viewcontroller communication and coordination. The view,viewcontroller associated with it always loads the view when it is needed and unloads the view when it is not needed, so it also assumes responsibility for managing the application resources.

Second, the life cycle of Viewcontroller

View refers to the controller's view. It acts as an attribute of the controler, and the life cycle is within the controller's life cycle. This means that your controller cannot be released before the view is released.

Life cycle Diagram of Viewcontroller

It should be stated that when you alloc and init a viewcontroller, the Viewcontroller should not have created a view. Viewcontroller view is created using the Lazyinit method, that is, the getter of the View property that you invoke: [Self view]. The getter will first determine if the view is created, and if not, it will call Loadview to create the view. Loadview will continue to call Viewdidload when it finishes. One difference between Loadview and viewdidload is that there is no view at Loadview. And the viewdidload when the view and created well.

Third, the load process of view

The text description is always very laborious in the presentation process, I found the following two pictures

View Load Process diagram


Follow the following text to understand the Viewcontroller view loading process:

1 First determine if the subclass overrides Loadview, if there is a direct call. After that, the viewdidload completes the view loading.

2 If you specify the nib file name externally by calling Initwithnibname:bundle, Viewcontroller record this nib to create the view.

3 if the name parameter of Initwithnibname:bundle is nil, Viewcontroller will find the nib associated with it in the following two steps.

A If the class name contains a controller, for example, the class name of Viewcontroller is Myviewcontroller, the lookup exists for myview.nib;

b Find the same file as the Viewcontroller class name, such as Myviewcontroller, to find out if there is a myviewcontroller.nib.

4 If the subclass does not have an overridden Loadview, Viewcontroller will find or call its default loadview from Stroyboards, and the default Loadview returns a blank UIView object.

Note that the first step, Viewcontroller, is to determine whether the subclass overrides the Loadview, rather than judging whether the Viewcontroller view is empty after the loadview of the child class is called. That is, if the subclass overrides the Loadview, the Loadview will be directly viewdidload to finish loading the view, regardless of whether the subclass can get to the View,viewcontroller.

View Unload Process Diagram


Follow the following text to understand the uninstallation process:

1 system warns or Viewcontroller itself calls cause didreceivememorywarning to be called

2 release view after calling Viewwillunload

3 Calling Viewdidunload

Iv. sequence of calls to the simulator

I have framed such an environment, in which there are two Viewcontroller, named A and B,tag respectively, 1 and 2,a control program start when the loading interface, put a button in a, press the segue to call to the interface b;b the page put a button, by executing

[Selfdismissmodalviewcontrolleranimated:yes];

To return to interface a

Then all the function calls are detected, in the following sequence

When you load a, call

1initWithCoder

1 loadview//If you make a rewrite, it will be called here, this step can refer to the following

1 viewdidload

1viewWillAppear

1viewWillLayoutSubviews

1viewDidLayoutSubviews

1viewDidAppear

When you switch to B, call

2 Initwithcoder//Initialize 2 first

1 Prepareforsegue//call 1 for an overly-prepared function, so you can assign values to some of the related properties of interface B in this function

2 Loadview//If overridden here

2 Viewdidload//2 Interface loading

1viewWillDisappear

2viewWillAppear

2viewWillLayoutSubviews

2viewDidLayoutSubviews

2viewDidAppear

1viewDidDisappear

When you switch back from B to a, call

2viewWillDisappear

1viewWillAppear

1viewDidAppear

2viewDidDisappear

2 Dealloc

Order Summary down loading: Load-Display-layout

Completion Order: Complete layout-Finish display-Finish loading

Small bet:-(void) Loadview; function If overridden, here is a possible demo

-(void) Loadview

{

Cgrectapplicationframe = [[Uiscreenmainscreen]applicationframe];

Uiview*contentview = [[Uiviewalloc] initwithframe:applicationframe];

contentview.backgroundcolor= [Uicolordarkgraycolor];

Self.view =contentview;

UILabel *lab= [[Uilabelalloc]initwithframe:cgrectmake (100, 100, 100, 100)];

Lab.text =@ "HelloWorld";

[Self.viewaddSubview:lab];

}

Loadview Although the return value is empty, you must assign a value to the Self.view in the body of the function, or you will receive the following log message when you set up the interface:

Applicationwindows is expected to has a root view controller at the end Ofapplication launch

The specific execution order is: The code executes the Initwithcoder directly after the three Loadview function is called, and no other functions are called (including Viewdidload, Viewwilldisappear, Viewwilllayoutsubviews)

Questions:

I do not know why the call three times, my guess is: The above three functions to detect the existence of the view, the discovery does not exist, so the respective call the Viewload, the last discovery still does not exist, so the above three functions returned the failure, loading completed

But the paradox is: Why did the above three functions not execute themselves? What did the bottom end do?

V. The creation phase of view and Viewcontroller, about when to do

1. Init

Allocatingcritical data structures required by your view Controller

Do not show code to create view. Good design, in Init should only the relevant data initialization, and these data are more critical data. Do not drop self.view in init, otherwise it will cause Viewcontroller to create the view. (because view is Lazyinit).

2, Loadview

Creating YourView Objects

Only the view is initialized, typically used to create a more critical view such as Tableviewcontroller's Tabview,uinavigationcontroller Navgationbar, Do not drop the getter of the view (before dropping the Superloadview), it is best not to initialize some non-critical view. If you are creating a Viewcontroller from the nib file, you must first call Super's Loadview method, but it is not recommended to overload this method.

3, Viewdidload

Allocating orloading data to being displayed in your view

Now that the view is there, it's best to create some additional view and controls. One thing to note is that Viewdidload is called multiple times (Viewcontroller may load the view multiple times, see Figure 2).

4. Viewwillappear this is typically called before the view is added to the Superview before switching animations. Here you can do some pre-display processing. For example, keyboard popup, some special process animation (such as the status bar and Navigationbar color).

5, Viewdidappear is generally used for display, after switching the animation, if there is a need for the operation, you can add the relevant code here.

6, Viewdidunload

Releasingreferences to view objects

Releasing Datathat is isn't needed when your view isn't displayed

At this time Viewcontroller view is already nil. Since this generally occurs in memory warnings, you should release the view that is not displayed here. For example, if you add a label to the Viewcontroller view, and the label is a Viewcontroller attribute, then you have to set this property to nil so as not to consume unnecessary memory. This label is re-created when Viewdidload.

7, Dealloc

Releasingcritical data structures required by your view Controller

Six, several notes:

1. By structure, all viewcontroller of iOS can be divided into two categories:

1), mainly used to display the content of the Viewcontroller, this viewcontroller is mainly used to show the user content and interaction with users, such as Uitableviewcontroller,uiviewcontroller.

2), Viewcontroller for control and display of other viewcontroller. This kind of viewcontroller is generally a viewcontroller container. such as Uinavigationcontroller,uitabbarcontroller. They all have a property: Viewcontrollers. Where Uinavigationcontroller represents a stack-type structure, push a viewcontroller or pop once, so the latter viewcontroller generally depend on the previous viewcontroller. While Uitabbarcontroller represents an array structure, each viewcontroller is tied.

The first type of viewcontroller is often inherited to show different data to the user. And the second is rarely inherited unless you really need to customize it.

2. When view is added before other view, Viewwillappear is called and then Viewdidappear is called.

When a view is moved out of another view, Viewwilldisappear is called, and then Viewdiddisappear is called.

When view is not in use and is disappeared and is subject to a memory warning, Viewcontroller will release the view and point it to nil.

3. As the controller loads the view, some view objects are automatically pointed to their corresponding iboutlet variables.

So when the view is unloaded we have to release these variables in Viewdidunload, Viewcontroller won't do it automatically.

The way to do this is to set the variable to null (note the difference between the variable release in dealloc) and note that the controller's view property is empty.

http://zhece.com

Reference:

https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ViewLoadingandUnloading/ VIEWLOADINGANDUNLOADING.HTML#//APPLE_REF/DOC/UID/TP40007457-CH10-SW10 Official document: TheView Controller Life Cycle

Life cycle analysis and use of http://xcodev.com/wordpress/?p=341 Viewcontroller

Http://szblcy.blog.163.com/blog/static/17663779201261645237281/iosview Controller loading the uninstall view process

http://blog.csdn.net/wihing/article/details/7314702 Viewcontroller life cycle and the steps to load view

The life cycle of IOS view and Viewcontroller

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.