Take the 2-minute tour ×

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?

share|improve this question
I don't understand, why can't you release the view controller if its a "subclass with extra functionality"? –  Jasarien Jul 29 '09 at 12:45
Indeed, confused as to why subclasses are forced to be permanently resident... –  Nick Veys Jul 29 '09 at 12:57
I have a 'block' class which subclasses UIViewController and holds content and layout information, which I only want to set up once, when a page is loaded. I want dynamic control of the UIView, since this is what is eating up memory when I have a lot of these "blocks" –  Sam Jul 29 '09 at 13:04
I even tried changing the relationship between Block and UIViewController so that instead of subclassing, block owns a viewcontroller as a member variable. This means I have full control over when the viewcontroller gets deleted. Even when I completely release (gets dealloc'd) the viewcontroller after it has been drawn once, I don't gain any memory back. I need to be able to free up this memory –  Sam Jul 29 '09 at 13:07
add comment

2 Answers

up vote 2 down vote accepted

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.

share|improve this answer
add comment

You can destroy the view in

- viewWillDisappear:(BOOL)animated

and re-create it in

- viewWillAppear:(BOOL)animated

methods of your view controller

share|improve this answer
add comment

Your Answer


