IOS开发基础之──viewDidUnload/dealloc调用分析

     在IOS开发中,内存管理重要性不言而喻,而如何理解viewDidUnload/dealloc调用对理解内存管理的实质有着非常重要的作用,

下面将网友对两者的调用时机摘抄如下,这些分析应该能对理解程序的生命周期有所帮助。


网友一观点:

viewDidLoad并不是需要和 viewDidUnload 成队调用重写,如果你不需要在 viewDidLoad里面作一些特别的事情,比如初始化变量之类的,你就不需要重写 viewDidLoad,注意,是重写。 实际上,你写这些语句,都是重写它爸的方法。 viewDidLoad , viewDidUnload, - init 之类的。如果系统需要,而你又没有重写,它会跟他爸拿这些方法,不用你操心。之所以要重写 viewDidLoad,大部分是因为你要初始化那些变量。因为你在 .h 里面声明某个变量,并不代表它已经是初始化可用的。之所以要有 viewDidUnload 和 dealloc重写,是因为你必然会在现在的 class 里面有一些需要释放的东西,比如某个变量。只要你记住内存管理的规律,这些周期什么的,跟c是一样的。


网友二观点:

viewDidLoad
Called after the controller’s view is loaded into memory.

- (void)viewDidLoad

Discussion
This method is called after the view controller has loaded its associated views into memory. This method is called regardless of whether the views were stored in a nib file or created programmatically in the loadView method. This method is most commonly used to perform additional initialization steps on views that are loaded from nib files.

刚看了下viewDidLoad,也没说什么,就是说当视图载入内存时会被调用.

搞不清楚,小小的总结下我的问题,希望有高人明天现身说法,以及普及一下基础知识.
1.究竟unload后会不会调用dealloc方法? (其实如果调用unload后调用dealloc,那么这个unload就是虚设,没用!另外就是文档上那据不应该在unload释放用户数据或者重量级对象,让人疑惑.觉得这个unload后dealloc不应该被调用)
2.如果unload后不会调用dealloc是不下次在载入视图load方法会不调用(感觉是废话...)


网友三观点:

viewDidUnload
Called when the controller’s view is released from memory.

估计太简单了,我就琢磨这玩意可能可以查到,果不其然,xCode文档帮助搜索出来了.
当view从内存中释放的时候将会得到调用..可能还有疑惑 - dealloc方法呢?

文档接着讨论了如下话题:

Discussion
This method is called as a counterpart to the viewDidLoad method. It is called during low-memory conditions when the view controller needs to release its view and any objects associated with that view to free up memory. Because view controllers often store references to views and other view-related objects, you should use this method to relinquish ownership in those objects so that the memory for them can be reclaimed. You should do this only for objects that you can easily recreate later, either in your viewDidLoad method or from other parts of your application. You should not use this method to release user data or any other information that cannot be easily recreated.

虽然没有直接指出,但是只需要看看最后一句话就可以了:You should not use this method to release user data or any other information that cannot be easily recreated.
你不应该使用这个方法释放用户的数据或者任何重量级对象(构造有一定开销的对象).

间接说明, viewDidLoad也有可能会被多次调用..


不知道我理解的对不?英语确实太烂了.大虾们就点评下吧.我也尽力了!!


网友四观点:

当内存吃紧,没用的view可能会被卸载,这时候control的viewunload会被调用(这个方法告诉我view已经被释放出内存了,看看我是否有引用view中的一些资源从而释放他。我想这个方法应该是这个目的)。但是control呢?是否会dealloc?这个应该取决于代码是如何跑的,一般情况下control都会被引用着,所以应该不会dealloc。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值