I need to free the view of my mainViewController I have on a navigationViewController in order to avoid the memory-warnings that I’ve been receiving.
The good practice loading the view is in the ‘loadview’ method, but when I try to dealloc the view (when I go to the next controller of the stack) doing “self.view = nil” it calls again the ‘loadview’ method so nothing happens.
I solved this problem loading the view in the ‘viewWillAppear’ method, but, my question is:
Is that procedure correct?
If not (that’s what I think) how can I release that view to free its memory?
Which is the best (or more correct) way to do it?
This is the code:
‘loadView’ without loading the view
- (void)loadView
{
[super loadView];
(some more code)
}
The view it is loaded here
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (!pral)
{
pral = [[TouchDrawView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self setView:pral];
[pral release];
}
(some more code)
}
This is the call to the next viewController of the stack
- (void)menus:(id)sender
{
opcions *ops = [[opcions alloc] init];
[[self navigationController] pushViewController:ops animated:YES];
[ops release];
//Here I'm trying to free memory releasing the view
[pral removeFromSuperview];
pral = nil;
self.view = nil;
}
Thanks
A better hook to build custom subviews is viewDidLoad.
Minimally, you should free the retained subviews (pre-ARC) in didReceiveMemoryWarning and in dealloc.
No need to release when you push to the new viewController. Your old one is still there, underneath.