转自:http://blog.csdn.net/sxfcct/article/details/7840969

每个ios开发者对loadView和viewDidLoad肯定都很熟悉,虽然这两个函数使用上真的是非常简单,但是和类似的initWithNibName/awakeFromNib/initWithCoder放在一起还是非常容易让人混淆的.

一、loadView

永远不要主动调用这个函数。view controller会在view的property被请求并且当前view值为nil时调用这个函数。如果你手动创建view,你应该重载这个函数,且不要在重载的时候调用[super loadview]。如果你用IB创建view并初始化view controller,那就意味着你使用initWithNibName:bundle:方法,这时,你不应该重载loadView函数。

这个方法系统的默认实现是这样:

1;寻找有关可用的nib文件的信息,根据这个信息来加载nib文件       //所以,nib的加载过程是在loadview中完成的哦。

2;如果没有有关nib文件的信息,默认创建一个空白的UIView对象,然后把对象成赋值给viewcontroller的主view。

所以,如果你决定重载这个函数时,你也应该完成这些步骤:

把子类的view赋给view属性(property)(你create的view必须是唯一的实例,并且不被其他任何controller共享),而且你重载的这个函数不应该调用super,这个也是为了保持主view与controller的单一映射关系。

二、viewDidLoad

这个函数在controller加载了相关的views后被调用,而不论这些views存储在nib文件里还是在loadView函数中生成。

这个函数的作用主要是让你可以进一步的初始化你的views。viewDidLoad通常负责的是view及其子view被加载进内存之后的数据初始化的工作,即视图的数据部分的初始化。在iOS 3.0以及更高版本中,你应该重载viewDidUnload函数来释放任何对view的引用或者它里面的内容(子view等等)。

其多数情况下是做nib文件的后续工作。

三、viewDidUnload

这个函数是viewDidLoad的对立函数。在程序内存欠缺时,这个函数被controller调用,来释放它的view以及view相关的对象。由于controller通常保存着view以及相关object的引用,所以你必须使用这个函数来放弃这些对象的所有权以便内存回收。但不要释放那些难以重建的数据。

通常controller会保存nib文件建立的views的引用,但是也可能会保存着loadView函数创建的对象的引用。最完美的方法是使用合成器方法:

1
self.myCertainView = nil;

这样合成器会release这个view,如果你没有使用property,那么你得自己显式释放这个view。

四、结论

所以流程应该是这样:

loadView来加载view(无论nib文件或自定义的views)到内存 ——>viewDidLoad函数进一步初始化这些view (通常是侧重于数据data的初始化)——>内存不足时,调用viewDidUnload函数释放views

—->当需要使用view时又回到第一步

如此循环

需要厘清两个概念,创建一个类和实例化一个类.在XCode中创建一个类和实例化一个类很容易区分,但是在IB(Interface Builder)中有时候就会迷糊.其实也很好区分,孤零零地创建了一个nib文件,没有和其他可被实例化的类有直接或间接关系的时候,这个类或这些类(一个nib文件也可能包含多个类)是没有机会被实例化的,所以这种情况只是通过ib创建了一个类,而没有实例化.真正的实例化还需要通过在Xcode用代码来读取这个nib文件.知道这两这的区别后这些方法也就容易辨认多了

viewDidLoad其实没什么可混淆的,无论通过什么途径加载(Xcode或者IB,这里的加载属于实例化)完view后肯定会执行这个方法.

loadView需要分两种情况.当你通过Xcode实例化一个类的时候就需要自己在controller中实现这个方法.而在IB中实例化就不需要实现它.

initWithNibName这个方法是在controller的类在IB中创建,但是通过Xcode实例化controller的时候用的.

initWithCoder是一个类在IB中创建但在xocdde中被实例化时被调用的.比如,通过IB创建一个controller的nib文件,然后在xcode中通过initWithNibName来实例化这个controller,那么这个controller的initWithCoder会被调用.

