How to pop to root view controller with view hiera

2019-06-05 05:15发布

问题:

I have a view hierarchy with multiple UINavigationControllers

Now from a particular view controller, I wanted to pop to window.rootviewcontroller

How can I do that?

I tried

[UIApplication sharedApplication].keyWindow.rootViewController popToRootViewController];

But it does not work. Please suggest.

Please note I want to go to window.rootVC.

THis will not work for me

 [self.navigationController popToRootViewControllerAnimated:YES];

回答1:

Just get the window instance and set the root view controller again, as popToRootViewController only pops to root view controller of particular navigation stack

- (void)popToRoot
{
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    UIWindow *mainWindow = appDelegate.window;
    ViewController *viewControllerObj = [ViewController new];
    UINavigationController *navigationObject = [[UINavigationController alloc] initWithRootViewController:viewControllerObj];
    [mainWindow setRootViewController:navigationObject];
}

Hope this helps.



回答2:

set

UINavigationController *navController = (UINavigationController*)self.window.rootViewController;


[self.navigationController popToRootViewControllerAnimated:YES];