【问题标题】:Swift / How to set (and load) a new root view controllerSwift / 如何设置(和加载)一个新的根视图控制器
【发布时间】:2026-01-31 04:40:02
【问题描述】:

我有一个 RootVC。这是一个SwipeViewController: UINavigationController

SwipeViewController

WelcomeNavigationController 内,我正在检查一个条件。如果条件为真,我想打开 RootVC。​​

class WelcomeNavigationController: UINavigationController { 

override func viewWillAppear(animated: Bool) {
        backendless.userService.setStayLoggedIn(true)

    if backendless.userService.currentUser != nil {

        print("currentUser != nil")

        dispatch_async(dispatch_get_main_queue()) {

            let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            appDelegate.window = UIWindow(frame: UIScreen.mainScreen().bounds)
            let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let vc : RootVC = storyboard.instantiateViewControllerWithIdentifier("RootVC") as! RootVC
            appDelegate.window?.rootViewController = vc
            appDelegate.window?.makeKeyAndVisible()

        }
    }
}

打印语句被执行,我的 RootVC 中的一些打印语句也被执行。所以理论上RootVC是加载的。但它是不可见的。如何使我的 RootVC 可见?

非常感谢您的帮助。

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    我认为你应该将 rootVC 添加到当前视图控制器

    self.addChildViewController(vc)
    vc.view.frame = self.view.frame
    self.view.addSubview(vc.view)
    vc.didMoveToParentViewController(self)
    

    【讨论】:

    • vc.didMoveToParentViewController:self 抛出错误 = 预期表达式
    • (self) 解决了它。但这对我不起作用。它与以前相同,但顶部有一个白色导航栏。 “旧”观点似乎被冻结了。并执行来自我的RootVC 的打印语句。
    • 啊,我在这种情况下的实验。你应该从 welcomeViewController -> rootVC 推送。之后删除navigationController.viewControllers 中的welcomeVC。并将 rootVC 设置为 rootViewController
    • 在您的帮助下我得以解决。感谢您的努力
    最近更新 更多