6

I am using storyboards in my iOS. The first screen is the login screen. When a user logs out or gets logged out, he may be on a screen in a deep hierarchy.

For example: login view controller => modal view controller => tab bar controller => nav controller => view controller => view controller. I want to go all the way back from the top-most view controller to the bottom one.

Edit: Here's a diagram of the view hierarchy:enter image description here

Thanks!

4

3 回答 3

5

我为 UIViewControllers 编写了一个似乎正在工作的类别:

- (void) popToInitialViewController
{
    UIViewController *vc;
    if (self.navigationController != nil) {
        vc = self.navigationController;
        [self.navigationController popToRootViewControllerAnimated:NO];
        [vc popToInitialViewController];
    }
    else if (self.tabBarController != nil) {
        vc = self.tabBarController;
        [vc popToInitialViewController];
    }
    else if (self.presentingViewController != nil) {
        vc = self;

        while (vc.presentingViewController != nil)
            vc = vc.presentingViewController;

        [vc dismissModalViewControllerAnimated:NO];

        [vc popToInitialViewController];
    }
}

评论表示赞赏:)

于 2012-10-22T19:38:02.193 回答
2

假设所有内容都被推送到导航堆栈上,这应该可以工作:

[self.navigationController popToRootViewControllerAnimated:YES];
于 2012-10-22T17:00:00.187 回答
0

就我而言,我直接设置窗口rootViewController如下:

func popToInitialScreen() {
    let window = UIApplication.shared.keyWindow
    window.rootViewController = nil
    window.rootViewController = // init your initial view controller
    window.makeKeyAndVisible()
}

PS keyWindow 在 iOS 13 中已弃用,不应用于支持多个场景的应用程序,因为它会在所有连接的场景中返回一个键窗口。

于 2020-03-19T03:32:47.487 回答