简体   繁体   English

在iPhone上释放主视图的所有保留子视图

[英]Release any retained subviews of the main view on iPhone

I saw this code in the project template and a few other sample projects. 我在项目模板和其他一些示例项目中看到了这段代码。

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

Can someone explain to me what self.myOutlet = nil does? 有人可以向我解释一下self.myOutlet = nil有什么用? Does it release the memory? 它会释放内存吗? I thought you put [myOutlet release] in - (void)dealloc to release the memory. 我以为你把[myOutlet release]放在 - (void)dealloc中释放内存。 what is = nil? 什么是=零? and when do you need to do this? 你什么时候需要这样做?

If myOutlet is specified as a @property (retain) then whenever you assign it to point to a new object, the old one will be released and the new one retained. 如果将myOutlet指定为myOutlet @property (retain)则无论何时将其指定为指向新对象,都将释放旧对象并保留新对象。 When you assign it to nil , that therefore releases the object that it previously pointed to. 将它指定为nil ,会释放它先前指向的对象。

Typically viewDidUnload method is called when low memory warning occurred and controller's view gets unloaded (controller itself stays in memory). 通常在发生低内存警告并且控制器视图被卸载时调用viewDidUnload方法(控制器本身保留在内存中)。 As David pointed in his answer usually outlets are being retained by controller so they stay in memory even after the main view is gone - that reduces the benefits of unloading the view. 正如David在他的回答中指出的那样,控制器通常会保留插座,因此即使在主视图消失后它们也会留在内存中 - 这会降低卸载视图的好处。

You still need to release you outlets in dealloc method even if you release them in viewDidUnload 即使您在viewDidUnload释放它们,仍然需要以dealloc方法释放出口

For more details see this SO question 有关详细信息,请参阅此SO问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM