问题
I have a navigation controller. I first pushed a VC, then presented 12 modals VC's. Now I want to pop to root viewController. How can I do that? Please help me out.
回答1:
You will need to dismiss the 12 modal views that you have presented. popViewController or popToRootViewController will not work.
回答2:
Dismiss your modal views to get to the root view.
回答3:
Sorry, I am not sure I do understand your question correctly.
But is UINavigationController's
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
what you are searching for?
回答4:
If you have navigation controller and want to enjoy the benefits of popping back multiple levels in a single command, then you should be using pushViewController
(or push segues) not presentViewController
(or modal segues).
As others have pointed out, if you were correcting pushing to the subsequent controllers, then you could pop back via:
[self.navigationController popToRootViewControllerAnimated:YES];
回答5:
Tell the pushed VC to dismissViewControllerAnimated:completion:
. This will dismiss all the presented VCs. Now you can pop the pushed VC.
回答6:
Keep a first NavigationController in an instance variable of AppDelegate. In AppDelegate.h
@property (nonatomic, retain) UINavigationController *navigationControllerFirst;
In the RootViewController viewDidLoad add the following.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
app.navigationControllerFirst = self.navigationController;
}
Then, to achieve what you wanted, in the 12th presented modal add the following function to go back to RootViewController.
- (IBAction)GoToHome:(id)sender {
AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[app.navigationControllerFirst dismissViewControllerAnimated:YES completion:^{
[app.navigationControllerFirst popToRootViewControllerAnimated:YES];
}];
}
来源:https://stackoverflow.com/questions/13661388/how-to-pop-when-first-pushed-vc-then-presented-modal-vc-12-times