UIView的生命周期
//
// ViewController.m
// UIViewLive
//
// Created by mac on 2021/9/16.
//
#import "ViewController.h"
@interface TestView : UIView
@end
@implementation TestView
-(instancetype)init{
self = [super init];
if(self){
}
return self;
}
- (void)willMoveToSuperview:(nullable UIView *)newSuperview{
//从写方法的话前面加上super
[super willMoveToSuperview:newSuperview];
}
- (void)didMoveToSuperview{
[super didMoveToSuperview];
}
- (void)willMoveToWindow:(nullable UIWindow *)newWindow{
[super willMoveToWindow:newWindow];
}
- (void)didMoveToWindow{
[super didMoveToWindow];
}
@end
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
TestView *view2 = [[TestView alloc]init];
view2.backgroundColor = [UIColor greenColor];
view2.frame = CGRectMake(150,150,100,100);
[self.view addSubview:view2];
}
@end
执行过程
UIViewController的生命周期
视图控制器,管理视图View层级结构
自身包含view,可以理解为一个容器
管理view视图的生命周期
响应用户操作
和app整体交互,视图的切换
作为一个container管理逗哥controller跟动画
在执行完四操作之后,视图显示出来了
在viewcoltroller准备好之后就会调用viewDidLoad
在日常工作中所有跟view相关的初始化逻辑都会放在viewDidLoad当中
总结一下viewController的生命周期
init
viewDidLoad
viewWillAppear
viewDidAppear
viewWillDisAppear
viewDisDisAppear
dealloc