iOS UIViewController 和 nib 相关的3个方法

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.

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出来的!

时间: 2024-08-09 00:57:29

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

iOS UIViewController API解读

/*UIViewController is a generic controller base class that manages a view. It has methods that are calledwhen a view appears or disappears. Subclasses can override -loadView to create their custom view hierarchy, or specify a nib name to be loadedaut

细数AutoLayout以来UIView和UIViewController新增的相关API<转写>

细数AutoLayout以来UIView和UIViewController新增的相关API – UIViewController篇 UILayoutSupport @property(nonatomic,readonly,retain) id<UILayoutSupport> topLayoutGuide NS_AVAILABLE_IOS(7_0); @property(nonatomic,readonly,retain) id<UILayoutSupport> bottomLay

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

UILayoutSupport 1 @property(nonatomic,readonly,retain) id topLayoutGuide NS_AVAILABLE_IOS(7_0); 2 @property(nonatomic,readonly,retain) id bottomLayoutGuide NS_AVAILABLE_IOS(7_0); 3 4 @protocol UILayoutSupport 5 @property(nonatomic,readonly) CGFloat l

UIViewController XIB/NIB加载过程

UIViewController中关于nib初始化的函数 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil; 从这个函数的说明我们可以知道,如果你subclass一个UIViewController,不管有没有使用NIB, [super initWithNibName:bundle]这个方法必须被调用, 这个方法会在如下两种情况下被调用: 显示调用, 指定一个nib名称,系统会去找指定

iOS开发中单例对象的标准创建方法

//标准的单例写法 //以创建歌曲的管理者为例进行创建.+(instancetype) sharedQYSongManager { static QYSongsManager *songManager =nil; //采用GDC标准单例实现方法 static dispatch_once_t onceToken; //Executes a block object once and only once for the lifetime of an application. dispatch_onc

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

iOS开发之动画编程的几种方法 IOS中的动画总结来说有五种:UIView<block>,CAAnimation<CABasicAnimation,CATransition,CAKeyframeAnimation>,NSTimer 这里我就总结了一下这五种方法,其实iOS开发中动画的编程都会在这里面变化,所以只要弄懂了这些动画编程就不难了. 一:UIView动画 一般方式 [UIView beginAnimations:@"ddd" context:nil];/

[转]Oracle 10g及pro*c相关问题及解决方法(转)

Oracle 10g及pro*c相关问题及解决方法 2008年08月21日 星期四 上午 11:21 最近一直在进行ORACLE 10g和PRO*C的学习. 其中遇到了不少的问题: 现列于此,已备他用. [注:我的linux版本是RHEL 5,Oracle版本是10g] 1.在ORACLE 10g 安装准备的过程中:缺少libXp.so.6依赖 上网搜过不少文章,但是都不是很好的解决 我自己摸索出一个解决方法: 在RHEL5的安装盘中找到libXp-1.0.0-8.i386.rpm,进行安装后,

树莓派相关-树莓派串口配置方法

树莓派是否能够使用串口呢,答案是肯定的 ,以下是具体方法 1.树莓派的第一排的第三,四,五个分别,也就是下图的6,8,10三个端口分别是地,TX与RX,与被连接设备连接起来.注意树莓派的TX要连接从设备的RX,树莓派的RX要连接树莓派的TX 2.树莓派上只有一个串口,系统默认将它作为调试口,所以不能直接当成普通串口进行编程和使用,需要先将调试口的配置去掉,再使用.此处需要修改两处文件 2.1  修改/boot/cmdline.txt 在终端输入如下指令 sudo nano /boot/cmdli

IOS开发中数据持久化的几种方法--NSUserDefaults

IOS开发中数据持久化的几种方法--NSUserDefaults IOS 开发中,经常会遇到需要把一些数据保存在本地的情况,那么这个时候我们有以下几种可以选择的方案: 一.使用NSUserDefault是最简单直接的一个办法: 1)保存数据: 1 // 实例化一个NSUserDefaults单例对象 2 NSUserDefaults *user = [NSUserDefaults standardUserDefaults]; 3 // 把一个数组array保存在key为allContact的键值