Reputation: 1499
I am using the following code to switch between two tabs:
-(void)switchPage:(UIButton *)sender{
DLog(@"");
DLog(@"child view controllers %lu", (unsigned long)self.childViewControllers.count);
switch(sender.tag){
case 0:{
for(id object in self.childViewControllers){
if([object isKindOfClass:[QuestionsVC class]]){
[questionsTab willMoveToParentViewController:nil];
[questionsTab.view removeFromSuperview];
[questionsTab removeFromParentViewController];
questionsTab = nil;
}
}
questionsTab = [[QuestionsVC alloc] init];
[self addChildViewController:questionsTab];
[self.view addSubview: questionsTab.view];
[questionsTab didMoveToParentViewController:self];
[self.view bringSubviewToFront:tabBarView];
[self drawGrayLineLayer];
break;
}
case 1:{
for(id object in self.childViewControllers){
if([object isKindOfClass:[AnswersVC class]]){
[answersTab willMoveToParentViewController:nil];
[answersTab.view removeFromSuperview];
[answersTab removeFromParentViewController];
answersTab = nil;
}
}
answersTab = [[AnswersVC alloc] init];
[self addChildViewController:answersTab];
[self.view addSubview: answersTab.view];
[answersTab didMoveToParentViewController:self];
[self.view bringSubviewToFront:tabBarView];
[self drawGrayLineLayer];
break;
}
}
The curious thing is that the count of childVCs for the root viewcontroller (the tab bar), remains constant while none of the childviewcontrollers are released.
What is the problem?
Below is an instruments snapshot telling me that each viewcontroller gets re-allocated each time the code runs, and the old one does not get deallocated.
Upvotes: 1
Views: 889
Reputation: 1499
What was the problem for me was that I initialized other objects from the child viewcontrollers that then had a delegate property, and that delegate was strong
so the objects retained its parent, the delegate -- the viewcontrolller, which created retain cycles.
Delegates should almost always be weak
(or almost never strong
) from what i have learned
Upvotes: 1