Afshin M. Khiabani Afshin M. Khiabani - 4 months ago 25
iOS Question

programmatically set initial view controller to tab controller swift 2

I have a tab controller on my iOS App and I want to check some conditions first and do as follow:

self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
if condition {
globalClass.token = u
let mvc: MainViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("mainView") as! MainViewController
self.window?.rootViewController = mvc

} else {
globalClass.token = ""
let mvc: LoginViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("loginView") as! LoginViewController
self.window?.rootViewController = mvc
}
self.window?.makeKeyAndVisible()


My problem is that when I programmatically set the initial
view controller
to first tab of the tab controller when it is loaded, the tab menus at bottom won't load. It just loads the
view controller
not tab menu.

MainViewController is the first tab of the tab view controller

Thanks,

Afshin

Answer

Try this code.

let tbc = mainStoryBoard.instantiateViewControllerWithIdentifier("tabbarStoryboardId") as! UItabBarController self.window?.rootViewController = tbc

Thanks

Comments