How to pop to root view controller with view hiera

2019-06-05 05:22发布

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];

2条回答
迷人小祖宗
2楼-- · 2019-06-05 05:59

set

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


[self.navigationController popToRootViewControllerAnimated:YES];
查看更多
一纸荒年 Trace。
3楼-- · 2019-06-05 06:04

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.

查看更多
登录 后发表回答