Uiviewcontroller life cycle and method invocation order

Source: Internet
Author: User

    • Viewcontroller parsing

Uiviewcontroller is the carrier and controller of iOS top-level view, the interaction between user and program interface is controlled by Uiviewcontroller, Uiviewcontroller manages the life cycle of UIView and the load and release of resources.

UIView and UIWindow together demonstrate the application's user interface. UIView can be understood as a canvas, UIWindow understood as a picture frame. The inheritance of these two classes is this:

Nsobject-uiresponder-uiview-uiwindow (UIWindow is a special kind of UIView, usually only one UIWindow in an app)

In iOS, all objects that appear on the interface are inherited directly or indirectly from Uiresponder, and UIView and UIWindow are no exception.

You can imagine the relationship between them as a scene: first there will be an empty frame (UIWindow), we place a canvas (UIView) on the frame, and then we can draw on this canvas (UIView), the canvas may be painted on various elements, such as Uilabel, UIButton and so on. These elements are also a uiview, they will have a hierarchical relationship management, a bit equivalent to the concept of Photoshop layer, high-level elements will cover the low level of elements, resulting in low-level elements are partially or completely obscured.

    • Viewcontroller classification

iOS Viewcontroller can be divided into two categories:

    1. mainly used to show the viewcontroller of content, This viewcontroller is primarily used to present content to the user and interact with the user, such as uitableviewcontroller , uiviewcontroller .
    2. uinavigationcontroller , uitabbarcontroller . They all have a property: Viewcontrollers. Where Uinavigationcontroller is a stack-type structure, push into a viewcontroller or pop out of a viewcontroller, Therefore, the latter viewcontroller will generally rely 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.
When you create a new Viewcontroller in Xcode, you can choose to inherit from Uiviewcontroller and Uitableviewcontroller only, and they are the first.

    • Viewcontroller Code Execution process

When a view controller is created, the order in which the code is executed
1, Alloc create objects, allocate space
2. Init (Initwithnibname) initializes the object, initializes the data
3, Loadview from the nib load view, usually this step does not need to interfere. Unless you're not using the Xib file to create a view
4, Viewdidload loading complete, can be customized data and dynamic creation of other controls
5, Viewwillappear View will appear before the screen, the view will be displayed on the screen immediately
6, Viewdidappear view has been rendered on the screen completed

The order of execution when a view is removed from the screen and destroyed
1. Viewwilldisappear view will be removed from the screen before execution
2, Viewdiddisappear view has been removed from the screen, the user can not see this view
3. Dealloc view is destroyed, you need to release the objects you created in Init and viewdidload

Such as:

    • Description

1. When you alloc and init a viewcontroller, the Viewcontroller should not have created a view. Viewcontroller view is created using the Lazyinit (lazy Loading) method.

2. When you invoke the Getter method of the View property: [Self view]. The getter will first determine if the view is created, and if not, it will call Loadview to create the view.

3. 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.

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

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

6. 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.

Uiviewcontroller life cycle and method invocation order

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.