Afshin M. Khiabani Afshin M. Khiabani - 1 year ago 104
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

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



Answer Source

Try this code.

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


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download