简体   繁体   中英

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? Does it release the memory? I thought you put [myOutlet release] in - (void)dealloc to release the memory. 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. When you assign it to nil , that therefore releases the object that it previously pointed to.

Typically viewDidUnload method is called when low memory warning occurred and controller's view gets unloaded (controller itself stays in memory). 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.

You still need to release you outlets in dealloc method even if you release them in viewDidUnload

For more details see this SO question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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