iOS UIViewController 的 awakeFromNib 以及 - (id)initWithCoder:(NSCoder *)aDecoder 和 - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

首先看一下awakeFromNib的官方文档:

The nib-loading infrastructure sends an awakeFromNib message to each object recreated from a nib archive, but only after all the objects in the archive have been loaded and initialized. When an object receives an awakeFromNib message, it is guaranteed to have all its outlet and action connections already established. 就是说,awakeFromNib函数中,可以保证所有的IBoutlet链接都有效,而- (id)initWithCoder:(NSCoder *)aDecoder函数中,仅仅能保证self的实例化,无法保证在xib中的其他对象也实例化完毕,所以有的IBoutlet链接有可能是nil!
During the instantiation process, each object in the archive is unarchived and then initialized with the method befitting its type. Objects that conform to the NSCoding protocol (including all subclasses of UIView and UIViewController) are initialized using their initWithCoder: method. All objects that do not conform to the NSCoding protocol are initialized using their init method. After all objects have been instantiated and initialized, the nib-loading code reestablishes the outlet and action connections for all of those objects. It then calls the awakeFromNib method of the objects.

对于UIviewController来说,awakeFromNib 和 - (id)initWithCoder:(NSCoder *)aDecoder是一起出现的,先调用- (id)initWithCoder:(NSCoder *)aDecoder 再调用 awakeFromNib。用storyboard 方法创建的UIViewcontroller就会产生这样的效果。

但是对于使用以前的初始化函数 - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil来生成UIViewcontroller是不会调用上面2个函数的!问什么呢?这其实是2套初始化方法,第一套使用storyboard 方法创建的UIViewcontroller,确实是从nib文件中decode出了 viewcontroller;第二套方法,也用到了nib,但是这个nib仅仅包含view的信息,根本没有包含viewcontroller对象,viewcontroller对象不是从nib中decode出来的!

iOS UIViewController 和 nib 相关的3个方法的更多相关文章

  1. iOS开发之--cocopods相关问题及解决方法

    Note: as of CocoaPods 1.0, `pod repo update` does not happen on `pod install` by default 解决办法:删除工程中的 ...

  2. 细数AutoLayout以来UIView和UIViewController新增的相关API

    本文转载至 http://www.itjhwd.com/autolayout-uiview-uiviewcontroller-api/ 细数AutoLayout以来UIView和UIViewContr ...

  3. 转:基于IOS上MDM技术相关资料整理及汇总

    一.MDM相关知识: MDM (Mobile Device Management ),即移动设备管理.在21世纪的今天,数据是企业宝贵的资产,安全问题更是重中之重,在移动互联网时代,员工个人的设备接入 ...

  4. UIViewController XIB/NIB加载过程

    UIViewController中关于nib初始化的函数 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBu ...

  5. 基于IOS上MDM技术相关资料整理及汇总

    (转自:http://www.mbaike.net/special/1542.html) 一.MDM相关知识:MDM (Mobile Device Management ),即移动设备管理.在21世纪 ...

  6. iOS开发之动画编程的几种方法

    iOS开发之动画编程的几种方法 IOS中的动画总结来说有五种:UIView<block>,CAAnimation<CABasicAnimation,CATransition,CAKe ...

  7. iOS 使用 UIMenuController 且不隐藏键盘的方法

    iOS 使用 UIMenuController 且不隐藏键盘的方法 在键盘显示的时候使用 UIMenuController 弹出菜单,保持键盘显示且可输入的状态. 实现方法有 修改响应链(推荐) 遵循 ...

  8. bootsrtap h5 移动版页面 在苹果手机ios滑动上下拉动滚动卡顿问题解决方法

    bootsrtap h5 移动版页面 在苹果手机ios滑动上下拉动滚动卡顿问题解决方法 bootsrtap框架做的h5页面,在android手机下没有卡顿问题,在苹果手机就一直存在这问题,开始毫无头绪 ...

  9. iOS UITextView placeHolder占位文字的N种方法实现方法

    方法一 1.把UITextView的text属性当成“placeholder”使用. 2.在开始编辑的代理方法里清除“placeholder”. 3.在结束编辑的代理方法里根据条件设置“placeho ...

随机推荐

  1. Redis for .NET 系列之实现分页需求

    代码笔记: var tableName = "Table1"; redisClient.AddItemToSortedSet(tableName, ); redisClient.A ...

  2. 【转】 详解Kafka生产者Producer配置

    粘贴一下这个配置,与我自己的程序做对比,看看能不能完善我的异步带代码:   -----------------------------------------    详解Kafka生产者Produce ...

  3. php 选择排序法

    private function demo(array $arr = array()){ $len = count($arr); if ($len == 1) return $arr; else fo ...

  4. EasyUI datagrid优化

    easyui datagrid 在IE上加载速度慢, 150行数据就无法忍受了. firefox加载速度还可以. jquery easyui datagrid使用参考 http://www.cnblo ...

  5. NUGet的诞生与使用

    本文引用地址:http://msdn.microsoft.com/zh-cn/magazine/hh547106.aspx NuGet 使用 NuGet 管理项目库 Phil Haack 无论多么努力 ...

  6. 开机自动连接/断开VPN 批处理

    或许大家在工作或生活中有接触到VPN,如果使用Windows自带的VPN来连接,每次开机要像宽带拨号那样,右击该VPN连接图标,然后选择“连接”(如果未记住密码甚至还要输入密码),然后点击确定,有点麻 ...

  7. 必须知道的.net(性能条款)

    以dispose的模式来代替finalize方式:非托管资源的清理主要有终止化操作和Dispose模式两种,其中Finalize方式存在执行时间不确定,运行顺序不确定,同时对垃圾回收的性能有极大的损伤 ...

  8. Java并发包源码学习之AQS框架(二)CLH lock queue和自旋锁

    上一篇文章提到AQS是基于CLH lock queue,那么什么是CLH lock queue,说复杂很复杂说简单也简单, 所谓大道至简: CLH lock queue其实就是一个FIFO的队列,队列 ...

  9. Mac Pro 使用 ll、la、l等ls的别名命令

    在 Linux 下习惯使用 ll.la.l 等ls别名的童鞋到 mac os 可就郁闷了~~ 其实只要在用户目录下建立一个脚本“.bash_profile”, vim .bash_profile 并输 ...

  10. ORA-22868: 具有 LOB 的表包含有位于不同表空间的段

    由于lob对象引起的表空间无法删除.本来是要删除DMS表空间,但是上面有LOB对象,而且表却是在别的表空间DMS4上.解决的办法就是将这些lob移动到DMS4表空间.下面是解决过程 删除用户时报错: ...