那么,如何处理ARC并在viewDidUnload上释放属性/子视图? [英] So what's the deal with ARC and releasing properties/subviews on viewDidUnload

查看:76
本文介绍了那么,如何处理ARC并在viewDidUnload上释放属性/子视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在学习iOS开发,并且一直在研究各种教程和书籍.有些是ARC之前的版本,有些是使用ARC的.

I'm still learning iOS development and have been working with various tutorials and books. Some pre-ARC, some with ARC.

在某些情况下,我们被教导要在viewDidUnload上释放ViewController的所有属性和子视图,但在某些情况下,我被告知不再需要这样做.

In some cases, we're taught to release all of the properties and subviews of a ViewController on viewDidUnload but in some cases I've been told this is no longer required.

有人可以给出明确的答案吗?在iOS 5以上版本中,必须要做全部事情:

Can someone give a definitive answer? In iOS 5+, does one have to do the whole:

-(void)viewDidUnload
{
  [super viewDidUnload];
  self.photoViewCell = nil;
  self.photoImageView = nil;
  self.firstNameTextField = nil;
  self.lastNameTextField = nil;
}

...还是不?如果是这样,这仅适用于UIView的后代属性,还是适用于ViewController的所有属性?

... or not? If so, is this only for properties that are descendants of UIView or is it for all properties of the ViewController?

谢谢

推荐答案

因此,每个视图都有许多所有者.当该所有者计数"(通常称为retainCount)达到0时,该对象将被销毁.

So each view has a number of owners. When that "owner count" (usually referred to as the retainCount) reaches 0, that object gets destroyed.

在iOS 5中,我们现在具有弱引用,这实际上意味着不拥有此对象".

In iOS 5, we now have weak references, which essentially means "don't own this object".

在iOS 5之前,您会在头文件中看到

Before iOS 5, in our header files, you'd see

IBOutlet UILabel *myLabel;

并且此标签已添加到XIB文件的视图中.在这种情况下,myLabel有2个所有者:它是超级视图(XIB文件中的视图)和视图控制器(通过具有IBOutlet).调用viewDidUnload get时,视图控制器的视图已释放,因此对myLabel的所有权也消失了.因此,myLabel目前只有1个所有者,即视图控制器.因此,我们需要在viewDidLoad中将其释放,以确保它没有任何所有者并因此被销毁.

And this label was added to the XIB file's view. myLabel has 2 owners in this case: it's superview (the view in the XIB file) and the view controller (by having the IBOutlet). When viewDidUnload get's called, the view controller's view has been released, and therefore it's ownership of myLabel is gone. So myLabel at this point only has 1 owner, the view controller. So we needed to release it in viewDidLoad to make sure it didn't have any owners and so was destroyed.

在iOS 5上,您通常会看到它

With iOS 5, you will often seen this instead

__weak IBOutlet UILabel *myLabel

这就是说我们不希望视图控制器成为myLabel的所有者.因此,唯一的所有者是视图控制器的视图.因此,当调用viewDidUnload get时,视图控制器的视图已被释放,因此其对myLabel的所有权也已释放.在这种情况下,myLabel现在没有所有者,并且其内存已释放.不需要self.myLabel = nil;在那里.

This is saying that we don't want the view controller to be an owner of myLabel. So the only owner is the view controller's view. So when viewDidUnload get's called, the view controller's view has already been released, and so its ownership of myLabel has also been released. In this case, myLabel now has no owners and its memory is released. There is no need for the self.myLabel = nil; there.

因此,对于iOS 5,建议您将所有IBOutlets用作较弱的参考.有了这个,您甚至不需要实现viewDidUnload,因为所有的内存都已经为您照顾好了.

So with iOS 5, the recommendation is to make all of your IBOutlets a weak reference. With this, you don't even need to implement viewDidUnload, as all the memory has been taken care of for you.

但是,即使您使用的是iOS 5,如果IBOutlets不是弱引用,您也需要在viewDidUnload中使用该代码.

But even if you are using iOS 5, if your IBOutlets aren't weak references, you'll need that code in viewDidUnload.

这篇关于那么,如何处理ARC并在viewDidUnload上释放属性/子视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