SoFunction
Updated on 2025-04-13

Detailed explanation of the life cycle example of the IOS view controller

IOS View Controller

The so-called life cycle is the order of calling several functions. Here, we take the example of using Storyboard to create a ViewController.

Then we test the following code

// 
//  
// 
// Created by huangwenchen on 14/12/26. 
// Copyright (c) 2014 huangwenchen. All rights reserved.// 
 
#import "" 
 
@interface ViewController () 
 
@end 
 
@implementation ViewController 
 
- (id)initWithCoder:(NSCoder *)aDecoder{ 
  if (self = [super initWithCoder:aDecoder]) { 
    NSLog(@"initWithCoder Called"); 
  } 
  return self; 
} 
- (void)viewDidLoad { 
  [super viewDidLoad]; 
  NSLog(@"viewDidLoad Called"); 
  // Do any additional setup after loading the view, typically from a nib. 
   
} 
- (void)viewWillAppear:(BOOL)animated{ 
  [super viewWillAppear:animated]; 
  NSLog(@"viewWillAppear Called"); 
} 
 
-(void)viewDidAppear:(BOOL)animated{ 
  [super viewDidAppear:animated]; 
  NSLog(@"viewDidAppear Called"); 
 
} 
 
-(void)awakeFromNib{ 
  NSLog(@"awakeFromNib Called"); 
} 
 
-(void)viewWillLayoutSubviews{ 
  NSLog(@"viewWillLayoutSubviews Called"); 
} 
-(void)viewDidLayoutSubviews{ 
  NSLog(@"viewDidLayoutSubviews Called"); 
} 
 
@end 

Then, run on the simulator, the output is

2014-12-29 19:42:47.904 KVCForCSDN[2370:126741] initWithCoder Called 
2014-12-29 19:42:47.907 KVCForCSDN[2370:126741] awakeFromNib Called 
2014-12-29 19:42:47.927 KVCForCSDN[2370:126741] viewDidLoad Called 
2014-12-29 19:42:47.927 KVCForCSDN[2370:126741] viewWillAppear Called 
2014-12-29 19:42:47.961 KVCForCSDN[2370:126741] viewWillLayoutSubviews Called 
2014-12-29 19:42:47.963 KVCForCSDN[2370:126741] viewDidLayoutSubviews Called 
2014-12-29 19:42:47.970 KVCForCSDN[2370:126741] viewDidAppear Called 

Therefore, the call order is

1 init function (init; initWithFrame; initWithCoder; etc.) - Initialization
2 wakeFromNib - Work before loadView is put here
3 viewDidLoad - Note that this function will only be called once in a ViewController within a lifetime.
4 viewWillAppear -- The view will appear, and it will be called every time the view disappears and appears again.
5 viewWillLayoutSubviews---A brief attempt to layout the sub-sub
6 viewDidLayoutSubivews--Complete pair attempts to layout
7 viewDidAppear---The view will appear on the screen

--The above code does not contain any part

8 viewWillDisappear--View is about to disappear
9 viewDidDisappear--View has disappeared

These nine are the usual order of function calls in the life cycle of the view controller. You must be clear about what kind of code should be placed in the life cycle of the view controller.
BTY:

didReceiveMemoryWarning

This function is usually within the life cycle considerations. It will be called when there is insufficient memory. At this time, appropriate memory release should be performed, otherwise iOS will forcefully close the current APP

Thank you for reading, I hope it can help you. Thank you for your support for this site!