【iOS】ViewController生命周期

了解ViewController生命周期之前,我们首先认识一下ViewController的结构


UIViewController的结构

视图控制器(ViewController),管理View的层级结构,其自身内置了一个view,可以将view的Controller理解为一个view的container(容器),ta的主要职责有以下几点:

  • 管理View视图的生命周期
  • 响应用户操作
  • 和App整体交互,视图的切换
  • 作为一个容器管理多个Controller和动画

ViewController自身内置的view(UIViewController Default View)就是self.view,把自定义的view通过addSubView:粘贴在这个默认提供的view(self.view)上,就实现了控制器对全部视图的管理,因此可以把视图控制器理解为一个包含多个视图容器

ViewController的生命周期

重点关注以下方法:

方法名称 职能
init ViewController初始化时的一个重载函数
viewDidLoad ViewController内置的默认view创建时调用
viewWillAppear ViewController管理的所有视图即将出现时调用
viewDidAppear ViewController管理的所有视图已经出现时调用
viewWillDisappear ViewController管理的所有视图即将消失时调用
viewDidDisappear ViewController管理的所有视图已经消失时调用
Dealloc ViewController销毁时调用

下面我们将以上方法在程序中重写,观察各个方法被调用的时机:

objectivec 复制代码
@implementation ViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.freshView = [[UIView alloc] init];
    self.freshView.backgroundColor = [UIColor magentaColor];
    self.freshView.frame = CGRectMake(0, 0, 266, 277);
    self.freshView.center = self.view.center;
    [self.view addSubview: self.freshView];
    
    UITapGestureRecognizer* gestureTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(hideViewController)];
    [self.freshView addGestureRecognizer: gestureTapRecognizer];
}

//默认视图及其包含的子视图即将在屏幕中出现
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear: animated];
}    // Called when the view is about to made visible. Default does nothing

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear: animated];
}     // Called when the view has been fully transitioned onto the screen. Default does nothing

//Controller从屏幕中消失不见时(对应一些销毁的逻辑)
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear: animated];
} // Called when the view is dismissed, covered or otherwise hidden. Default does nothing

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear: animated];
}  // Called after the view was dismissed, covered or otherwise hidden. Default does nothing

- (void) dealloc {
    
}

- (void)hideViewController {
    UIViewController* viewController = [[UIViewController alloc] init];
    viewController.view.backgroundColor = [UIColor systemMintColor];
    
    viewController.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController: viewController animated: YES completion: nil];
    
}

@end

在重写的每个方法中都打上断点:

可以看到程序先在viewDidLoad里面加载好控件,

ViewController即将出现时,调用viewWillAppear

ViewController已经出现时,调用viewDidAppear

点击隐藏ViewController事件方法后,

新的viewController即将出现时,调用viewWillDisappear

新的viewController已经出现时,调用viewDidDisAppear

总结

了解了UIViewController的生命周期后,就可以在 合适的时机(重写的方法里) 根据需求自定义想要实现的交互事件、代码逻辑

相关推荐
weixin_4038101319 小时前
iOS自动化脚本怎么避免风控?无屏幕镜像截图原理详解
ios·自动化·跨境电商·苹果群控·苹果投屏·多媒体运营
wangruofeng20 小时前
3 步把 MacBook 变成 7×24 远程主机,人在外面随时接管桌面
ios·agent·mac
weixin_4038101320 小时前
iOS免越狱自动化蓝牙HID方案:不装代理也能跑脚本
运维·ios·自动化·跨境电商·ai智能体·ios投屏·苹果投屏
牛哇网络工作室1 天前
UnityHDRP写实数字人全流程基础5—语音输入和语音识别
android·unity·c#·游戏引擎·aigc·语音识别·xcode
GitLqr1 天前
Flutter 3.47 与 Dart 3.13:你必须了解的迁移指南
flutter·ios·dart
冯汉栩2 天前
Swift Control View,Label渐变颜色(源码)
开发语言·ios·swift
jike_20262 天前
iPhone日历查看会议纪要APP推荐:按日期查找历史录音更方便
ios·语音识别·iphone
冯汉栩2 天前
OC 技术 App上存蒲公英教程
ios
码云之上2 天前
小程序 iOS 多输入框焦点乱序脱坑记
ios·微信小程序·taro
2501_915909063 天前
iOS test 测试怎么做?功能、性能、兼容、稳定与安全五类测试指南
android·ios·小程序·https·uni-app·iphone·webview