释放保留视图的最佳做法? [英] Best practices for releasing retained views?

查看:111
本文介绍了释放保留视图的最佳做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在iOS 4.x或更低版本中释放保留在viewDidLoad中的视图的正确(最佳方法)吗?还有什么需要考虑的吗?

Is this the correct (best?) way to release views retained in viewDidLoad, in iOS 4.x or lower? Is there anything else to consider?

- (void) viewDidUnload
{
    [super viewDidUnload];
    [self releaseViews];
}

- (void) dealloc {
    [self releaseViews];
    [super dealloc];
}

#define SAFE_RELEASE(a) [a release]; a = nil;

- (void) releaseViews {
    SAFE_RELEASE(myView1);
    SAFE_RELEASE(myView2);
    SAFE_RELEASE(myView3);
}

推荐答案

-dealloc是正确的,并且-viewDidUnload将起作用,但是通常保留的视图仅在-viewDidUnload中被清除而不被释放.这似乎也是Apple的做法,这是当您通过Assistant编辑器创建自动生成的IBOutlet时,它们已经融入Xcode的内容.

The -dealloc is correct and the -viewDidUnload will work, but typically retained views are only nilled in -viewDidUnload and not released. This seems to be Apple's practice as well and it is what they've baked into the Xcode when you create an auto-generated IBOutlet via the Assistant editor.

对于自动生成的IBOutlet,自动生成的-viewDidUnload如下所示:

For auto-generated IBOutlets, the auto-generated -viewDidUnload looks like this:

- (void)viewDidUnload {
    [self myView1:nil];
    [self myView2:nil];
    [self myView3:nil];
    [super viewDidUnload];
}

另外,来自 Apple文档-viewDidUnload:

放弃任何对象(包括出口中的对象)所有权的首选方法是使用相应的访问器方法将对象的值设置为nil.但是,如果您没有给定对象的访问器方法,则可能必须显式释放该对象

The preferred way to relinquish ownership of any object (including those in outlets) is to use the corresponding accessor method to set the value of the object to nil. However, if you do not have an accessor method for a given object, you may have to release the object explicitly

所以,您去了.如果您的插座具有与之关联的属性(它们都应该不再存在),则在-viewDidUnload中将其设置为零-但不要释放它.当您考虑综合访问器中实际发生的情况时,这很有意义.代码看起来像这样:

So, there you go. If your outlet has a property associated with it (which they all should anymore), then nil it in -viewDidUnload -- but don't release it. This makes sense when you consider what is actually happening in a synthesized accessor; the code looks something like this:

- (void) setMyView1 : (UIView *) view {
   if (myView1) // the associated IVAR is already set
      [myView1 release];

   myView1 = [view retain];
}

如您所见,将synthesize属性设置为nil会隐式释放保留的对象.

As you can see, setting a synthesize property to nil implicitly releases the retained object.

文档中关于-dealloc的信息:

如果实现此方法但正在为iOS 2.x构建应用程序,则dealloc方法应释放每个对象,但也应在调用super之前将对该对象的引用设置为nil.

If you implement this method but are building your application for iOS 2.x, your dealloc method should release each object but should also set the reference to that object to nil before calling super.

除非您支持iOS2.x,否则无需在dealloc中将对象设置为nil.

Unless you are supporting iOS2.x, there is no need to set objects to nil in dealloc.

因此,总结一下有关-viewDidUnload-dealloc的Apple文档:

So, to summarize Apple's docs regarding -viewDidUnload and -dealloc:

  • -viewDidUnload中,没有属性(包括IBOutlet属性),但是释放它们
  • -dealloc发布属性中,但不要将它们设置为零(除非为2.x构建).
  • In -viewDidUnload, nil properties (including IBOutlet properties), but don't release them
  • In -dealloc release properties, but don't nil them (unless building for 2.x).

这篇关于释放保留视图的最佳做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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