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. VIP卡

    VIP卡:http://item.taobao.com/item.htm?id=6826715667&ali_refid=a3_420435_1006:1102617497:6::683ff3 ...

  2. mysql和oracle的区别(功能性能、选择、使用它们时的sql等对比)

    一.并发性 并发性是oltp数据库最重要的特性,但并发涉及到资源的获取.共享与锁定. mysql:mysql以表级锁为主,对资源锁定的粒度很大,如果一个session对一个表加锁时间过长,会让其他se ...

  3. 用友android

    获取token要改 value要改 不用通配符 直接双引号

  4. 成功的背后!(给所有IT人)

    转载:来自CSDN第一名博主:http://blog.csdn.net/phphot/article/details/2187505 成功的背后,有着许多不为人知的故事,而正是这些夹杂着泪水和汗水的过 ...

  5. Mac Pro 修改主机名

    修改 主机名称 sudo scutil --set HostName jianbao-PC 修改 共享名称 sudo scutil --set ComputerName jianbao-PC

  6. sql 2005,2008开启bcp的方法嗯哈步骤

    sqlserver 2008开启bcp服务的方法和步骤 sqlserver 2005开启bcp服务的方法和步骤 在开始菜单中找到sql server 2005 -->> 配置工具 --&g ...

  7. MFC线程内操作主窗体 控件

    CWnd* h_d2 = AfxGetApp()->GetMainWnd(); //获取主窗口的句柄 h_d2-> GetDlgItem(IDC_EDIT2)->GetWindowT ...

  8. Sublime多行编辑快捷键

    鼠标选中多行,按下 Ctrl Shift L (Command Shift L) 即可同时编辑这些行: 鼠标选中文本,反复按 CTRL D (Command D) 即可继续向下同时选中下一个相同的文本 ...

  9. HNU 12868 Island (简单题)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12868&courseid=272 解题报告:输入n*m ...

  10. WWDC15 Session笔记 - Xcode 7 UI 测试初窥

    https://onevcat.com/2015/09/ui-testing/ WWDC15 Session笔记 - Xcode 7 UI 测试初窥 Unit Test 在 iOS 开发中已经有足够多 ...