当前位置: 首页 > news >正文

【iOS】ViewController的生命周期

文章目录

  • 概念
  • 生命周期
    • 函数
    • 周期函数的调用顺序
    • push/pop
      • 运行结果
      • 输出
    • present/dismiss
      • 运行结果
      • 输出

概念

在iOS中一般有两种ViewController,一类是用于显示内容的,比如UIViewController,UITableVIewController或者自定义的VC等,另一类是容器型的控制器,如UINavigationController(内部维护一个栈,前面博客中有所讲解,用push,pop进行控制),UITabBarController(内部维护一个数组)等

内容型 VC = 页面本身(展示内容和逻辑)

容器型 VC = 用来管理多个内容 VC 的关系(栈 or 数组)

生命周期

请添加图片描述

函数

  1. init:初始化程序
  2. loadView:在UIViewController对象的view被访问且为空时调用
  3. viewDidLoad:视图加载完成后调用,进行如控件的初始化之类的操作,在这个控制器销毁之前只会调用一次该方法
  4. viewWillAppear:视图即将展现时调用
  5. viewWillLayoutSubviews:即将开始子视图位置布局
  6. viewDidLayoutSubviews:用于通知视图的位置布局已经完成
  7. viewDidAppear:视图已经出现时调用
  8. viewWillDisappear:视图即将消失
  9. viewDidDisappear:视图已经消失
  10. dealloc:视图被销毁

周期函数的调用顺序

我们具体调用控制器推出另一个控制器时的具体方法有两种,一个是push方法,另一个是present方法,他们的具体调用顺序调用我们在下面演示一下

push/pop

#import "ViewControllerA.h"
#import "ViewControllerB.h"
@interface ViewControllerA ()
@property (nonatomic, strong) UIButton *pushButton;
@end@implementation ViewControllerA- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor blueColor];self.title = @"ViewController A";NSLog(@"A - %s", __func__);_pushButton = [UIButton buttonWithType:UIButtonTypeSystem];[_pushButton setTitle:@"Push to B" forState:UIControlStateNormal];_pushButton.frame = CGRectMake(self.view.frame.size.width / 2 - 100, 200, 200, 50);_pushButton.backgroundColor = [UIColor whiteColor];[self.view addSubview:_pushButton];[_pushButton addTarget:self action:@selector(pushViewControllerB) forControlEvents:UIControlEventTouchUpInside];
}- (void)pushViewControllerB {ViewControllerB *b = [[ViewControllerB alloc] init];NSLog(@"-------- A push B --------");[self.navigationController pushViewController:b animated:YES];
}
- (void)loadView {[super loadView];NSLog(@"A - %s", __func__);
}- (void)viewWillAppear:(BOOL)animated {[super viewWillAppear:animated];NSLog(@"A - %s", __func__);
}- (void)viewWillLayoutSubviews {[super viewWillLayoutSubviews];NSLog(@"A - %s", __func__);
}- (void)viewDidLayoutSubviews {[super viewDidLayoutSubviews];NSLog(@"A - %s", __func__);
}- (void)viewDidAppear:(BOOL)animated {[super viewDidAppear:animated];NSLog(@"A - %s", __func__);
}- (void)viewWillDisappear:(BOOL)animated {[super viewWillDisappear:animated];NSLog(@"A - %s", __func__);
}- (void)viewDidDisappear:(BOOL)animated {[super viewDidDisappear:animated];NSLog(@"A - %s", __func__);
}
#import "ViewControllerB.h"@interface ViewControllerB ()
@property (nonatomic, strong) UIButton *popButton;
@end@implementation ViewControllerB- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor redColor];self.title = @"ViewController B";NSLog(@"B - %s", __func__);// Pop 按钮_popButton = [UIButton buttonWithType:UIButtonTypeSystem];[_popButton setTitle:@"Pop to A" forState:UIControlStateNormal];_popButton.frame = CGRectMake(self.view.frame.size.width / 2 - 100, 200, 200, 50);_popButton.backgroundColor = [UIColor whiteColor];[self.view addSubview:_popButton];[_popButton addTarget:self action:@selector(popToViewControllerA) forControlEvents:UIControlEventTouchUpInside];
}- (void)popToViewControllerA {NSLog(@"-------- B pop A --------");[self.navigationController popViewControllerAnimated:YES];
}
- (void)loadView {[super loadView];NSLog(@"B - %s", __func__);
}- (void)viewWillAppear:(BOOL)animated {[super viewWillAppear:animated];NSLog(@"B - %s", __func__);
}- (void)viewWillLayoutSubviews {[super viewWillLayoutSubviews];NSLog(@"B - %s", __func__);
}- (void)viewDidLayoutSubviews {[super viewDidLayoutSubviews];NSLog(@"B - %s", __func__);
}- (void)viewDidAppear:(BOOL)animated {[super viewDidAppear:animated];NSLog(@"B - %s", __func__);
}- (void)viewWillDisappear:(BOOL)animated {[super viewWillDisappear:animated];NSLog(@"B - %s", __func__);
}- (void)viewDidDisappear:(BOOL)animated {[super viewDidDisappear:animated];NSLog(@"B - %s", __func__);
}

