0

I have inherited a storyboard that launched directly to a UITabBarController. In the AppDelegate.swift file in the didFinishLaunchingWithOptions method there is the following code:

//let loginScreen = window!.rootViewController
let tabController = window!.rootViewController as! UITabBarController
let navController = tabController.viewControllers![0] as! UINavigationController
let salaryController = navController.topViewController as! SalaryTableViewController
let nav2Controller = tabController.viewControllers![1] as! UINavigationController
let employeeController = nav2Controller.topViewController as! EmployeeTableViewController

I am trying to change the code so the login screen is the rootViewController but when I remove the rootViewController from the tabController assignment I get an error that casting a UITableViewController to UITabBarController always fails. Does anyone know why casting a UITableViewController to a UITabBarController always fails unless the UITableViewController is the rootViewController? Is there a way to change the launch screen to a different controller without changing the rootViewController assignment? Here is the storyboard for visual: enter image description here

Jacobi
  • 21
  • 1
  • 6

2 Answers2

1

UITabBarController is a subclass of UIViewController. You cannot cast superclass to subclass. (Viceversa is possible) To implement your idea, you should remove initialing your root view controller from storyboard and do it programmatically. Because now, the Xcode think your initial view controller is UITabBarController and the only way is that you instantiate your class programmatically and then make it root view controller.

0

Why don't you present your login VC as a modal view controller? That way you didn't need to change anything on the view hierarchy.

Anticro
  • 685
  • 4
  • 12