一、介绍
前面介绍了VC的生命周期,闲着没事也来捋一捋View的生命周期,简单用两个类型的View来监测。一个View纯代码创建,另一个View使用Xib创建。
二 、代码
MyCodeView:
// // MyCodeView.m // 生命周期 // // Created by 夏远全 on 2019/11/3. // Copyright © 2019 Beijing Huayue Education Technology Co., Ltd. All rights reserved. // #import "MyCodeView.h" @implementation MyCodeView - (instancetype)initWithFrame:(CGRect)frame { NSLog(@"myCodeView-------%s",__func__); return [super initWithFrame:frame]; } - (nullable instancetype)initWithCoder:(NSCoder *)coder { NSLog(@"myCodeView-------%s",__func__); return [super initWithCoder:coder]; } -(void)awakeFromNib { NSLog(@"myCodeView-------%s",__func__); return [super awakeFromNib]; } -(instancetype)init { NSLog(@"myCodeView-------%s",__func__); return [super init]; } - (void)willMoveToSuperview:(nullable UIView *)newSuperview { NSLog(@"myCodeView-------%s",__func__); [super willMoveToSuperview:newSuperview]; } - (void)didMoveToSuperview { NSLog(@"myCodeView-------%s",__func__); [super didMoveToSuperview]; } - (void)willMoveToWindow:(nullable UIWindow *)newWindow { NSLog(@"myCodeView-------%s",__func__); [super willMoveToWindow:newWindow]; } - (void)didMoveToWindow { NSLog(@"myCodeView-------%s",__func__); [super didMoveToWindow]; } - (void)layoutSubviews { NSLog(@"myCodeView-------%s",__func__); [super layoutSubviews]; } - (void)drawRect:(CGRect)rect { NSLog(@"myCodeView-------%s",__func__); [super drawRect:rect]; } -(void)dealloc { NSLog(@"myCodeView-------%s",__func__); } @end
MyXibView:
// // MyXibView.m // 生命周期 // // Created by 夏远全 on 2019/11/3. // #import "MyXibView.h" @implementation MyXibView - (instancetype)initWithFrame:(CGRect)frame { NSLog(@"myXibView-------%s",__func__); return [super initWithFrame:frame]; } - (nullable instancetype)initWithCoder:(NSCoder *)coder { NSLog(@"myXibView-------%s",__func__); return [super initWithCoder:coder]; } -(void)awakeFromNib { NSLog(@"myXibView-------%s",__func__); return [super awakeFromNib]; } -(instancetype)init { NSLog(@"myXibView-------%s",__func__); return [super init]; } - (void)willMoveToSuperview:(nullable UIView *)newSuperview { NSLog(@"myXibView-------%s",__func__); [super willMoveToSuperview:newSuperview]; } - (void)didMoveToSuperview { NSLog(@"myXibView-------%s",__func__); [super didMoveToSuperview]; } - (void)willMoveToWindow:(nullable UIWindow *)newWindow { NSLog(@"myXibView-------%s",__func__); [super willMoveToWindow:newWindow]; } - (void)didMoveToWindow { NSLog(@"myXibView-------%s",__func__); [super didMoveToWindow]; } - (void)layoutSubviews { NSLog(@"myXibView-------%s",__func__); [super layoutSubviews]; } - (void)drawRect:(CGRect)rect { NSLog(@"myXibView-------%s",__func__); [super drawRect:rect]; } -(void)dealloc { NSLog(@"myCodeView-------%s",__func__); } @end
三 、测试
MyCodeView:
- (void)viewDidLoad { [super viewDidLoad]; NSLog(@"创建过程:"); _codeView = [[MyCodeView alloc] init];
[self.view addSubview:_codeView]; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSLog(@"移除过程:"); [self.codeView removeFromSuperview]; [self setCodeView:nil]; }
2019-11-03 18:53:48.017455+0800 生命周期[7776:582010] 创建过程: 2019-11-03 18:53:48.017693+0800 生命周期[7776:582010] myCodeView--------[MyCodeView init] 2019-11-03 18:53:48.017831+0800 生命周期[7776:582010] myCodeView--------[MyCodeView initWithFrame:] 2019-11-03 18:53:48.018089+0800 生命周期[7776:582010] myCodeView--------[MyCodeView willMoveToSuperview:] 2019-11-03 18:53:48.018259+0800 生命周期[7776:582010] myCodeView--------[MyCodeView didMoveToSuperview] 2019-11-03 18:53:48.030801+0800 生命周期[7776:582010] myCodeView--------[MyCodeView willMoveToWindow:] 2019-11-03 18:53:48.031085+0800 生命周期[7776:582010] myCodeView--------[MyCodeView didMoveToWindow] 2019-11-03 18:53:48.035874+0800 生命周期[7776:582010] myCodeView--------[MyCodeView layoutSubviews] 2019-11-03 18:53:49.043080+0800 生命周期[7776:582010] 移除过程: 2019-11-03 18:53:49.043505+0800 生命周期[7776:582010] myCodeView--------[MyCodeView willMoveToSuperview:] 2019-11-03 18:53:49.043670+0800 生命周期[7776:582010] myCodeView--------[MyCodeView willMoveToWindow:] 2019-11-03 18:53:49.043856+0800 生命周期[7776:582010] myCodeView--------[MyCodeView didMoveToWindow] 2019-11-03 18:53:49.043977+0800 生命周期[7776:582010] myCodeView--------[MyCodeView didMoveToSuperview] 2019-11-03 18:53:49.044246+0800 生命周期[7776:582010] myCodeView--------[MyCodeView dealloc]
MyXibView:
- (void)viewDidLoad { [super viewDidLoad]; NSLog(@"创建过程:"); _xibView = [[NSBundle mainBundle] loadNibNamed:@"MyXibView" owner:self options:nil][0]; [self.view addSubview:_xibView]; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSLog(@"移除过程:"); [self.xibView removeFromSuperview]; [self setXibView:nil]; }
2019-11-03 18:54:37.271733+0800 生命周期[7807:583483] 创建过程: 2019-11-03 18:54:37.272366+0800 生命周期[7807:583483] myXibView--------[MyXibView initWithCoder:] 2019-11-03 18:54:37.272802+0800 生命周期[7807:583483] myXibView--------[MyXibView awakeFromNib] 2019-11-03 18:54:37.273005+0800 生命周期[7807:583483] myXibView--------[MyXibView willMoveToSuperview:] 2019-11-03 18:54:37.273185+0800 生命周期[7807:583483] myXibView--------[MyXibView didMoveToSuperview] 2019-11-03 18:54:37.285532+0800 生命周期[7807:583483] myXibView--------[MyXibView willMoveToWindow:] 2019-11-03 18:54:37.285846+0800 生命周期[7807:583483] myXibView--------[MyXibView didMoveToWindow] 2019-11-03 18:54:37.291993+0800 生命周期[7807:583483] myXibView--------[MyXibView layoutSubviews] 2019-11-03 18:54:37.292182+0800 生命周期[7807:583483] myXibView--------[MyXibView layoutSubviews] 2019-11-03 18:54:37.292659+0800 生命周期[7807:583483] myXibView--------[MyXibView drawRect:] 2019-11-03 18:54:38.979328+0800 生命周期[7807:583483] 移除过程: 2019-11-03 18:54:38.979581+0800 生命周期[7807:583483] myXibView--------[MyXibView willMoveToSuperview:] 2019-11-03 18:54:38.979720+0800 生命周期[7807:583483] myXibView--------[MyXibView willMoveToWindow:] 2019-11-03 18:54:38.979915+0800 生命周期[7807:583483] myXibView--------[MyXibView didMoveToWindow] 2019-11-03 18:54:38.980028+0800 生命周期[7807:583483] myXibView--------[MyXibView didMoveToSuperview] 2019-11-03 18:54:38.980257+0800 生命周期[7807:583483] myCodeView--------[MyXibView dealloc]
四 、总结
- 不论是纯代码的创建View还是xib创建的View,在添加到容器中和从容器中移除时,都会调用willMoveToSuperview、didMoveToSuperview、willMoveToWindow、didMoveToWindow这四个方法,只不过调用顺序有所变化;
- 使用纯代码创建View时,只要调用了init方法,就一定会调用initWithFrame方法,会调用一次布局;
- 使用Xib创建的View时,需要进行反序列化和唤醒,也即调用initWithCoder、awakeFromNib方法, 会调用两次布局,然后进行绘制;