美文网首页
UIView生命周期详解

UIView生命周期详解

作者: Don_He | 来源:发表于2019-04-12 16:14 被阅读0次

    UIView生命周期详解

    View代码实现

    • 一般情况
    MyView *myView = [[MyView alloc] init];
    [self.view addSubview:myView];
    
    // 初始化过程
    2019-04-12 14:44:47.558747+0800 UIViewLifeDemo[12529:360679] Step 1 -- -[MyView initWithFrame:]
    2019-04-12 14:44:47.558957+0800 UIViewLifeDemo[12529:360679] Step 2 -- -[MyView init]
    2019-04-12 14:44:47.559035+0800 UIViewLifeDemo[12529:360679] Step 3 -- -[MyView willMoveToSuperview:]
    2019-04-12 14:44:47.559131+0800 UIViewLifeDemo[12529:360679] Step 4 -- -[MyView didMoveToSuperview]
    2019-04-12 14:44:47.569525+0800 UIViewLifeDemo[12529:360679] Step 5 -- -[MyView willMoveToWindow:]
    2019-04-12 14:44:47.569683+0800 UIViewLifeDemo[12529:360679] Step 6 -- -[MyView didMoveToWindow]
    2019-04-12 14:44:47.591882+0800 UIViewLifeDemo[12529:360679] Step 7 -- -[MyView layoutSubviews]
    
    // 销毁过程
    2019-04-12 14:44:56.431678+0800 UIViewLifeDemo[12529:360679] Step 8 -- -[MyView willMoveToWindow:]
    2019-04-12 14:44:56.431824+0800 UIViewLifeDemo[12529:360679] Step 9 -- -[MyView didMoveToWindow]
    2019-04-12 14:44:56.432235+0800 UIViewLifeDemo[12529:360679] Step 10 -- -[MyView removeFromSuperview]
    2019-04-12 14:44:56.432473+0800 UIViewLifeDemo[12529:360679] Step 11 -- -[MyView dealloc]
    
    • 重复地移除再添加到相同的SuperView,willMoveToSuperview和didMoveToSuperview不会重复调用。
    MyView *myView = [[MyView alloc] init];
        
    for (int n = 0; n < 4; n++) {
        [self.view addSubview:myView];
        [myView removeFromSuperview];
    }
    
    // 初始化过程
    2019-04-12 15:13:27.774165+0800 UIViewLifeDemo[14955:381550] Step 1 -- -[MyView initWithFrame:]
    2019-04-12 15:13:27.774380+0800 UIViewLifeDemo[14955:381550] Step 2 -- -[MyView init]
    2019-04-12 15:13:27.774450+0800 UIViewLifeDemo[14955:381550] Step 3 -- -[MyView willMoveToSuperview:]
    2019-04-12 15:13:27.774539+0800 UIViewLifeDemo[14955:381550] Step 4 -- -[MyView didMoveToSuperview]
    2019-04-12 15:13:27.774684+0800 UIViewLifeDemo[14955:381550] Step 5 -- -[MyView removeFromSuperview]
    2019-04-12 15:13:27.774773+0800 UIViewLifeDemo[14955:381550] Step 6 -- -[MyView removeFromSuperview]
    2019-04-12 15:13:27.774863+0800 UIViewLifeDemo[14955:381550] Step 7 -- -[MyView removeFromSuperview]
    2019-04-12 15:13:27.774951+0800 UIViewLifeDemo[14955:381550] Step 8 -- -[MyView removeFromSuperview]
    2019-04-12 15:13:27.782601+0800 UIViewLifeDemo[14955:381550] Step 9 -- -[MyView willMoveToWindow:]
    2019-04-12 15:13:27.782798+0800 UIViewLifeDemo[14955:381550] Step 10 -- -[MyView didMoveToWindow]
    2019-04-12 15:13:27.797559+0800 UIViewLifeDemo[14955:381550] Step 11 -- -[MyView layoutSubviews]
    
    • 重复地移除再添加到不同的SuperView,willMoveToSuperview和didMoveToSuperview会重复调用。
    MyView *myView = [[MyView alloc] init];
        
    UIView *view0 = [[UIView alloc] init];
    UIView *view1 = [[UIView alloc] init];
        
    for (int n = 0; n < 4; n++) {
        if (n % 2 == 0) {
            [view0 addSubview:myView];
        }
        else {
            [view1 addSubview:myView];
        }
        
        [myView removeFromSuperview];
    }
        
    [self.view addSubview:myView];
    
    // 初始化过程
    2019-04-12 15:21:07.953595+0800 UIViewLifeDemo[15040:386253] Step 1 -- -[MyView initWithFrame:]
    2019-04-12 15:21:07.953780+0800 UIViewLifeDemo[15040:386253] Step 2 -- -[MyView init]
    2019-04-12 15:21:07.953927+0800 UIViewLifeDemo[15040:386253] Step 3 -- -[MyView willMoveToSuperview:]
    2019-04-12 15:21:07.954039+0800 UIViewLifeDemo[15040:386253] Step 4 -- -[MyView didMoveToSuperview]
    2019-04-12 15:21:07.954126+0800 UIViewLifeDemo[15040:386253] Step 5 -- -[MyView removeFromSuperview]
    2019-04-12 15:21:07.954193+0800 UIViewLifeDemo[15040:386253] Step 6 -- -[MyView willMoveToSuperview:]
    2019-04-12 15:21:07.954276+0800 UIViewLifeDemo[15040:386253] Step 7 -- -[MyView didMoveToSuperview]
    2019-04-12 15:21:07.954390+0800 UIViewLifeDemo[15040:386253] Step 8 -- -[MyView removeFromSuperview]
    2019-04-12 15:21:07.954629+0800 UIViewLifeDemo[15040:386253] Step 9 -- -[MyView willMoveToSuperview:]
    2019-04-12 15:21:07.954850+0800 UIViewLifeDemo[15040:386253] Step 10 -- -[MyView didMoveToSuperview]
    2019-04-12 15:21:07.955045+0800 UIViewLifeDemo[15040:386253] Step 11 -- -[MyView removeFromSuperview]
    2019-04-12 15:21:07.955218+0800 UIViewLifeDemo[15040:386253] Step 12 -- -[MyView willMoveToSuperview:]
    2019-04-12 15:21:07.955427+0800 UIViewLifeDemo[15040:386253] Step 13 -- -[MyView didMoveToSuperview]
    2019-04-12 15:21:07.955629+0800 UIViewLifeDemo[15040:386253] Step 14 -- -[MyView removeFromSuperview]
    2019-04-12 15:21:07.955832+0800 UIViewLifeDemo[15040:386253] Step 15 -- -[MyView willMoveToSuperview:]
    2019-04-12 15:21:07.956073+0800 UIViewLifeDemo[15040:386253] Step 16 -- -[MyView didMoveToSuperview]
    2019-04-12 15:21:07.980502+0800 UIViewLifeDemo[15040:386253] Step 17 -- -[MyView willMoveToWindow:]
    2019-04-12 15:21:07.980657+0800 UIViewLifeDemo[15040:386253] Step 18 -- -[MyView didMoveToWindow]
    2019-04-12 15:21:07.994333+0800 UIViewLifeDemo[15040:386253] Step 19 -- -[MyView layoutSubviews]
    

    View xib实现

    • 一般情况
    // 初始化过程
    2019-04-12 15:24:56.344593+0800 UIViewLifeDemo[15131:389556] Step 1 -- -[MyView initWithCoder:]
    2019-04-12 15:24:56.344728+0800 UIViewLifeDemo[15131:389556] Step 2 -- -[MyView willMoveToSuperview:]
    2019-04-12 15:24:56.344828+0800 UIViewLifeDemo[15131:389556] Step 3 -- -[MyView didMoveToSuperview]
    2019-04-12 15:24:56.351139+0800 UIViewLifeDemo[15131:389556] Step 4 -- -[MyView willMoveToWindow:]
    2019-04-12 15:24:56.351460+0800 UIViewLifeDemo[15131:389556] Step 5 -- -[MyView didMoveToWindow]
    2019-04-12 15:24:56.364634+0800 UIViewLifeDemo[15131:389556] Step 6 -- -[MyView layoutSubviews]
    
    // 销毁过程
    2019-04-12 15:25:19.571373+0800 UIViewLifeDemo[15131:389556] Step 7 -- -[MyView willMoveToWindow:]
    2019-04-12 15:25:19.571539+0800 UIViewLifeDemo[15131:389556] Step 8 -- -[MyView didMoveToWindow]
    2019-04-12 15:25:19.572150+0800 UIViewLifeDemo[15131:389556] Step 9 -- -[MyView removeFromSuperview]
    2019-04-12 15:25:19.572476+0800 UIViewLifeDemo[15131:389556] Step 10 -- -[MyView dealloc]
    
    • 在xib中修改View的属性,initWithCoder方法中就会生效

    xib中的MyView修改了背景颜色,从日志可以看到背景颜色从-[MyView initWithCoder:]开始已经修改为#007affff。

    2019-04-12 15:54:48.920910+0800 UIViewLifeDemo[15547:409869] Step 1 -- -[MyView initWithCoder:] -- backgroundColor #007affff
    2019-04-12 15:54:48.921155+0800 UIViewLifeDemo[15547:409869] Step 2 -- -[MyView willMoveToSuperview:] -- backgroundColor #007affff
    2019-04-12 15:54:48.921288+0800 UIViewLifeDemo[15547:409869] Step 3 -- -[MyView didMoveToSuperview] -- backgroundColor #007affff
    2019-04-12 15:54:48.929106+0800 UIViewLifeDemo[15547:409869] Step 4 -- -[MyView willMoveToWindow:] -- backgroundColor #007affff
    2019-04-12 15:54:48.929429+0800 UIViewLifeDemo[15547:409869] Step 5 -- -[MyView didMoveToWindow] -- backgroundColor #007affff
    2019-04-12 15:54:48.941557+0800 UIViewLifeDemo[15547:409869] Step 6 -- -[MyView layoutSubviews] -- backgroundColor #007affff
    
    • 在Category中为View添加属性,并在xib中修改该属性,didMoveToSuperview方法执行后才会生效

    在Category中为MyView添加myText属性

    // MyView+Extension.h
    
    IB_DESIGNABLE
    
    @interface MyView (Extension)
    
    @property(nonatomic, strong) IBInspectable NSString *myText;
    
    @end
    
    
    
    // MyView+Extension.m
    
    @implementation MyView (Extension)
    
    - (void)setMyText:(NSString *)myText {
        objc_setAssociatedObject(self, (__bridge const void * _Nonnull)(self), myText, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    - (NSString *)myText {
        NSString *myText = objc_getAssociatedObject(self, (__bridge const void * _Nonnull)(self));
        return myText;
    }
    
    @end
    

    在xib中设置myText的值, -[MyView didMoveToSuperview]执行完后才会生效

    MyText.png
    2019-04-12 15:59:48.380369+0800 UIViewLifeDemo[15605:413050] Step 1 -- -[MyView initWithCoder:] -- myText (null)
    2019-04-12 15:59:48.380536+0800 UIViewLifeDemo[15605:413050] Step 2 -- -[MyView willMoveToSuperview:] -- myText (null)
    2019-04-12 15:59:48.380626+0800 UIViewLifeDemo[15605:413050] Step 3 -- -[MyView didMoveToSuperview] -- myText (null)
    2019-04-12 15:59:52.558483+0800 UIViewLifeDemo[15605:413050] Step 4 -- -[MyView willMoveToWindow:] -- myText Hello world
    2019-04-12 15:59:52.558827+0800 UIViewLifeDemo[15605:413050] Step 5 -- -[MyView didMoveToWindow] -- myText Hello world
    2019-04-12 15:59:52.574201+0800 UIViewLifeDemo[15605:413050] Step 6 -- -[MyView layoutSubviews] -- myText Hello world
    

    相关文章

      网友评论

          本文标题:UIView生命周期详解

          本文链接:https://www.haomeiwen.com/subject/abkfottx.html