运行结果

请添加图片描述

输出

请添加图片描述

请添加图片描述

present/dismiss

#import "ViewControllerA.h"
#import "ViewControllerB.h"@interface ViewControllerA ()
@property (nonatomic, strong) UIButton *presentButton;
@end@implementation ViewControllerA- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor blueColor];self.title = @"ViewController A";NSLog(@"A - %s", __func__);_presentButton = [UIButton buttonWithType:UIButtonTypeSystem];[_presentButton setTitle:@"Present B" forState:UIControlStateNormal];_presentButton.frame = CGRectMake(self.view.frame.size.width / 2 - 100, 200, 200, 50);_presentButton.backgroundColor = [UIColor whiteColor];[self.view addSubview:_presentButton];[_presentButton addTarget:self action:@selector(presentViewControllerB) forControlEvents:UIControlEventTouchUpInside];
}- (void)presentViewControllerB {ViewControllerB *b = [[ViewControllerB alloc] init];NSLog(@"-------- A present B --------");b.modalPresentationStyle = UIModalPresentationFullScreen;[self presentViewController:b animated:YES completion:nil];
}
- (void)loadView {[super loadView];NSLog(@"A - %s", __func__);
}- (void)viewWillAppear:(BOOL)animated {[super viewWillAppear:animated];NSLog(@"A - %s", __func__);
}- (void)viewWillLayoutSubviews {[super viewWillLayoutSubviews];NSLog(@"A - %s", __func__);
}- (void)viewDidLayoutSubviews {[super viewDidLayoutSubviews];NSLog(@"A - %s", __func__);
}- (void)viewDidAppear:(BOOL)animated {[super viewDidAppear:animated];NSLog(@"A - %s", __func__);
}- (void)viewWillDisappear:(BOOL)animated {[super viewWillDisappear:animated];NSLog(@"A - %s", __func__);
}- (void)viewDidDisappear:(BOOL)animated {[super viewDidDisappear:animated];NSLog(@"A - %s", __func__);
}/*
#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {// Get the new view controller using [segue destinationViewController].// Pass the selected object to the new view controller.
}
*/@end
#import "ViewControllerB.h"@interface ViewControllerB ()
@property (nonatomic, strong) UIButton *dismissButton;
@end@implementation ViewControllerB- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor redColor];self.title = @"ViewController B";NSLog(@"B - %s", __func__);_dismissButton = [UIButton buttonWithType:UIButtonTypeSystem];[_dismissButton setTitle:@"Dismiss to A" forState:UIControlStateNormal];_dismissButton.frame = CGRectMake(self.view.frame.size.width / 2 - 100, 200, 200, 50);_dismissButton.backgroundColor = [UIColor whiteColor];[self.view addSubview:_dismissButton];[_dismissButton addTarget:self action:@selector(dismissToViewControllerA) forControlEvents:UIControlEventTouchUpInside];
}- (void)dismissToViewControllerA {NSLog(@"-------- B dismiss A --------");[self dismissViewControllerAnimated:YES completion:nil];
}- (void)loadView {[super loadView];NSLog(@"B - %s", __func__);
}- (void)viewWillAppear:(BOOL)animated {[super viewWillAppear:animated];NSLog(@"B - %s", __func__);
}- (void)viewWillLayoutSubviews {[super viewWillLayoutSubviews];NSLog(@"B - %s", __func__);
}- (void)viewDidLayoutSubviews {[super viewDidLayoutSubviews];NSLog(@"B - %s", __func__);
}- (void)viewDidAppear:(BOOL)animated {[super viewDidAppear:animated];NSLog(@"B - %s", __func__);
}- (void)viewWillDisappear:(BOOL)animated {[super viewWillDisappear:animated];NSLog(@"B - %s", __func__);
}- (void)viewDidDisappear:(BOOL)animated {[super viewDidDisappear:animated];NSLog(@"B - %s", __func__);
}/*
#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {// Get the new view controller using [segue destinationViewController].// Pass the selected object to the new view controller.
}
*/@end

