Life cycle Issues with iOS

Source: Internet
Author: User

Android must have a clear Android lifecycle to take control of the program's framework, making the whole project clearer and smoother, so iOS has to understand the life cycle of iOS as well.

Let's start with a simple example.

The contents of the APPDELEGATE.M file are as follows:

appdelegate.m//switchview////Created by Pishum on 15/5/5.//Copyright (c) 2015 Pishum. All rights reserved.//#import "AppDelegate.h" #import "ViewController.h" @interface appdelegate () {//Viewcontroller *ma in;//Uinavigationcontroller *nav;} @end @implementation appdelegate-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (    Nsdictionary *) launchoptions {//Override point for customization after application launch.            NSLog (@ "APP didfinishlaunchingwithoptions"); Self.window = [[UIWindow alloc] Initwithframe:[[uiscreen Mainscreen] bounds]];////Override point for Customizat Ion after application launch.////_storyboard = [Uistoryboard storyboardwithname:@ "Main" bundle:nil];////M Ain = [_storyboard instantiateviewcontrollerwithidentifier:@ "main"];//nav = [[Uinavigationcontroller alloc] InitWithR        ootviewcontroller:main];//[Self.window AddSubview:nav.view]; return YES;} -(void) ApplicationwillrEsignactive: (uiapplication *) application {//Sent when the "application" is "about" to "move from active to inactive state." This can occur for certain types of temporary interruptions (such as a incoming phone call or SMS message) or when the U    Ser quits the application and it begins the transition to the background state. Use the This method to the pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates.        Games should use this method to pause the game. NSLog (@ "APP applicationwillresignactive");} -(void) Applicationdidenterbackground: (uiapplication *) application {//Use the method to release shared resources, SA  ve user data, invalidate timers, and store enough application state information to restore your application    State-in-case it is terminated later. If your application supports background execution, this method is called instead of Applicationwillterminate:when the     User quits. NSLog (@ "APP applicationdidenterbackground");} -(void) Applicationwillenterforeground: (uiapplication *) application {//called as part of the transition from the BACKGR Ound to the inactive state;     Here's can undo many of the changes made on entering the background. NSLog (@ "APP applicationwillenterforeground");} -(void) Applicationdidbecomeactive: (uiapplication *) application {//Restart any tasks this were paused (or not yet STA rted) While the application was inactive.     If the application is previously in the background, optionally refresh the user interface. NSLog (@ "APP applicationdidbecomeactive");} -(void) Applicationwillterminate: (uiapplication *) application {//called when the application was about to terminate. S Ave data if appropriate.         See also Applicationdidenterbackground:. NSLog (@ "APP applicationwillterminate");} @end

The contents of VIEWCONTROLLER.M are as follows

  viewcontroller.m//  switchview////  Created by Pishum on 15/5/5.//  Copyright (c) 2015 Pishum. All rights reserved.//#import "AppDelegate.h" #import "ViewController.h" #import "SecondViewController.h" #import " TestXXX.h "@interface Viewcontroller () {} @end @implementation viewcontroller-(void) Viewdidload {    [ Super viewdidload];    Uibarbuttonitem *backbutton = [[Uibarbuttonitem alloc]init];    Backbutton.title = @ "return list";    Self.navigationItem.backBarButtonItem = backbutton;    Do any additional setup after loading the view, typically from a nib.     NSLog (@ "A  &nbs P Viewdidload ");    }-(void) didreceivememorywarning {    [Super didreceivememorywarning];   //Dispose of any of the resources that can recreated.} -(Ibaction) Startnewview: (ID) sender {      uibutton* btn = (uibutton*) sender;    switch (btn.tag) {        case 1://             [Self performseguewithidentifier:@ "second" sender:self];//             NSLog (@ "onclick  ==%@", Self.navigationcontroller);             break;             //        default://             break;   }    }-(void) Prepareforsegue: ( Uistoryboardsegue *) Segue Sender: (ID) Sender {        if ([[Segue identifier] isequaltostring:@ "Second"]) { //        id object = @ "Pass in a value";         testxxx *object = [[Testxxx alloc]init];        ObjecT.name = @ "xxx";        [email protected] "all";         [[Segue Destinationviewcontroller] setprevdata:object];   }}-(void) viewdidunload{    NSLog (@ "a   viewdidunload");} -(void) Viewwillappear: (BOOL) animated{    NSLog (@ "a   viewwillappear");} -(void) Viewdidappear: (BOOL) animated{    NSLog (@ "a   viewdidappear");} -(void) Viewwilldisappear: (BOOL) animated{    NSLog (@ "a   viewwilldisappear");} -(void) Viewdiddisappear: (BOOL) animated{    NSLog (@ "a   viewdiddisappear");} @end
