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
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
First of all, even when viewDidUnload
was not deprecated, you must have had to unregister that notification in viewDidUnload
AND dealloc
. Even before iOS 6, viewDidUnload
is NOT called in most circumstances; only in low memory situations. So if you had only put it in viewDidUnload
and not dealloc
before, it would not have been unregistered, and it would probably have crashed when it was deallocated and received a notification. So you must have had to put it in dealloc
before, for it to have worked correctly.
Second, if you did it correctly before, you don't need to do anything extra for it to work correctly in iOS 6. The only difference in iOS 6 is that views are no longer unloaded at all (even in low memory situations). So it's the same as in iOS 5 when you didn't run into a low memory situation. Since views are not unloaded, viewDidLoad
will only be called once, so your notification will only be registered once. It will be unregistered in dealloc
, as you must have put it for it to have worked correctly.
Alex answer is good. But I like proper pairing. For that reason, unless the view need to be notified when it's not even seen, I usually add notification at viewWillAppear and viewDidDisappear
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With