运行结果

请添加图片描述

输出

请添加图片描述
请添加图片描述


文章转载自:

http://Mf67LWvN.tktcr.cn
http://PBOPoC6s.tktcr.cn
http://RzTxShBa.tktcr.cn
http://PS8oQ5gK.tktcr.cn
http://UQORskss.tktcr.cn
http://a5IJQkeW.tktcr.cn
http://mRabUACp.tktcr.cn
http://MjCuPkIt.tktcr.cn
http://D6oEvkkZ.tktcr.cn
http://U03ef9NB.tktcr.cn
http://lLZjlull.tktcr.cn
http://C4OftK7E.tktcr.cn
http://KlZwmQck.tktcr.cn
http://45ZIOxNa.tktcr.cn
http://QbmarAHw.tktcr.cn
http://m2J9sNLW.tktcr.cn
http://kolqyjU4.tktcr.cn
http://YhV7XrHC.tktcr.cn
http://4i1AePeC.tktcr.cn
http://NZ32HwU2.tktcr.cn
http://Rsq83uoI.tktcr.cn
http://ZafB6c0H.tktcr.cn
http://wOxy41rc.tktcr.cn
http://CwKGHbAy.tktcr.cn
http://xfWSOrth.tktcr.cn
http://n83CtYxU.tktcr.cn
http://bqBz0EPV.tktcr.cn
http://wtRSyQZY.tktcr.cn
http://0Mu696Lj.tktcr.cn
http://wDjXg3OK.tktcr.cn
http://www.dtcms.com/a/382776.html

相关文章:

  • 数据库基础-01
  • 免费无版权!PPT图标素材的6个优质获取渠道
  • 【STL库】map/set 的封装原理
  • 市面上各类USB无线抓包网卡测试与收录(握手包抓包/无线监听)
  • 基于bang-bang起停式算法的交流电机FOC控制系统simulink建模与模拟仿真
  • 使用HTTPS 服务在浏览器端使用摄像头的方式解析
  • AI 机器视觉检测方案:破解食物包装四大质检难题,筑牢食品安全防线
  • Science Advances--3D打印生物启发扭曲双曲超材料,用于无人机冲击缓冲和自供电实时传感
  • HarmonyOS生态开发核心工具技术介绍及关于CSDN增加ArkTS等标签建议
  • 【算法笔记】堆和堆排序
  • 电商导购系统的微服务监控体系:基于Prometheus与Grafana的可视化方案
  • fMoE论文阅读笔记
  • 721SJBH笔记本电脑销售网站
  • k3s集群部署(使用外部etcd集群)
  • 京东返利app的分布式ID生成策略:雪花算法在订单系统中的实践
  • 大数据分析岗位发展前景与行业需求分析
  • 【Linux手册】共享内存:零拷贝实现共享的优势与实操指南
  • ARM的TrustZone
  • 返利app排行榜的缓存更新策略:基于过期时间与主动更新的混合方案
  • springboot+zookeeper+(2025最新)Dubbo-admin实现分布式
  • 缓存与数据库一致性实战手册:从故障修复到架构演进
  • 基于 Linux 内核模块的字符设备 FIFO 驱动设计与实现解析(C/C++代码实现)
  • 【C++】类和对象(下):初始化列表、类型转换、Static、友元、内部类、匿名对象/有名对象、优化
  • JSON、Ajax
  • 第2课:Agent系统架构与设计模式
  • Python上下文管理器进阶指南:不仅仅是with语句
  • Entities - Entity 的创建模式
  • 用html5写王者荣耀之王者坟墓的游戏2deepseek版
  • 【Wit】pure-admin后台管理系统前端与FastAPI后端联调通信实例
  • godot+c#使用godot-sqlite连接数据库