chevron-up bell reply instagram twitter2 feed3 finder search-25px-p0

Are you familar with viewWillAppear, viewDidAppear, viewDidLoad, viewWillDisappear and viewDidDisappear?

2017-04-14

Before this, let me ask you what is the running order of viewWillAppear, viewDidAppear, viewDidLoad, viewWillDisappear and viewDidDisappear?

If you know the answer, please close this webpage and contine your game. 🙂

So a simple piece code is written to verify this:

Example code:

//
//  ViewController.m
//  TestBlock
//
//  Created by Ben Liu on 14/4/17.
//  Copyright © 2017 Ben Liu. All rights reserved.
//

#import "ViewController.h"

@interface ViewController (){
    UIViewController *anotherView;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"viewDidLoad is running");

    anotherView = [UIViewController new];
    anotherView.view.backgroundColor = [UIColor redColor];

}


-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"viewWillAppear is running");
}

-(void)viewDidAppear:(BOOL)animated{
    NSLog(@"viewDidAppear is running");
}


-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    NSLog(@"viewWillDisappear is running");
}


-(void)viewDidDisappear:(BOOL)animated{
     NSLog(@"viewDidDisappear is running");
}


- (IBAction)onClickBtn:(id)sender {
    [self presentViewController:anotherView animated:YES completion:nil];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

Running results:

2017-04-14 08:18:18.923 TestBlock[3940:264012] viewDidLoad is running
2017-04-14 08:18:18.924 TestBlock[3940:264012] viewWillAppear is running
2017-04-14 08:18:18.946 TestBlock[3940:264012] viewDidAppear is running
2017-04-14 08:18:22.187 TestBlock[3940:264012] viewWillDisappear is running
2017-04-14 08:18:22.689 TestBlock[3940:264012] viewDidDisappear is running

After you click the button, you will see the order:

  1. viewDidLoad
  2. viewWillAppear
  3. viewDidAppear
  4. viewWillDisappear
  5. viewDidDisappear

More thoughts on this:

viewDidLoad: This method might be the most frequently used method while developing, normally we initialize data objects and controls. It will create all the necessary memory for all controls/data objects for this view. i.e. In the above case, anotherView and btnView, they will keep the same memory addresses for the whole life cycle.

viewWillAppear: Called before the view is added to the windows’ view hierarchy. So it is ideal for updating the viewcontroller's data.

viewDidAppear: Called after the view is added to the windows' view hierarchy.

viewWillDisappear: Called before the view is removed from the windows’ view hierarchy.

viewDidDisappear: Called after the view is removed from the windows’ view hierarchy.

Notice: There also used to be a method called viewDidUnload, it has been deprecated since the iOS 6, which used to do some final clean ups.


Reference:

Code on Gist in Objectiv-C
Code on Gist in Swift
https://developer.apple.com/reference/uikit/uiviewcontroller

arkilis

Make Your Comments