ViewController 生命周期重要方法
2015-07-03 22:31
375 查看
先上图
(图片来源,iGD Game Development (Summer School 2012) )
![](http://img.blog.csdn.net/20150703223353554?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
可以看到,在ViewController加载的时候,有两个重要的步骤, loadView 和viewDidLoad
名字上可以看出来是
loadView是加载View对象
viewDidLoad是View已经加载完成了
在ViewController被实例化之后,会加载所有可以看见的View对象,
1. 当ViewController在Storyboard中使用,loadView是被自动调用的,会把Storyboard上的对应的View对象赋值给view属性。
2.在自定义的代码中,返回一个空的UIView对象(这里要求手动创建View对象,并且把这个View对象赋值给view属性)。
viewDidLoad方法是给ViewController的子类调用的,用于增加而外的资源(View),这些资源是初始化必须的。
以下是Apple的原文
(见 View Controller Programming Guide for iOS)
A View Controller Instantiates Its View Hierarchy When Its View is Accessed
Whenever some part of your app asks the view controller for its view object and that object is not currently in memory, the view controller loads the view hierarchy into memory and stores it in its view property
for future reference. The steps that occur during the load cycle are:
The view controller calls its
loadView method. The default implementation of theloadView method
does one of two things:
If the view controller is associated with a storyboard, it loads the views from the storyboard.
If the view controller is not associated with a storyboard, an empty UIView object is created and
assigned to the view property.
The view controller calls its
viewDidLoad method, which enables your subclass to perform any additional load-time tasks.
在应用程序收到内存警告的时候,调用 didReceiveMemoryWaring
这个方法主要是在手机发出内存警告的时候调用,主要作用是释放屏幕的view和相关的资源。这个方法不是要求销毁controller。
在iOS 6 (以及6以后),释放屏幕的view是可选的;在iOS5 (以及5以前),系统自动释放屏幕的View。
根绝Apple自己的文档,屏幕的View占用的内存是很大的。
在实现这个方法的时候,主要遵循以下步骤:
1. 调用基类(super class)的 didReceiveMemoryWarning方法。
2. 清理ViewController的其他资源。
3. 检查view是不是还在被显示([self.view window] == nil),如果还在屏幕显示,清理这个view(包括其subview)的所有强链接(strong references)
4. 如果数据需要保存,保存一下数据。
(图片来源,iGD Game Development (Summer School 2012) )
可以看到,在ViewController加载的时候,有两个重要的步骤, loadView 和viewDidLoad
名字上可以看出来是
loadView是加载View对象
viewDidLoad是View已经加载完成了
在ViewController被实例化之后,会加载所有可以看见的View对象,
1. 当ViewController在Storyboard中使用,loadView是被自动调用的,会把Storyboard上的对应的View对象赋值给view属性。
2.在自定义的代码中,返回一个空的UIView对象(这里要求手动创建View对象,并且把这个View对象赋值给view属性)。
viewDidLoad方法是给ViewController的子类调用的,用于增加而外的资源(View),这些资源是初始化必须的。
以下是Apple的原文
(见 View Controller Programming Guide for iOS)
A View Controller Instantiates Its View Hierarchy When Its View is Accessed
Whenever some part of your app asks the view controller for its view object and that object is not currently in memory, the view controller loads the view hierarchy into memory and stores it in its view property
for future reference. The steps that occur during the load cycle are:
The view controller calls its
loadView method. The default implementation of theloadView method
does one of two things:
If the view controller is associated with a storyboard, it loads the views from the storyboard.
If the view controller is not associated with a storyboard, an empty UIView object is created and
assigned to the view property.
The view controller calls its
viewDidLoad method, which enables your subclass to perform any additional load-time tasks.
在应用程序收到内存警告的时候,调用 didReceiveMemoryWaring
这个方法主要是在手机发出内存警告的时候调用,主要作用是释放屏幕的view和相关的资源。这个方法不是要求销毁controller。
在iOS 6 (以及6以后),释放屏幕的view是可选的;在iOS5 (以及5以前),系统自动释放屏幕的View。
根绝Apple自己的文档,屏幕的View占用的内存是很大的。
在实现这个方法的时候,主要遵循以下步骤:
1. 调用基类(super class)的 didReceiveMemoryWarning方法。
2. 清理ViewController的其他资源。
3. 检查view是不是还在被显示([self.view window] == nil),如果还在屏幕显示,清理这个view(包括其subview)的所有强链接(strong references)
4. 如果数据需要保存,保存一下数据。
相关文章推荐
- const修饰指针时候的注意事项
- C++实现顺序栈之两栈共享存储空间
- 二进制的妙用
- 自开发实现Quartz Web管理
- 【HM】第8课:Request与Response详解
- 20150630_UI之按钮UIButton使用
- Pascal 排序算法
- .net MVC简介、项目中每个文件夹的功能
- Win8.1屏幕亮度自动调节关闭方法
- iOS 计算 日期 距离 当前 系统的日期 相差 多少
- Codeforces Round #310 (Div. 1) B. Case of Fugitive
- Android实践-使用ViewPager和Fragment实现带Tab导航条的仿新闻类页面
- 【足迹】终于学完了web组件设计的视频课程
- BZOJ3024 : [Balkan2012]balls
- ACPI 高级配置与电源接口 (介绍的比较详细)
- 黑马程序员——Java基础---线程的另一个总结(4)--线程通信(互斥),线程范围的共享变量
- 矩阵清零(算法)
- 非常不错的一款打字代码效果
- [iOS]使用autolayout的时候会有明明设置和父视图左右间距为0但却还有空隙问题
- Word标题怎么更改