• iOS中控制器的生命周期

  一般我们在创建控制器的时候,有三种方法。

  1.  直接通过代码创建

  2.  通过storyboard创建

  3.  通过Xib,在创建控制器的时候传入一个Xib文件作为这个控制器的view。

  • 直接通过代码创建

  通过代码创建这种凡是,我们打印调用顺序可以发现

  

  对应的代码调用顺序就是  loadView -> viewDidLoad -> viewWillAppear -> viewWillLayoutSubviews -> viewDidLayoutSubviews -> viewDidAppear

  

// 1.
- (void)loadView; // This is where subclasses should create their custom view
hierarchy if they aren't using a nib. Should never be called directly. // 2.
- (void)viewDidLoad; // Called after the view has been loaded. For view controllers created in code, this is after -loadView. For view controllers unarchived from a nib, this is after the view is set. // 3.
- (void)viewWillAppear:(BOOL)animated; // Called when the view is about to made visible. Default does nothing // 4.
- (void)viewWillLayoutSubviews NS_AVAILABLE_IOS(5_0);
// Called just after the view controller's view's layoutSubviews method is invoked. Subclasses can implement as necessary. The default is a nop. // 5.
- (void)viewDidLayoutSubviews NS_AVAILABLE_IOS(5_0);

  控制器创建之后,弹出另一个控制器(当前控制器消失),执行的代码顺序 viewWillDisappear -> viewWillLayoutSubviews -> viewDidLayoutSubviews -> viewDidDisappear

  

  

// 1.
- (void)viewWillDisappear:(BOOL)animated; // Called when the view is dismissed, covered or otherwise hidden. Default does nothing // 2.
- (void)viewWillLayoutSubviews NS_AVAILABLE_IOS(5_0);
// Called just after the view controller's view's layoutSubviews method is invoked. Subclasses can implement as necessary. The default is a nop. // 3.
- (void)viewDidLayoutSubviews NS_AVAILABLE_IOS(5_0); // 4.
- (void)viewDidDisappear:(BOOL)animated; // Called after the view was dismissed, covered or otherwise hidden. Default does nothing
  • 通过storyboard创建控制器

  

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:NSStringFromClass([CViewController class]) bundle:nil];
CViewController *touch = [storyBoard instantiateViewControllerWithIdentifier:NSStringFromClass([CViewController class])];
[self presentViewController:touch animated:YES completion:nil];

  为了方便,同时减少手误,这里重用ID我们使用类名

  

  通过打印来观察

  

  可以看到代码的调用顺序就是 awakeFromNib -> loadView -> viewWillAppear -> viewWillLayoutSubviews -> viewDidLayoutSubviews -> viewDidAppear

// 1.
- (void)awakeFromNib NS_REQUIRES_SUPER; // 2.
- (void)loadView; // This is where subclasses should create their custom view hierarchy if they aren't using a nib. Should never be called directly. // 3.
- (void)viewDidLoad; // Called after the view has been loaded. For view controllers created in code, this is after -loadView. For view controllers unarchived from a nib, this is after the view is set. // 4.
- (void)viewWillAppear:(BOOL)animated; // Called when the view is about to made visible. Default does nothing // 5.
- (void)viewWillLayoutSubviews NS_AVAILABLE_IOS(5_0);
// Called just after the view controller's view's layoutSubviews method is invoked. Subclasses can implement as necessary. The default is a nop.
- (void)viewDidLayoutSubviews NS_AVAILABLE_IOS(5_0); // 6.
- (void)viewDidAppear:(BOOL)animated; // Called when the view has been fully transitioned onto the screen. Default does nothing

  这里需要注意的是,我们是控制器通过storyboard来加载,那么是根据storyboard的描述创建view。

  

  • 通过XIB来创建加载控制器

  首先创建一个XIB,快捷键 command + N 来创建。

  

  

  创建后XIB之后我们还需要设置文件

  

    这里设置为我们创建的控制器

  

  所有需要的已经设置好,这时候我们可以来码代码了。

DViewController *touch = [[DViewController alloc] initWithNibName:NSStringFromClass([DViewController class]) bundle:nil];
[self presentViewController:touch animated:YES completion:nil];

  下面我们来观察控制器的生命周期

  代码层面上就是

// 1.
- (void)loadView; // This is where subclasses should create their custom view hierarchy if they aren't using a nib. Should never be called directly. // 2.
- (void)viewDidLoad; // Called after the view has been loaded. For view controllers created in code, this is after -loadView. For view controllers unarchived from a nib, this is after the view is set. // 3.
- (void)viewWillAppear:(BOOL)animated; // Called when the view is about to made visible. Default does nothing // 4.可能会多次调用
- (void)viewWillLayoutSubviews NS_AVAILABLE_IOS(5_0);
// Called just after the view controller's view's layoutSubviews method is invoked. Subclasses can implement as necessary. The default is a nop.
- (void)viewDidLayoutSubviews NS_AVAILABLE_IOS(5_0); // 5.
- (void)viewDidAppear:(BOOL)animated; // Called when the view has been fully transitioned onto the screen. Default does nothing

  说明:

  加载XIB的时候,我们同样可以不指定XIB名称,这样loadView 就会最终加载与控制器同名的 XIB (命名规范很重要啊)

DViewController *touch = [[DViewController alloc] init];
[self presentViewController:touch animated:YES completion:nil];

  同时,如果我们删掉 DViewController.xib ,同时创建一个 DView.xib 当我们用以上不指定xib名称,同样是可以加载出来的(命名规范很重要)。

  我们可以知道,加载xib的过程是,如果指定名称,那么加载指定名称的xib,如果没有指定名称就会加载和控制器同名的xib,如果没有就会找去控制器名相同但是没有 controller 的xib来加载。

  • A present B控制器再返回,两个控制器的生命周期

  1. 创建A控制器

  

  流程:

A:loadView
A:viewDidLoad
A:viewWillAppear
A:viewWillLayout...
A:viewDidAppear

  2. A present 到B控制器

  

  流程:

A:loadView
A:viewDidLoad
A:viewWillAppear
A:viewWillLayout... // 可能多次调用
A:viewDidAppear -------------- Present B控制器 -------------- B:loadView
B:viewDidLoad A:viewWillDisappear B:viewWillAppear
B:viewLayout.... // 可能多次调用 A;viewLayout... // 可能多次调用 B:viewDidAppear // 多次调用,会跟下面的调用顺序可能会有些调换 A:viewDidDisappear

ios控制器生存周期的更多相关文章

  1. iOS控制器的创建方式

    iOS控制器的创建.除了常见的alloc init外还有通过加载storyboard和xib的方式,下边逐一展开: 1.代码alloc init 创建方式 ViewController *vc= [[ ...

  2. iOS 控制器的生命周期(UIController)

    前言: 在iOS开发中,控制器的生命周期非常重要,什么时候加载页面,什么时候请求接口,什么时候刷新界面等等,都有很多值得优化的地方 loadView: 最先执行的方法,控制器关联的有Nib文件的时候, ...

  3. iOS控制器与视图加载方法

    转载记录, 请看原文: 1. iOS中的各种加载方法(initWithNibName,loadNibNamed,initWithCoder,awakeFromNib等等)简单使用   http://w ...

  4. iOS 控制器title和tabbar的title设置问题

    iOS 设置tabbarItem的title的是通过 controller.tabBarItem.title = @"标题" iOS 设置导航栏控制器title通过 contoll ...

  5. iOS控制器的生命周期分析和使用

    转自http://blog.csdn.net/qijianli/article/details/7826979 iOS的SDK中提供很多原生ViewController,大大提高了我们的开发效率,下面 ...

  6. ios控制器生命周期详解

    #import "MyOneViewController.h" @interface MyOneViewController () @property (nonatomic, st ...

  7. iOS控制器瘦身-面向超类编程

    今天写这篇文章的目的,是提供一种思路,来帮助大家解决控制器非常臃肿的问题,对控制器瘦身. 滴滴 老司机要开车了 如果手边有项目,不妨打开工程看一下你的控制器代码有多少行,是不是非常多?再看一下tabl ...

  8. 【iOS控制器跳转时,NavigationBar有阴影动画闪过的解决办法】

    如题,push控制器时,由于默认的控制器view是黑色,push到这个控制器时,navigationBar(默认是透明效果)后面有一个黑色阴影一闪而过,解决办法将navigationBar设为图片填充 ...

  9. ios控制器modal跳转

    1. http://www.cnblogs.com/smileEvday/archive/2012/05/29/presentModalViewController.html 2012年5月- Pre ...

随机推荐

  1. 由 “无法使用从远程表选择的 lob 定位符” 错误而引导出来的一系列问题解决方案

    周一上班遇到一个数据加工问题:无法使用从远程表选择的 lob 定位符,由于数据源表不是自己的,不能对源数据做修改,于是我打起了存储过程的主意 我们公司的存过是分三步走,第一层是同步源数据,第二层是对一 ...

  2. Ubuntu发行版升级

    从UK 13.10升级到UK 14.10 方法一: 1.sudo apt-get update   2.sudo update-manager -c -d   3.选择upgrade(升级)  方法二 ...

  3. 通过PHP前端后台交互/通过ajax前端后台交互/php基础传输数据应用/简单的留言版/简单的注册账户/简单的登录页/

      前  言  PHP     通过上一篇博客,注册账号与登录页面--前后台数据交互  跳转转到index主页,接下来进入主页留言板功能,通过ajax向后台传输数据,同时发表留言. 具体的内容分析如下 ...

  4. Angular中使用Swiper不能滑动的解决方法

    Swiper是目前较为流行的移动端触摸滑动插件,因为其简单好用易上手,很受很多设计师的欢迎. 今天在使用Swiper的时候遇到这个问题: 使用angularjs动态循环生成swiper-slide类, ...

  5. SDS 链表

    sds定义 struct sdshdr{ int len int free char buf[] } sds和c语言类似,仍然把字符串的末尾加上一个'.0',但是不会计入总长度,也就是不会对len造成 ...

  6. jquery hide和show使用

    $("#qitarenyuanDiv").hide("fast");$("#qitarenyuanDiv").show("fast ...

  7. 逻辑性最强的React Native环境搭建与调试

    React Native(以下简称RN),已经“火”了好一段时间了,网上的资料相对也很丰富,只是一直迟迟没有发布1.0,不过出身豪门(Facebook)的RN和国内顶级互联网公司对于RN的实践与应用, ...

  8. Ext 创建workspace package

    Ext 创建workspace package Package ExtJs Project 1. 创建工作区间文件目录 md wpt 2. 进入目录 cd wpt 3. 创建 创建工作区间 sench ...

  9. MySQL(四)--练习题

     2.1 编写一条 SQL 语句,从 Product(商品)表中选取出“登记日期(regist_date)在 2009 年 4 月 28 日之后”的商品.查询结果要包含 product_name 和 ...

  10. Linux内核的基本概念

    Linux内核学习,推荐的书籍: <linux设备驱动开发详解第二版>.<Linux内核设计与实现第三版>.<嵌入式Linux应用开发完全手册> 第一篇:讲解Lin ...