Uiviewcontroller life cycle and iOS program execution sequence

Source: Internet
Author: User

When a view controller is created and displayed on the screen. Order of execution of code
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

When a view is removed from the screen and the execution order is destroyed, the order is almost the opposite of the above
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

About Viewdidunload: In the event of a memory warning, if this view is not the view being displayed on the current screen, Viewdidunload will be executed and all child views of this view will be destroyed to free up memory, when developers need to manually viewload, The objects created in the Viewdidload free memory. Because when the view is displayed again on the screen, Viewload and viewdidload are called again to construct the view again.

When we create an object of the Uiviewcontroller class, it is common for the system to generate several default methods, most of which are related to the invocation of the view, but the order in which these methods are called when the view is called, needs to be sorted out.

Usually the above methods include the following, which are methods of the Uiviewcontroller class:

-(void) viewdidload;

-(void) viewdidunload;

-(void) Viewwillappear: (BOOL) animated;

-(void) Viewdidappear: (BOOL) animated;

-(void) Viewwilldisappear: (BOOL) animated;

-(void) Viewdiddisappear: (BOOL) animated;

The following describes the order in which the app is called at runtime.

1)-(void) viewdidload;

An app loads the view into memory first by invoking the Loadview method or by loading the initial interface created in IB. The Viewdidload method is then called to make further settings. In general, we have a lot of content of initial data loading, initial setting and so on, this method is implemented in this way, so this method is a very common, very important method.

Note, however, that this method will only be called once when the app starts to load and will not be called again, so it can only be used for initial setup.

2)-(void) viewdidunload;

In the case of sufficient memory, the view of the software is usually kept in memory, but if there is not enough memory, some viewcontroller that are not being displayed will receive a warning of insufficient memory and then release the view they own to achieve the purpose of freeing the memory. But the system only frees up memory and does not release ownership of the object, so usually we need to take ownership of objects that do not need to be kept in memory, that is, to set their pointer to nil.

This method is not usually called when the view is transformed, but only when the system exits or a memory warning is received. But since we need to make sure we can react to it when we receive a memory warning, this approach usually needs to be implemented.

In addition, even after the home key is pressed on the device, the system does not necessarily call this method, because after IOS4, the system allows the app to hang in the background and continue to be stuck in memory, so Viewcontroller does not call this method to clear the memory.

3)-(void) Viewwillappear: (BOOL) animated;

After the system loads all the data, the view is displayed on the screen, and this method is called first. We usually use this method to make further settings for the view that will be displayed. For example, we can use this method to set how the device will be displayed in different directions.

On the other hand, when the app has multiple views, switching between views does not load the Viewdidload method again, so if the data needs to be updated when the view is transferred, it can only be implemented within this method. So this method is also very common.

4)-(void) Viewdidappear: (BOOL) animated;

Sometimes, for some special reason, we cannot update the view in the Viewwillapper method. Then you can override this method to further set the view that is being displayed.

5)-(void) Viewwilldisappear: (BOOL) animated;

When the view is transformed, the current view is called when it is about to be removed or overwritten, and this method is invoked to handle and set up some aftercare.

Since after IOS4, the system allows the app to hang in the background, the system does not call this method after the home key is pressed, because the app itself, the view that the application displays, is still the view at the time of the hang, so this method is not called.

6)-(void) Viewdiddisappear: (BOOL) animated;

We can override this method and do some other work on the view that has disappeared, or is overwritten, or has been hidden.

The difference between OS development init and Loadview and Viewdidload

They can all be used to initialize some content when the view is loaded. But what difference do they have?

1.loadview must be assigned to the view of the controller, either by inheriting or instantiating one; the main function is to instantiate the view

2. Execution sequence, init->load->viewdidload

3. If the device is out of memory, the view controller receives a didreceivememorywarning message. The default implementation is to check whether the current controller's view is in use. If its view is not inside the view hierarchy currently in use and your controller implements the Loadview method, the view will be release and the Loadview method will be re-used to create a new view. So don't write any of the view-related content in init.

Func application (application:uiapplication!, Didfinishlaunchingwithoptions launchoptions:nsdictionary!)Bool {//Override point for customization after application launch.Self.window=UIWindow (Frame:UIScreen.mainScreen (). bounds); Self.window!. BackgroundColor =Uicolor.whitecolor (); Self.window!. makekeyandvisible (); varRootcontroller =Viewcontroller (Nibname:nil,bundle:nil); Self.window!. Rootviewcontroller =Rootcontroller; return true    }

classViewcontroller:uiviewcontroller {varTestview1:testview?; varTestview2:testview?; Overridefunc Loadview () {Self.view= UIView ();//can be customized or executed self.loadview (); Ensure Self.view is instantiated    }            Overridefunc viewdidload () {super.viewdidload ()varLabel = UILabel (Frame:cgrect (x:0, Y:uiscreen.mainscreen (). bounds.height/2+ -, Width: the, Height: -)); Label.text="AAAAA"; }

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.