views:

428

answers:

2

I have a viewcontroller which can't be released since it is a subclass with extra functionality... Anyway there are many of them, and I need the views to be released when they go offscreen.

removeFromSuperView vanishes the views but they remain in memory. The only way I can get rid of the view from memory is to completely dealloc it. However, when I access viewcontroller.view, it is just going to create the view again, isn't it?

The views themselves don't take up any significant memory until they are drawn for the first time, so...

Ideally I will have an array of views I can [ copy] into viewcontroller.view when they are needed (onscreen) and release completely to dealloc when they go offscreen, so that Cocoa knows to get rid of whatever internal graphics memory it is allocating.

Any thoughts?
Thanks

A: 

You can destroy the view in

- viewWillDisappear:(BOOL)animated

and re-create it in

- viewWillAppear:(BOOL)animated

methods of your view controller

Valerii Hiora
+1  A: 

To release the view from your controller, use:

self.view = nil;

Later on, referencing self.view in a 'get' context will recreate the view (will reload from NIB or call loadView).

Also, never send a dealloc message explicitly to your view or any object.

Jason