I have a custom container UIViewController that has six child UIViewControllers, and a set of tabs that the user interacts with to switch between the child view controllers. The
As I suspected, the problem is not related to the view controller containment code in the question, but rather your adding of the observers (which you discuss in your answer to this question):
[[NSNotificationCenter defaultCenter] addObserverForName:kUpdateEventName object:nil queue:nil usingBlock:^(NSNotification *note) {
// do stuff when update happen
}];
And that you tried to remove it with
[[NSNotificationCenter defaultCenter] removeObserver:self name:kUpdateEventName object:nil];
So, there are two problems:
If you use addObserverForName:object:queue:
, this is not the correct way to remove this observer. Instead, define a property to keep track of the observer:
@property (nonatomic, weak) id notification;
Then save a reference to that observer when you create it:
self.notification = [[NSNotificationCenter defaultCenter] addObserverForName:kUpdateEventName object:nil queue:nil usingBlock:^(NSNotification *note) {
// do something
}];
And when you want to remove it, use this reference:
[[NSNotificationCenter defaultCenter] removeObserver:self.notification];
That would ensure that the observer would be removed properly.
The failure of the child view controllers to be released while this observer was in place means that this block that you passed to addObserverForName:object:queue:
must of had a reference to self
. If you tried to remove this observer correctly (as shown above) in dealloc
, you would still have a strong reference cycle (formerly known as a retain cycle). This is resolved in a number of ways, but the most robust pattern is to prevent the strong reference cycle in the first place by using the weakSelf
pattern:
typeof(self) __weak weakSelf = self;
self.notification = [[NSNotificationCenter defaultCenter] addObserverForName:kFooNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
// use `weakSelf` in this block; not `self`
}];
My original answer is below:
While Srikanth is right that after addChildViewController
, you should call didMoveToParentViewController:self
and before removeFromParentViewController
you should call willMoveToParentViewController:nil
. But that's not your problem. In fact, I used a variation of your code (even without the dealloc
), and the children controllers are released fine.
Bottom line, I suspect your problem rests elsewhere, probably a retain cycle somewhere. For example, do you children have strong reference to the parent? Are you using recurring timers? You reference some tabs. You're not using a tab bar controller, are you? It's got to be something like that.
[Refer to revision history if you want to see rest of the original answer with code snippets and minor details on the OP's code sample]