iOS6 viewDidUnload Deprecated

Hons picture Hons · Sep 26, 2012 · Viewed 44.5k times · Source

Maybe this is a bad practice, but from the documentations that I read I got the advice to initialize objects in some cases inside the viewDidLoad method and nil it in viewDidUnload.

For example if you have something like adding an Observer

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(filterready:)
                                                 name:@"filterReady"
                                               object:nil];

Now I don't have a method to remove the Observer, however the viewDidLoad becomes called every time the view is shown which results in having multiple observers running after a while and the selector is then called multiple times.

I can fix this by moving some cleaners into the viewDidDisappear method, but now I have some doubts if I'm doing the right thing.

In my sample I have multiple Navigation Controllers that are controlling their subnavigations, but the dealloc is never called for them, even though they are not referenced

Answer

Alex Rouse picture Alex Rouse · Sep 26, 2012

You should use the - (void)didReceiveMemoryWarning and - (void)dealloc methods.

In iOS 6, the viewWillUnload and viewDidUnload methods of UIViewController are now deprecated. If you were using these methods to release data, use the didReceiveMemoryWarning method instead. You can also use this method to release references to the view controller’s view if it is not being used. You would need to test that the view is not in a window before doing this.

So you should check if your view is in the window first, then remove your observer in the didReceiveMemoryWarning