"Xamarin develops IOS--ios Viewcontroller life cycle"

Source: Internet
Author: User

Viewcontroller

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.

Load of view

As you can see, there are 2 ways to create a view in a controller, one is to use a visualization tool such as storyboard, and the other is to create it through code. Visual creation is rarely used in engineering because it doesn't meet the hungry needs of developers--。

Code Creation Interface File

1. Create a new empty application Project

2. New Viewcontroller class, add Loadview method, and Viewdidload method

////XYZVIEWCONTROLLER.M//viewlifecycle////Created by Norcy on 14-7-28.//Copyright (c) 2014 qqlive. All rights reserved.//#import "XYZViewController.h"@interfaceXyzviewcontroller ()@end@implementationXyzviewcontroller- (ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) nibbundleornil{ Self=[Super Initwithnibname:nibnameornil Bundle:nibbundleornil]; if(self) {//Custom Initialization    }    returnSelf ;}- (void) loadview{UIView*contentview =[[UIView alloc] Initwithframe:[[uiscreen Mainscreen] applicationframe]; Contentview.backgroundcolor=[Uicolor Bluecolor]; Self.view=Contentview;}- (void) viewdidload{[Super Viewdidload]; //Do any additional setup after loading the view.NSLog (@"View did Load");}- (void) Viewwillappear: (BOOL) animated{NSLog (@"View would Appear");}- (void) Viewdidappear: (BOOL) animated{NSLog (@"View did Appear");}- (void) Viewwilldisappear: (BOOL) animated{NSLog (@"View would disappear");}- (void) Viewdiddisappear: (BOOL) animated{NSLog (@"View did disappear");}@end
View Code

3. Application:didfinishlaunchingwithoptions in APPDELEGATE.M: Register Viewcontroller

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions{     = [[UIWindow alloc    ] initwithframe:[[uiscreen mainscreen] bounds]; // Override point for customization after application launch.    Self.window.backgroundColor = [Uicolor whitecolor];     *viewcontroller = [[Xyzviewcontroller Alloc]initwithnibname:nil Bundle:nil];     = Viewcontroller;    [Self.window makekeyandvisible];     return YES;}
View Code

Although we can add a view after the code in the APPDELEGATE.M in Application:didfinishlaunchingwithoptions: To set the window, but in general engineering, We do not manage our view in the delegate class. Instead, use the UIWindow in the delegate class to add Uiviewcontroller, and then manage the view in the Viewcontroller class.

Attached, create code with Navigationcontroller

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions{Self.window=[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]; Self.window.backgroundColor=[Uicolor Whitecolor]; /*setting up the navigation controller*/Xyzfirstviewcontroller*viewcontroller =[[Xyzfirstviewcontroller alloc] Initwithnibname:nil Bundle:nil]; Uinavigationcontroller*nav =[[Uinavigationcontroller alloc] initwithrootviewcontroller:viewcontroller];    [Nav Setnavigationbarhidden:yes Animated:no]; Self.window.rootViewController=nav;    [Self.window makekeyandvisible];    [Viewcontroller release];    [Nav release]; returnYES;}
View Code

Viewcontroller life cycle

Viewwillappear, Viewdidappear, Viewdidload, Loadview, Initwithnibname, Alloc, Dealloc, Ear, viewdiddisappear

Note that Viewwillunload and viewdidunload have been abandoned in IOS6 because clearing references to the views are no longer necessary.

Note 1. No viewwillload.

Note that 2.viewDidLoad and viewdidunload are not paired.

Start the program

2014-07-28 17:43:36.124 viewlifecycle[4007:a0b] View did Load
2014-07-28 17:43:36.125 viewlifecycle[4007:a0b] View would Appear
2014-07-28 17:43:36.128 viewlifecycle[4007:a0b] View did Appear

Press the home key, and there is no record

Double-click the Home key to remove the program
2014-07-28 17:43:51.327 viewlifecycle[4007:a0b] View would disappear
2014-07-28 17:43:51.327 viewlifecycle[4007:a0b] View did disappear

Why didn't you call Viewwilldisappear and viewdiddisappear after you pressed the home button?

Because the concept of backstage was introduced after iOS4, when the home button was pressed, the program was suspended, but the view is still the original view, not new. Viewwilldisappear and Viewdiddisappear are only called when there is insufficient memory or when the program is terminated.

Uninstall of view

Loadview V.s. Viewdidload

When the nib file for view is nil, the Loadview is called when manually creating the view interface, and when the view's nib file exists, the initialization is done in Viewdidload.

The view has not been generated at loadview time, and when the view is Viewdidload, it has been generated. The Loadview method is used to create a new view. Loadview will only be called once, and Viewdidload may be called multiple times.

When a memory warning is present, the system default action checks that the current view controller's view is still in use, and if it is not in use and the controller implements the Loadview method, Viewcontroller will view release and point it to nil.

Note that you do not call the parent class method [Super Loadview] in Loadview, because this affects CPU performance.

Note 2, switching the front and rear stations does not call Viewwillappear

Reference articles

iOS Learning Note--viewcontroller life cycle detailed

iOS Development Interface life cycle (view lifecycle)

"Xamarin develops IOS--ios Viewcontroller life cycle"

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.