awakeFromNib 
当.nib文件被加载的时候,会发送一个awakeFromNib的消息到.nib文件中的每个对象,每个对象都可以定义自己的awakeFromNib函数来响应这个消息,执行一些必要的操作。也就是说通过nib文件创建view对象时执行awakeFromNib.awakeFromNib在loadView前被调用。

viewDidLoad 
当view对象被加载到内存后就会执行viewDidLoad,所以不管通过nib文件还是代码的方式创建对象都会执行viewDidLoad 。

- (void)loadView

Description Creates the view that the controller manages.

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.

If the view controller has an associated nib file, this method loads the view from the nib file. A view controller has an associated nib file if the nibName property returns a non-nil value, which occurs if the view controller was instantiated from a storyboard, if you explicitly assigned it a nib file using the initWithNibName:bundle: method, or if iOS finds a nib file in the app bundle with a name based on the view controller's class name. If the view controller does not have an associated nib file, this method creates a plain UIView object instead.

If you use Interface Builder to create your views and initialize the view controller, you must not override this method.

You can override this method in order to create your views manually. If you choose to do so, assign the root view of your view hierarchy to the view property. The views you create should be unique instances and should not be shared with any other view controller object. Your custom implementation of this method should not call super.

If you want to perform any additional initialization of your views, do so in the viewDidLoad method.

- (void)viewDidLoad

Description Called after the controller's view is loaded into memory.

This method is called after the view controller has loaded its view hierarchy into memory. This method is called regardless of whether the view hierarchy was loaded from a nib file or created programmatically in the loadView method. You usually override this method to perform additional initialization on views that were loaded from nib files.

- (void)awakeFromNib

Description Prepares the receiver for service after it has been loaded from an Interface Builder archive, or nib file.

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.

You must call the super implementation of awakeFromNib to give parent classes the opportunity to perform any additional initialization they require. Although the default implementation of this method does nothing, many UIKit classes provide non-empty implementations. You may call the super implementation at any point during your own awakeFromNib method.

Note:Note

During Interface Builder’s test mode, this message is also sent to objects instantiated from loaded Interface Builder plug-ins. Because plug-ins link against the framework containing the object definition code, Interface Builder is able to call their awakeFromNib method when present. The same is not true for custom objects that you create for your Xcode projects. Interface Builder knows only about the defined outlets and actions of those objects; it does not have access to the actual code for them.

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. For more detailed information about the steps followed during the nib-loading process, see Nib Files in Resource Programming Guide.

Important:Important

Because the order in which objects are instantiated from an archive is not guaranteed, your initialization methods should not send messages to other objects in the hierarchy. Messages to other objects can be sent safely from within an awakeFromNib method.

Typically, you implement awakeFromNib for objects that require additional set up that cannot be done at design time. For example, you might use this method to customize the default configuration of any controls to match user preferences or the values in other controls. You might also use it to restore individual controls to some previous state of your application.

转载于:https://www.cnblogs.com/UncleDraw/p/5703569.html

