The life cycle of Viewcontroller

Source: Internet
Author: User

First, the structure

By structure, you can divide all viewcontroller of iOS into two categories:
1, mainly used to display the content of the Viewcontroller, this viewcontroller is mainly used for users to display content and interaction with users, such as Uitableviewcontroller,uiviewcontroller.
2, for the control and display of other Viewcontroller 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.
Note: Careful classmates should be able to find, in Xcode new Viewcontroller, can only choose to inherit from Uiviewcontroller and Uitableviewcontroller, and they are the first kind.

Figure 1

Ii. life cycle of controller and view

The view referred to here 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.

Figure 2 Viewcontroller life cycle



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 view property that you call Getter:[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.
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.

Three, code organization (How to design good viewcontroller) There are so many functions in the Viewcontroller life cycle, an important question is what code should be written somewhere.
1. Do not show code to create view in init. 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 only initialize view, generally used to create a more critical view such as Tableviewcontroller Tabview,uinavigationcontroller Navgationbar, Do not swap out the view getter (before you drop the Super Loadview), 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 This time view has been, the most suitable to create some additional view and control. 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 This time Viewcontroller view is 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

The life cycle of Viewcontroller

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.