【问题标题】:How can we change rootview of navigationController? [duplicate]我们如何改变navigationController的rootview? [复制]
【发布时间】:2018-08-25 00:04:30
【问题描述】:

最初我的应用程序以我在 AppDelegate 文件中设置的视图控制器 (TabBarView) 开始。

     window = UIWindow(frame: UIScreen.main.bounds)
        window?.makeKeyAndVisible()
        let tabBarView = TabBarView()
navigationController = UINavigationController(rootViewController: tabBarView)
        window?.rootViewController = navigationController

稍后在应用程序中,当满足几个条件时,我想将我的 rootController 更改为新的(MusicPage)。

目前我创建了一个新的 NavigationController 对象来显示我的 (MusicPage)viewController 但是我认为我的 (TabBarView)viewController 可能在后台运行并消耗内存。

如果有人告诉我如何更改我的初始 rootViewController,我将不胜感激。

谢谢。 :)

【问题讨论】:

    标签: ios swift3 uinavigationcontroller viewcontroller


    【解决方案1】:

    答案可以在

    上找到

    Set rootViewController of UINavigationController by method other than initWithRootViewController

    let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    var homeViewController = mainStoryboard.instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController
    let nav = UINavigationController(rootViewController: homeViewController)
    appdelegate.window!.rootViewController = nav
    

    编辑:SWIFT 4

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let yourVc = UIViewController() // your view controller 
    appDelegate.window?.rootViewController = yourVc
    appDelegate.window?.makeKeyAndVisible()
    

    【讨论】:

    • 仅链接加上抄袭代码不是答案。你指出这个是重复的问题是正确的,但你的职责是评论它是重复的。不要试图利用别人的答案。
    • 我给出了答案的来源..这不应该是抄袭...我写这些的唯一原因是提出问题的人不必导航到原始答案并搜索他/她感兴趣的那个..
    • 好吧,“抄袭”这个词不合适,当然。对不起。但这实际上是一个仅链接的答案,尽管有代码,这是我的观点。因此,我已将其标记为不是答案。再次感谢您指出重复。
    • 酷..这应该是您作为版主的第一个评论,只是不要猛烈抨击..我以后会注意将问题标记为重复,并在需要时对问题发表评论..谢谢让我知道正确的流程是什么。
    • 这不是温和(我没有这样的等级或权力),只是一种意见。我认为这不是真正的答案也只是一种观点。 Stack Overflow 主要是一个“群体智慧”的东西;需要多人同意才能让事情发生。再次抱歉,不清楚。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-19
    相关资源
    最近更新 更多