Swift:跳过导航控制器堆栈中的第一个(和更多)屏幕

Kas*_*hif 0 uinavigationcontroller ios swift

我的应用程序中有一个NavigationController.堆栈中的第一个屏幕是TermsConditionVC,然后是LoginVC,然后是MainVC.当我跑步时,它直接进入TermsConditionsVC.

但是,根据用户是否已同意条款条件和/或登录,我希望跳过TermsConditionsVC和/或LoginVC并直接进入MainVC.

在这种情况下我有多个导航控制器是没有意义的,但我不知道如何跳过屏幕(特别是导航控制器堆栈中的第一个屏幕?

小智 5

在你的AppDelegate.swift文件的应用程序(didFinishLaunchingWithOptions)函数中......

首先,在代码中获取Storyboard文件:

let storyboard  = UIStoryboard(name: "your_storyboard_name_here", bundle: nil)
Run Code Online (Sandbox Code Playgroud)

接下来得到你的 ViewController.在您的情况下,它可能是UINavigationController.根控制器就是Storyboard文件中箭头所指向的内容

let root = storyboard.instantiateViewControllerWithIdentifier("root") as! UINavigationController
Run Code Online (Sandbox Code Playgroud)

"root"是您实际需要在故事板文件中命名的内容.所以转到"StoryboardName".storyboard并单击UINavigationController.转到身份检查器(左起第三个选项卡)并输入故事板ID.当你在它的时候,为你要跳转的视图输入一个Storyboard ID

第三

let destination = storyboard.instantiateViewControllerWithIdentifier("main_vc")
Run Code Online (Sandbox Code Playgroud)

第四

root.pushViewController(destination, animated: false)
Run Code Online (Sandbox Code Playgroud)

您将动画设置为false,以便在跳转到该视图时没有默认动画节目.我很确定用户会这么说,看起来事情刚好发生,没有他们按任何东西.它可能会让他们失望

第五

self.window?.rootViewController = root
Run Code Online (Sandbox Code Playgroud)

在此步骤中,将当前窗口设置为root.请记住,root是您刚刚推送MainVC ViewController的UINavigationController.如果你没有设置UINavigationController而你设置了MainVC ViewController,那么你将丢失导航栏.因此,请确保设置根(UINavigationController)而不是ViewController,以便保持导航