iOS — 初始化方法

2016-12-21  本文已影响249人  AllenYukin
init.png
使用StoryBoard创建绑定vc时,会调用 initWithCode / initawakeFromNib
使用xib或纯代码创建vc时,会调用init / initWithNibName 
"initWithCoder"
When a nib is loaded, the nib loader allocates and initializes all 
objects, then hooks up all of their outlets and actions. 
Because of the order in which this happens, 
you cannot access outlets in your initializer.
you can try, but they will all be nil

当加载nib文件的时候,由nib loader分配并初始化所有对象。
因为sb或者xib中的空间顺序是不确定的。你没有办法在initWithCoder获取到。
你可以试试,但是他们都是nil。
"awakeFromNib"
After all outlets and actions are connected, 
the nib loader sends awakeFromNib to every object in the nib. 
This is where you can access outlets to set up default values 
or do configuration in code. Example

在所有的事件都创建并绑定之后由系统调用的,
这是获取outlet并且赋予默认值的好地方
上一篇下一篇

猜你喜欢

热点阅读