ios控制器生存周期

  • 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
时间: 2024-10-11 07:47:15

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

iOS控制器的创建方式

iOS控制器的创建.除了常见的alloc init外还有通过加载storyboard和xib的方式,下边逐一展开: 1.代码alloc init 创建方式 ViewController *vc= [[ViewController alloc] init]; 2.storyboard创建控制器 1>加载制定的storyboard文件 UIStoryboard *board =     [UIStoryboard storyboardWithName:@"viewCon" bundl

ios 控制器 modal 学习

5.12 为了给学生改论文,在UPC上登录了Windows 8.1,晚上正常关机.今日切换登陆Ubuntu 14.04分区,发现原来可以正常访问的windows下的NTFS分区都被锁死,提示如下错误: Error mounting /dev/sda2 at /media/love/Mis Archiivos: Command-line `mount -t "ntfs" -o "uhelper=udisks2,nodev,nosuid,uid=1000,gid=1000,dma

一、iOS控制器view的创建方式(官方文档翻译)

1.loadView方法官方说明如下: You should never call this method directly. The view controller calls this method when its view property is requested but is currently nil. This method loads or creates a view and assigns it to the view property. 你不应该直接调用这个方法,当需要访

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

点击查看作者简书地址 今天写这篇文章的目的,是提供一种思路,来帮助大家解决控制器非常臃肿的问题,对控制器瘦身. 如果手边有项目,不妨打开工程看一下你的控制器代码有多少行,是不是非常多?再看一下tableView的代理方法cellForRow和heightForRow的代码是不是也是非常多?里面夹杂着switch和大量if esle的判断逻辑的代码.后期维护看着这些if else是不是特别烦躁?特别是自己在维护前人写的代码,并且还没有注释 一团糟,是不是有更想骂人的冲动?别怕,这里给您提供一种解决

iOS 控制器View的生命周期及相关函数

1.loadView 1.1 如果重写了控制器的loadView方法,则控制器的View按照loadView方法的描述去创建 - (void)loadView { self.view = [[UIView alloc]init]; self.view.backgroundColor = [UIColor redColor]; } 1.2 如果没重写控制器的loadView方法,则看有没有storyboard的,有的话,则按storyboard的描述创建view 加载storyboard的方法:

iOS控制器常见的创建方式有以下几种

>通过storyboard创建 >直接创建 YTViewController *yt = [[YTViewController alloc]init]; >指定xib文件来创建 YTViewController *yt = [[YTViewController alloc]initWithNibName:@"YTViewController" bundle:nil];

Phonegap 之 iOS银联在线支付(js调用ios端银联支付控件)

Phonegap项目,做支付的时候,当把网站打包到ios或android端成app后,在app上通过wap调用银联在线存在一个问题: 就是当从银联支付成功后,再从服务器返回到app客户端就很难实现. wap银联支付流程是这样:客户端---> 服务器(构建支付请求)--> 银联支付 ---> 返回到服务端(处理支付结果).所以对于手机网站银联支付没有问题,但是对于ios端app和android端app, 再通过wap支付,发现支付成功后,很难在回到app客户端了. 所以这里就必须借助Pho

IOS的控制器

控制器简单来说,就是用来做界面跳转的,类似于 Android 的Intent 1.创建一个控制器 1 控制器的常见的创建方式 2 3 1)通过storyboard创建 4 5 //直接创建 6 NJViewController *nj = [[NJViewController alloc]init]; 7 8 //指定xib文件 9 NJViewcontroller *nj = [[NJViewController alloc] 10 initWithNibName:@"NJViewContro

iOS开发项目-02添加子控制器以及项目分层

iOS开发项目-02添加子控制器以及项目分层 一.添加子控制器 1.设置根控制器(自定义) 说明:分析新浪微博应用,观察其整体建构层次.而系统的控制器不能满足项目开发的需求,这里把项目中原有的控制器删除. 自己定义一个TabBarViewController类.让这个类作为window窗口的根控制器. YYAppDelegate.m文件代码: 1 #import "YYAppDelegate.h" 2 #import "YYTabBarViewController.h&quo