前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS View 生命周期

iOS View 生命周期

作者头像
赵哥窟
发布2019-08-29 10:24:24
1.4K0
发布2019-08-29 10:24:24
举报
文章被收录于专栏:日常技术分享日常技术分享

现在来看看View的生命周期

显示过程

-(void)willMoveToSuperview:(UIView *)newSuperview -(void)didMoveToSuperview

-(void)willMoveToWindow:(UIWindow *)newWindow -(void)didMoveToWindow

-(void)layoutSubviews

移除过程

-(void)willMoveToSuperview:(UIView *)newSuperview -(void)willMoveToWindow:(UIWindow *)newWindow

-(void)didMoveToWindow -(void)didMoveToSuperview

-(void)dealloc

验证

代码语言:javascript
复制
#import "TestView.h"

@implementation TestView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self){
        NSLog(@"initWithFrame");
    }
    return self;
}

//(superview)
- (void)willMoveToSuperview:(UIView *)newSuperview{
     NSLog(@"willmovetosuperview");
    [super willMoveToSuperview:newSuperview];
}

- (void)didMoveToSuperview{
    NSLog(@"didmovetosuperview");
    [super didMoveToSuperview];
}

//(window)
- (void)willMoveToWindow:(UIWindow *)newWindow{
     NSLog(@"willmovetowindow");
    [super willMoveToWindow:newWindow];
}

- (void)didMoveToWindow{
     NSLog(@"didmovetowindow");
    [super didMoveToWindow];
}

- (void)layoutSubviews
{
    NSLog(@"layoutSubviews");
    [super layoutSubviews];
}


- (void)removeFromSuperview{
    NSLog(@"layoutSubviews");
    [super removeFromSuperview];
}

- (void)dealloc{
    NSLog(@"dealloc");
}

@end
代码语言:javascript
复制
- (void)testCase{
    _testView = [TestView new];
    [self.view addSubview:_testView];
    
    [NSTimer scheduledTimerWithTimeInterval:5 repeats:NO block:^(NSTimer * _Nonnull timer) {
        [self removeView];
    }];
}

- (void)removeView{
    [self.testView removeFromSuperview];
    self.testView = nil;
}

打印

代码语言:javascript
复制
2019-08-28 11:03:45.370026+0800 TestDemo[28304:5846595] initWithFrame
2019-08-28 11:03:45.370308+0800 TestDemo[28304:5846595] willmovetosuperview
2019-08-28 11:03:45.370479+0800 TestDemo[28304:5846595] didmovetosuperview
2019-08-28 11:03:45.371084+0800 TestDemo[28304:5846595] willmovetowindow
2019-08-28 11:03:45.371413+0800 TestDemo[28304:5846595] didmovetowindow
2019-08-28 11:03:45.374831+0800 TestDemo[28304:5846595] layoutSubviews

移除

代码语言:javascript
复制
2019-08-28 11:03:50.372110+0800 TestDemo[28304:5846595] layoutSubviews
2019-08-28 11:03:50.372423+0800 TestDemo[28304:5846595] willmovetosuperview
2019-08-28 11:03:50.372730+0800 TestDemo[28304:5846595] willmovetowindow
2019-08-28 11:03:50.373098+0800 TestDemo[28304:5846595] didmovetowindow
2019-08-28 11:03:50.373302+0800 TestDemo[28304:5846595] didmovetosuperview
2019-08-28 11:03:50.373521+0800 TestDemo[28304:5846595] dealloc
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.08.28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 显示过程
  • 移除过程
  • 验证
  • 打印
  • 移除
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档