David Seek David Seek - 3 months ago 14
Swift Question

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.

Answer

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)
Comments