My app setup is a UITabBar with three tabs. In each tab is a different UINavigationController.
In the first tab there is a refresh button – this loads in a load of data from the web (xml). The data is displayed in the three tab bars.
How do I release the UINavigationControllers whenever someone refreshes the data? The reason I want to to this is when then data changes the various tabs might have completely new data to display, so it would be a bit dangerous to keep that screen .. if that makes sense. So I want to completely refresh the UINavigationControllers and show the first view in the navigation stack when they click on a tab again.
Thanks to Ryan for his answer. The way I did was something like this
for(UINavigationController *navController in [self.navigationController.tabBarController viewControllers]) {
NSLog(@"popping %@", [navController title]);
[navController popToRootViewControllerAnimated:NO];
if ([[navController title] isEqualToString:@"Tab2"])
{
Tab2RootController *newRoot2 = [[Tab2RootController alloc] initWithNibName:@"Tab2RootController" bundle:nil];
newRoot2.title = @"Tab2";
[navController setViewControllers:[NSArray arrayWithObject:newRoot2] animated:NO];
//need [newRoot2 release]?
}
if ([[navController title] isEqualToString:@"Tab3"])
{
Tab3RootController *newRoot = [[Tab3RootController alloc] initWithNibName:@"Tab3RootController" bundle:nil];
newRoot3.title = @"Tab3";
[navController setViewControllers:[NSArray arrayWithObject:newRoot3] animated:NO];
//need [newRoot3 release]?
}
}
popToRootViewController will pop all the view controllers on the stack except for the root view controller. If you want to also get rid of the root view controller, you need to replace it entirely, using -[UINavigationController setViewControllers:animated:]. Of course, you’ll need to have your new root view controller set up already. It would look something like this (amending MattLeff’s answer, above):
Note that this will instantly switch the view controllers, with no animations. By setting the entire list of view controllers for each navbar, they will release the old view controllers, and they’ll be deallocated as long as no one else has retained them.