Swift /如何设置(和加载)新的根视图控制器

时间:2016-09-01 10:19:36

标签: ios swift

我有一个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()

        }
    }
}

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

非常感谢帮助。

1 个答案:

答案 0 :(得分:2)

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

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