-(void)viewDidUnload
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:LASTUPDATEDLOCATION object:nil];
[self setHeaderViewofWholeTable:nil];
[self setFooterViewofWholeTable:nil];
[self setHeaderActivityIndicator:nil];
[self setFooterActivityIndicator:nil];
[self setLastUpdated:nil];
[self setLblPullDowntoRefresh:nil];
[self setRefreshArrow:nil];
[self setContainerForFormerHeader:nil];
[self setFooterContainer:nil];
[super viewDidUnload];
}
I thought viewDidLoad is called the view itself goes nil. When we set the view to nil, wouldn’t all those things automatically become nil?
What am I misunderstanding?
Some of the other answers cover some of this but there is more to this. A view controller will have its
viewDidLoadmethod called. Typically this results in IBOutlets being retained and possibly lots of other views and objects being allocated and retained. If all goes well, eventually the view controller is deallocated and all of those retained objects need to be released.That’s the simple, happy path. Under low memory conditions, in iOS 5 and earlier, it is possible that a view controller’s view will be unloaded. The
viewDidUnloadmethod was a chance for you to clean up all of the other objects that were retained as part of theviewDidLoadprocess. And here’s the main reason – at some point,viewDidLoadmay be called again to redisplay the view controller’s view.Most people write their
viewDidLoadmethod like it will only ever be called once. And this is OK if theviewDidUnloadmethod properly clears up objects. If it doesn’t, the next call toviewDidLoadwill result in a bunch of memory leaks.ARC pretty much eliminated the issue with the memory leaks if you didn’t clean things up properly in
viewDidUnload. ButviewDidUnloadwas still helpful for cleaning up memory when needed.As was mentioned, as of iOS 6, a view controller’s view in never unloaded in low memory conditions and the
viewDidUnload(andviewWillUnload) methods have been deprecated.If your app still supports iOS 5 along with iOS 6, you still need to make proper use of
viewDidUnload. But if you want to free up memory when needed, usedidReceiveMemoryWarning.