swift - 如何设置新的 Root View Controller

标签 swift uinavigationcontroller storyboard

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

我的应用程序通过一个 uinavigation Controller 进行初始化,该 Controller 具有作为根 VC 的 TableView 。

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

我找到了这个,但我不知道如何使用它:

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作为新的root VC。

enter image description here

最佳答案

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

关于swift - 如何设置新的 Root View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33374272/

相关文章:

swift - 在 Swift 中对字典进行排序

iphone - 简单的 uiview 更改不起作用

xamarin - 当我按下后退按钮时,导航栏色调颜色不会改变 Xamarin.iOS

ios - 原型(prototype) Collection View 单元格 "Unable to simultaneously satisfy constraints."警告

ios - WatchKit:捆绑标识符和 WKAppBundleIdentifer 之间有什么区别?为什么它会导致应用程序组无法工作?

swift - 在 HTTP 请求中写入静态字典时未捕获的豁免

ios - 如何正确地子类化从 Swift 中的 xib 加载的 View 类?

ios - 所有 Controller 中带有标签栏和导航栏的应用程序

ios - 使用自适应布局的 iPhone 和 iPad 设备的不同 uitableview 单元格高度

ios - UIViewController 内的 UITableView 使用 Parse PFQueryTableViewController