SoFunction
Updated on 2025-04-12

iOS development tutorial: Detailed explanation of the life cycle of UIView and UIViewController

Preface

In iOS development, there are two common ways to create Views: pure code, and one is with the help of XIB; there are two common ways to create ViewController: pure code, and the other is with the help of StoryBoard.

Through communication, I found that many children's shoes are very vague about these concepts, so I wrote a blog for reference through experiments.

The following method of rewriting the View

@implementation YFView

-(instancetype)init{
 
 self = [super init];
 
 NSLog(@"%s", __func__);
 
 return self;
}

-(instancetype)initWithFrame:(CGRect)frame{
 
 self = [super initWithFrame:(CGRect)frame];
 
 NSLog(@"%s", __func__);
 
 return self;
 
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
 
 self = [super initWithCoder:aDecoder];
 
 NSLog(@"%s", __func__);
 
 return self;
 
}

-(void)awakeFromNib{
 
 [super awakeFromNib];
 
 NSLog(@"%s", __func__);
}


-(void)layoutSubviews{
 
 NSLog(@"%s", __func__);
 
}

@end

The following method of rewriting ViewController

@implementation YFViewController

-(void)loadView{
 
 NSLog(@"%s", __func__);
 
 [super loadView];
}

- (void)viewDidLoad {
 [super viewDidLoad];
 // Do any additional setup after loading the view.
 
 NSLog(@"%s", __func__);
 
  = [UIColor redColor];
}


-(void)viewDidAppear:(BOOL)animated{
 
 NSLog(@"%s", __func__);
 
 [super viewDidAppear:animated];
 
 
}

-(instancetype)init{
 
 self = [super init];
 
 NSLog(@"%s", __func__);
 
 return self;
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
 
 self = [super initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil];
 
 NSLog(@"%s", __func__);
 
 return self;
 
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
 
 self = [super initWithCoder:aDecoder];
 
 NSLog(@"%s", __func__);
 
 return self;
 
}

-(void)awakeFromNib{
 
 NSLog(@"%s", __func__);
 
 [super awakeFromNib];
}

@end

Conclusion summary

If the controller is created directly through the code and the call method is[[YFViewController alloc]init]The life cycle of creating a VC is:

-[YFViewController initWithNibName:bundle:]
-[YFViewController init]
-[YFViewController loadView]
-[YFViewController viewDidLoad]
-[YFViewController viewDidAppear:]

If XIB is checked when creating the controller, the call method is[[YFViewController alloc]init]The life cycle when creating a VC is:

-[YFViewController initWithNibName:bundle:] 
-[YFViewController init]
-[YFViewController loadView]
-[YFViewController viewDidLoad]
-[YFViewController viewDidAppear:]

If the controller is created through SB, the life cycle of the VC is:

-[ViewController initWithCoder:]
-[ViewController awakeFromNib]
-[ViewController loadView]
-[ViewController viewDidLoad]
-[ViewController viewDidAppear:]

If pure code creates a UIView, the call method isYFView *yfView = [[YFView alloc]init];Then its life cycle is:

-[YFView initWithFrame:]
-[YFView init]
-[YFView layoutSubviews]

If pure code creates a UIView, the call method isYFView *yfView = [[YFView alloc]initWithFrame:[UIScreen mainScreen].bounds];Then its life cycle is:

-[YFView initWithFrame:]
-[YFView layoutSubviews]

If you create a UIView through XIB, the call method isNSArray *array = [[NSBundle mainBundle]loadNibNamed:@"YFView" owner:nil options:nil];[array lastObject];Then its life cycle is:

-[YFView initWithCoder:]
-[YFView awakeFromNib]
-[YFView layoutSubviews]

Summarize

The above is the entire content of this article. I hope that the content of this article has a certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.