Sam
Sam

Reputation: 3659

possible to release viewcontroller.view without releasing viewcontroller?

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

Upvotes: 1

Views: 1331

Answers (2)

Jason
Jason

Reputation: 28600

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.

Upvotes: 2

Valerii Hiora
Valerii Hiora

Reputation: 1552

You can destroy the view in

- viewWillDisappear:(BOOL)animated

and re-create it in

- viewWillAppear:(BOOL)animated

methods of your view controller

Upvotes: 0

Related Questions