iOS:捋一遍View的生命周期

1、介绍ide

前面介绍了VC的生命周期,闲着没事也来捋一捋View的生命周期,简单用两个类型的View来监测。一个View纯代码建立,另外一个View使用Xib建立。布局

 

二 、代码测试

MyCodeView: spa

//
//  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
View Code

MyXibView: code

//
//  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
View Code

 

三 、注释对象

 init: 经过init实例化时调用,最终会调用initWithFrame,默认使用frame为CGRectZero

 initWithFrame: 给出一个frame进行实例化时,会调用

 initWithCoder: 经过xib文件建立时,进行反序列化调用

 awakeFromNib: 经过xib反序列化后唤醒,初始化调用

 willMoveToSuperview: 在视图即将加入或者移除时调用,若是参数newSuperview有值,则为即将加入到父视图,若是为空值,则即将从父视图移除

 didMoveToSuperview: 在视图即彻底加入或者移除时调用,若是参数newSuperview有值,则为彻底加入到父视图,若是为空值,则彻底从父视图移除

 willMoveToWindow: 在视图即将加入或者移除时调用,若是参数newWindow有值,则为即将加入到窗体,若是为空值,则即将从窗体移除

 didMoveToWindow: 在视图彻底加入或者移除时调用,若是参数newWindow有值,则为彻底加入到窗体,若是为空值,则彻底从窗体移除

 layoutSubviews: 对全部的子视图进行布局时调用

 drawRect: 经过xib文件建立的view在结束布局后进行绘制,此时调用

 dealloc: 当前视图从父视图移除并置为空时,对象销毁调用

 

四 、测试blog

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:rem

- (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]

 

五 、总结it

  • 不管是纯代码的建立View仍是xib建立的View,在添加到容器中和从容器中移除时,都会调用willMoveToSuperview、didMoveToSuperview、willMoveToWindow、didMoveToWindow这四个方法,只不过调用顺序有所变化;
  • 使用纯代码建立View时,只要调用了init方法,就必定会调用initWithFrame方法,会调用一次布局;
  • 使用Xib建立的View时,须要进行反序列化和唤醒,也即调用initWithCoder、awakeFromNib方法, 会调用两次布局,而后进行绘制;
相关文章
相关标签/搜索