One way to do this (and perhaps the simplest) would be to use a UINavigationController
and push UIViewController
s onto it.
Then you can simply call: [[self navigationController] popToRootViewControllerAnimated:YES];
Another more complex way to handle this if you really want modal view controllers would be to set up the UIViewControllers with callbacks to register that they should close immediately on next appearance.
-(void)closeImmediately
{
mCloseImmediately = YES;
}
-(void)viewWillAppear:(BOOL)animated
{
if(mCloseImmediately)
{
[self dismissModalViewControllerAnimated:NO];
return;
}
// ... other normal setup code
}
Doing this in viewWillAppear
and without animation means that this UIViewController
never gets displayed.