ViewController 生命周期重要方法

先上图  

(图片来源,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:

  1. 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.

  2. 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. 如果数据需要保存,保存一下数据。











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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值