转:awakeFromNib/loadView/viewDidLoad总结相关推荐

  1. iphone开发基础:loadView/viewDidLoad/initWithNibName/awakeFromNib/initWithCoder的用法

    每个ios开发者对loadView和viewDidLoad肯定都很熟悉,虽然这两个函数使用上真的是非常简单,但是和类似的initWithNibName/awakeFromNib/initWithCod ...

  2. awakeFromNib,viewDidLoad

    //1,在运行程序的时候,awakeFromNib是在viewDidLoad之前发生的. //就是如果想要对view本身进行一些例如背景颜色,透明度之类的设置就只能在awakeFromNib里面进行, ...

  3. awakeFromNib和viewDidLoad的区别

    1.使用nib或者故事版构建的界面会调用awakeFromNib,而且自定义的任何View都有这个方法,但是viewDidLoad只有UIViewController派生类的类对象才可以有,比如设置U ...

  4. ⑪(面试篇 2/3)、《史上最全iOS八股文面试题》2022年,金三银四我为你准备了,iOS《1000条》笔试题以及面试题(包含答案)。带面试你过关斩将,(赶紧过来背iOS八股文)

    iOS面试题 一共分为笔试题和面试题两部分 笔试题 一共分为10个 总共613题 面试题 一共400题 笔试题 一个10个系列 分别为 ①(语法篇) 共147题 已更新 ②(常识篇) 共72题 已更新 ...

  5. IOS-awakeFromNib和viewDidLoad

    awakeFromNib 当.nib文件被加载的时候,会发送一个awakeFromNib的消息到.nib文件中的每个对象,每个对象都可以定义自己的 awakeFromNib函数来响应这个消息,执行一些 ...

  6. 探究 UIViewController 生命周期

    由于种种原因,掘金等第三方平台博客不再保证能够同步更新,欢迎移步 GitHub:github.com/kingcos/Per-.谢谢! Lifecycle of UIViewController in ...

  7. iOS控制器与视图加载方法

    转载记录, 请看原文: 1. iOS中的各种加载方法(initWithNibName,loadNibNamed,initWithCoder,awakeFromNib等等)简单使用   http://w ...

  8. iOS开发入门——基础篇一

    文章目录 1. iOS应用生命周期 1.1 应用程序的架构 1.2 iOS应用的5种状态: 1.2.1做一些操作对应的生命周期调用的顺序 1.2.2全面的生命周期执行流程图 1.3 Main函数入口 ...

  9. 5.新浪微博Swift项目第五天

    2019独角兽企业重金招聘Python工程师标准>>> 第五天 昨天我们主要体验了access_token的作用,今天我们将学习怎么从新浪官方获取access_token 今天我们需 ...

  10. iOS开发-面试总结(九)

    iOS面试指导 一 经过本人最近的面试和对面试资料的一些汇总,准备记录这些面试题,以便ios开发工程师找工作复习之用,本人希望有面试经验的同学能和我同时完成这个模块,先出面试题,然后会放出答案. 1. ...

最新文章

  1. 在项目中配置Nexus Repository的信息
  2. Ubuntu学习——第一篇
  3. 豆瓣9.8分,周志明的《凤凰架构》,高屋建瓴,推荐(送书)
  4. 洛谷——P1056 排座椅
  5. Android之如何成为Android高手
  6. 计算机缺少fixos.dll,fix_toolbox.dll
  7. 4-30 HTML 细节摘录
  8. Asp.net网站开发架构设计要求
  9. sql数据库性能指标_SQL Server磁盘性能指标–第2部分–其他重要的磁盘性能指标
  10. Qt 学习之路 :Qt 线程相关类
  11. FFMPEG源码分析:avformat_open_input()(媒体打开函数)
  12. 监督分类空白处也被分类了_如何兼容自训练与预训练:更高效的半监督文本分类模型...
  13. cad隐藏图层命令快捷键_99%的人没用过CAD图层这个功能!
  14. java设置拨号界面_JS+CSS实现仿触屏手机拨号盘界面及功能模拟完整实例
  15. 关于ECharts中调整容器与图表的位置关系
  16. 思科计算机网络(1)计算机网络概述
  17. 腾讯云修改邮箱登录方式
  18. 用java画跳棋棋盘
  19. 计算机房里面味道很大,搞笑段子:我负责单位的计算机房,同事的计算机有问题来向我讨教...
  20. 矢量线的一种栅格化算法

热门文章

  1. Linux运维工程师,你能把iptables玩转麽?
  2. Python 从入门到进阶
  3. 从我的公众号谈执行力
  4. 求区间不同数的个数 树状数组||莫队算法
  5. 怎样在IIS下配置PHP
  6. .AsEnumerable() 和 .ToList() 的区别:
  7. Repeater——数据库控件学习
  8. BroadcastReceiver的学习和使用实例
  9. oracle用UNION-ALL 替换UNION ( 如果有可能的话)
  10. 面试中的排序算法总结