The contents of SECONDVIEWCONTROLLER.M are as follows

secondviewcontroller.m//switchview////Created by Pishum on 15/5/5.//Copyright (c) 2015 Pishum. All rights reserved.//#import "SecondViewController.h" @interface Secondviewcontroller () {} @end @implementation    Secondviewcontroller//testxxx *test;//nsstring* str2;-(void) viewdidload {[Super viewdidload];        Do any additional setup after loading the view.    NSLog (@ "B viewdidload str=%@", _str); NSLog (@ "B name=%@ age=%@", _test.name,_test.age);}    -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} /* #pragma mark-navigation//in a storyboard-based application, you'll often want to do a little preparation before Nav igation-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender {//Get the new view controller using [SEG    UE Destinationviewcontroller]. Pass the selected object to the new view Controller.}   */-(Ibaction) Back: (ID) Sender {NSLog (@ "clicked the second Btn"); [Self diSmissviewcontrolleranimated:yes Completion:nil];}    -(ID) Setprevdata: (id) sender{_str = sender;        _test = sender;    NSLog (@ "Output last interface to get information ====%@%@%@", sender,_test.name,_test.age); return sender;} -(void) Settheprevdata: (testxxx*) sender{_test = sender;//return sender;} -(void) viewdidunload{NSLog (@ "B viewdidunload");} -(void) Viewwillappear: (BOOL) animated{NSLog (@ "B viewwillappear");} -(void) Viewdidappear: (BOOL) animated{NSLog (@ "B viewdidappear");} -(void) Viewwilldisappear: (BOOL) animated{NSLog (@ "B viewwilldisappear");} -(void) Viewdiddisappear: (BOOL) animated{NSLog (@ "B viewdiddisappear");} @end

The above code is very clear, the purpose is to create a boot after the interface contains a button, and then click to enter the second interface to


Here is the result of the validation:

Uiviewcontroller life cycle

-(void) viewdidload;

-(void) viewdidunload;

-(void) Viewwillappear: (BOOL) animated; (Very common)

-(void) Viewdidappear: (BOOL) animated; Update view

-(void) Viewwilldisappear: (BOOL) animated; about to disappear.

-(void) Viewdiddisappear: (BOOL) animated; O has disappeared.

Start
APP didfinishlaunchingwithoptions
A Viewdidload
A Viewwillappear
A Viewdidappear
APP applicationdidbecomeactive


Click the Home button
APP applicationwillresignactive
APP Applicationdidenterbackground

Back in.
APP Applicationwillenterforeground
APP applicationdidbecomeactive


Click on the button to enter the B interface
B Viewdidload
A Viewwilldisappear
B Viewwillappear
B Viewdidappear
A Viewdiddisappear


Click on the second btn to return to the A interface
B Viewwilldisappear
A Viewwillappear
A Viewdidappear
B Viewdiddisappear


Double-click Home in the B interface and clean up the program
APP applicationwillresignactive
APP Applicationdidenterbackground
B Viewwilldisappear
B Viewdiddisappear
APP applicationwillterminate

It is clear from the results above that clicking the Home button has no effect on the life cycle of the Uiviewcontroller.

Some methods are found only when the interface is switched.


I do not put a test, personally experience the situation, very useful

Project Source Address

Https://github.com/pishum/SwitchView



This article original, reproduced please indicate the source

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Life cycle Issues with iOS

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.