ViewController的生命周期

ViewController分配和释放内存:

1. 

Task:Allocating critical data structures required by your view controller,分配ViewController所需要的最主要的数据结构。

Methods: Initialization methods, 初始化函数

Discussion: Your custom initialization method (whether it is named init or something else) is always responsible for putting your view controller object in a known good state. This includes allocating whatever data structures are needed to ensure proper operation. 你的自定义函数需要将你的ViewController对象初始化好,这包括完成相应操作所需要的数据结构的内存分配。


2. 

Task:Creating your view objects. 创建View对象

Methods:loadView

Discussion:Overriding the loadView method is required only if you intend to create your views programmatically. If you are using storyboards, the views are loaded automatically from the storyboard file. 

只有当你打算通过编程创建Views时你才需要去重写loadView函数。如果你使用storyboards工具,Views将会从storyboard file中自动载入


3. 

Task:Allocating or loading data to be displayed in your view,分配和载入在视图显示时所需要的数据

Methods:viewDidLoad

Discussion:Typically, data objects are provided by configuring your view controller’s properties. Any additional data objects your view controller wants to create should be done by overriding the viewDidLoad method. By the time this method is called, your view objects are guaranteed to exist and to be in a known good state. 

一般情况下,数据对象是通过配置view controller的属性来提供的,view controller需要的一些额外的数据应该通过重写viewDidLoad函数来提供。


4. 

Task:Releasing references to view objects,释放视图对象的引用

Methods:viewDidUnloaddealloc

Discussion:If you maintain strong references to any view objects in your view hierarchy using outlets or other instance variables in your class, you must always release those references when the views are no longer needed. You release a view object by setting your outlet or variable to nil. For more information about when views get released, see “Understanding How Views Are Loaded and Unloaded.”  

在视图层次结构中如果你使用输出口或者其他实例变量维持着视图对象的强引用关系,则必须在不需要这些视图的时候释放这些引用,可以通过设置输出口或者实例变量的值为nil来释放视图。有关视图在何时释放的更过信息可以查看Understanding How Views Are Loaded and Unloaded一节


5. 

Task:Releasing data that is not needed when your view is not displayed. 当视图不再显示时释放那些不需要的数据。

Methods:viewWillUnloadviewDidUnload

Discussion:You can use the viewDidUnload method to deallocate any data that is view specific and that can be re-created easily enough if the view is loaded into memory again. If re-creating the data might be too time-consuming, though, you do not have to release the corresponding data objects here. Instead, you should consider releasing those objects in your didReceiveMemoryWarning method. 

可以使用viewDidUnload函数释放视图拥有的所有数据,当然这些数据在视图重新载入内存时能够很容易重新创建。

如果重建这些数据很消耗时间,那么不必在viewDidUnload函数中释放这些数据,可以考虑在didReceiveMemoryWarning函数释放这些数据对象


6. 

Task:Responding to low-memory notifications

Methods:didReceiveMemoryWarning

Discussion:Use this method to deallocate all noncritical custom data structures associated with your view controller. Although you would not use this method to release references to view objects, you might use it to release any view-related data structures that you did not already release in your viewDidUnload method. (The view objects themselves should always be released in the viewDidUnload method.)

使用该函数释放所有与viewcontroller关联的不重要的数据结构,你不应该使用该函数释放视图对象的引用,而应该使用该函数来释放未能在viewDidUnload函数中释放的与视图相关的数据结构。(视图对象总是应该在viewDidUnload函数中释放)


7.

Task:Releasing critical data structures required by your view controller

Methods:dealloc

Discussion:Override this method only to perform any last-minute cleanup of your view controller class. Objects stored in instance variables and properties are automatically released; you do not need to release them explicitly. 重新该函数来进行view controller的最后清理工作,存储在实例变量和属性中对象会自动释放,你不用直接调用该函数。


总结:

loadView, 在需要用程序自定义view时重新,其他情况可以不用考虑

viewDidLoad,加载显示视图所需要的数据

viewDidUnload,释放视图对象的引用和显示视图时所需要的不难重建的数据

viewWillUnload,释放显示视图时所需要的不难重建的数据

didReceiveMemoryWarning,释放在viewDidUnload函数中没有释放的数据(重建耗时的数据)

dealloc,viewController最后的清理工作,释放视图对象的引用和viewController所需数据结构




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值