如何设置新的根视图控制器 [英] How to set a new root view controller

查看:34
本文介绍了如何设置新的根视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以设置一个新的根 VC?

I wonder if its possible to set a new root VC?

我的应用程序使用一个 uinavigation 控制器进行初始化,该控制器具有一个作为根 VC 的表视图.

My app gets init with a uinavigation controller that has a table view to be the root VC.

然后从表视图中,我正在运行另一个登录窗口(以模态显示)如果您然后登录,您最终会进入红色 VC/帐户页面.我现在想要做的是将红色 VC 设置为应用程序的新根 VC,并删除所有底层 VC.这样我就可以显示菜单按钮/图标而不是返回"按钮

Then from the table view I am running another segue to a login window (present modally) If you then login you end up in the red VC/account page. What I want to do now is to set the red VC to be the new root VC of the app, and remove all underlying VC's. So that I can show a menu button/icon instead of a "Back" button

我找到了这个,但我不明白如何使用它:

I have found this but I dont understand how to use it:

let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
        let yourViewController: ViewController = storyboard.instantiateViewControllerWithIdentifier("respectiveIdentifier") as! ViewController

        let navigationController = self.window?.rootViewController as! UINavigationController
        navigationController.setViewControllers([yourViewController], animated: true)

但我无法让它工作.那么是否可以将图中红色的vc作为新的根VC.

But I cannot get it to work. So is it possible to make the red vc in the picture act as the new root VC.

推荐答案

斯威夫特 4.2

Swift 4.2

也许你应该试试这个

let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let redViewController = mainStoryBoard.instantiateViewController(withIdentifier: "respectiveIdentifier") as! ViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = redViewController

这篇关于如何设置新的根视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