UIView的生命周期

将相关代码添加打印。

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

//通过代码创建控件就会调用这个方法
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        NSLog(@"%s",__func__);
    }
    return self;
}

//通过storyboared或者xib中创建控件就会调用这个方法
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        NSLog(@"%s",__func__);
    }
    return self;
}

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

//如果在initWithFrame中添加子视图会调用两次
- (void)layoutSubviews{
    [super layoutSubviews];
        NSLog(@"%s",__func__);
}

- (void)didAddSubview:(UIView *)subview{
    [super didAddSubview:subview];
        NSLog(@"%s",__func__);
}

- (void)willRemoveSubview:(UIView *)subview{
    [super willRemoveSubview:subview];
        NSLog(@"%s",__func__);
}

- (void)willMoveToSuperview:(nullable UIView *)newSuperview{
    [super willMoveToSuperview:newSuperview];
        NSLog(@"%s",__func__);
}

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

- (void)willMoveToWindow:(nullable UIWindow *)newWindow{
    [super willMoveToWindow:newWindow];
        NSLog(@"%s",__func__);
}

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

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

- (void)dealloc{
        NSLog(@"%s",__func__);
}
当创建view时
2017-11-06 10:35:12.347153+0800 IOSLife[7587:2353869] -[MyView willMoveToSuperview:]
2017-11-06 10:35:12.347312+0800 IOSLife[7587:2353869] -[MyView didMoveToSuperview]
2017-11-06 10:35:12.353483+0800 IOSLife[7587:2353869] -[MyView willMoveToWindow:]
2017-11-06 10:35:12.353644+0800 IOSLife[7587:2353869] -[MyView didMoveToWindow]
2017-11-06 10:35:12.363861+0800 IOSLife[7587:2353869] -[MyView layoutSubviews]
2017-11-06 10:35:12.655946+0800 IOSLife[7607:2356750] -[MyView drawRect:]

当view销毁时
2017-11-06 10:41:28.152448+0800 IOSLife[7607:2356750] -[MyView willMoveToWindow:]
2017-11-06 10:41:28.152693+0800 IOSLife[7607:2356750] -[MyView didMoveToWindow]
2017-11-06 10:41:28.155160+0800 IOSLife[7607:2356750] -[MyView willMoveToSuperview:]
2017-11-06 10:41:28.155281+0800 IOSLife[7607:2356750] -[MyView didMoveToSuperview]
2017-11-06 10:41:28.155336+0800 IOSLife[7607:2356750] -[MyView removeFromSuperview]
2017-11-06 10:41:28.155399+0800 IOSLife[7607:2356750] -[MyView dealloc]

可以看出上面方法中只会执行一次的方法有removeFromSuperviewdealloc两个方法(layoutSubviews在子视图布局变动时会多次调用),所以可以在这两个方法中执行释放内存等操作(e.g:移除观察者,定时器等)。

给view添加子视图时
2017-11-06 10:45:47.749310+0800 IOSLife[7614:2358053] -[MyView didAddSubview:]
2017-11-06 10:45:47.750111+0800 IOSLife[7614:2358053] -[MyView layoutSubviews]

didAddSubview:willRemoveSubview:需要有子视图才能执行。

此时再销毁该view
2017-11-06 10:46:28.022473+0800 IOSLife[7617:2358497] -[MyView willMoveToSuperview:]
2017-11-06 10:46:28.022643+0800 IOSLife[7617:2358497] -[MyView didMoveToSuperview]
2017-11-06 10:46:28.031533+0800 IOSLife[7617:2358497] -[MyView willMoveToWindow:]
2017-11-06 10:46:28.031738+0800 IOSLife[7617:2358497] -[MyView didMoveToWindow]
2017-11-06 10:46:28.036048+0800 IOSLife[7617:2358497] -[MyView layoutSubviews]
2017-11-06 10:46:28.719103+0800 IOSLife[7617:2358497] -[MyView didAddSubview:]
2017-11-06 10:46:28.719873+0800 IOSLife[7617:2358497] -[MyView layoutSubviews]
2017-11-06 10:46:30.529769+0800 IOSLife[7617:2358497] -[MyView willMoveToWindow:]
2017-11-06 10:46:30.530059+0800 IOSLife[7617:2358497] -[MyView didMoveToWindow]
2017-11-06 10:46:30.532931+0800 IOSLife[7617:2358497] -[MyView willMoveToSuperview:]
2017-11-06 10:46:30.533055+0800 IOSLife[7617:2358497] -[MyView didMoveToSuperview]
2017-11-06 10:46:30.533112+0800 IOSLife[7617:2358497] -[MyView removeFromSuperview]
2017-11-06 10:46:30.533183+0800 IOSLife[7617:2358497] -[MyView dealloc]
2017-11-06 10:46:30.533256+0800 IOSLife[7617:2358497] -[MyView willRemoveSubview:]

willRemoveSubview是在dealloc后面执行的。如果有多个子视图,willRemoveSubview会循环执行,直到移除所有子视图。

注意:

  • - (void)willMoveToSuperview:(nullable UIView *)newSuperview;- (void)willMoveToWindow:(nullable UIWindow *)newWindow;这俩个方法可以根据参数判断,nil则为销毁,否则为创建;
  • - (void)didMoveToSuperview;- (void)didMoveToWindow;可以根据self.superview判断,nil则为销毁,否则为创建。
  • 如果在initWithFrame中有view的初始化相关操作,layoutSubviews会调用两次。layoutSubviews只进行布局相关处理,不能有view初始化操作(当子视图布局改变时会调用,能多次调用),下面是layoutSubviews的触发条件:
    1、init初始化不会触发layoutSubviews,但是是用initWithFrame进行初始化时,当rect的值不为CGRectZero时,也会触发。
    2、addSubview会触发layoutSubviews
    3、设置view的Frame会触发layoutSubviews,当然前提是frame的值设置前后发生了变化。
    4、滚动一个UIScrollView会触发layoutSubviews
    5、旋转Screen会触发父UIView上的layoutSubviews
    6、改变一个UIView大小的时候也会触发父UIView上的layoutSubviews

你可能感兴趣的:(UIView的生命周期)