简体   繁体   中英

Swift / How to set (and load) a new root view controller

I have a RootVC. It is a SwipeViewController: UINavigationController .

SwipeViewController

Within WelcomeNavigationController , I'm checking a condition. If that condition is true, I want to open the 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()

        }
    }
}

The print statement is executed and some print statements from my RootVC are executed as well. So in theory the RootVC is loaded. But it is not visible. How can I make my RootVC visible?

Help is very appreciated.

I think you should be add rootVC to current view controller

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM