Zohak Hartoonian
Zohak Hartoonian

Reputation: 3

UINavigationController, UITabBarController, UITableViewController

I currently have the application set up with a UINavigationController as the initial view, which has a UITableViewController as its root view controller. The app runs fine up until this point. I have the following code in AppDelegate.swift:

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    let favLibrary = FavLibrary()

    let navController = window!.rootViewController as! UINavigationController
    let favController = navController.topViewController as! FLViewController
    favController.favLibrary = favLibrary

    return true
}

I am trying to implement a UITabBarController so that I can switch between two UITableViewControllers at the same level (Favorites and a Library) using the Tab Bar.

I embed each VC in its own Navigation Controller, then I embed the two Navigation controllers into one Tab Bar Controller.

Upon running the application, it crashes with the following error:

Could not cast value of type 'UITabBarController' (0x115f9e430) to 'UINavigationController' (0x115f971d0). 2018-09-27 15:49:43.811377-0700 appName [3675:954448] Could not cast value of type 'UITabBarController' (0x115f9e430) to 'UINavigationController' (0x115f971d0).

How can I correct the code in AppDelegate to retain functionality with the new arrangement of Tab Bar and Navigation Controllers?

Upvotes: 0

Views: 56

Answers (1)

aunnnn
aunnnn

Reputation: 1942

If I understand your question correctly, I suppose you should access the tabBarController first, then retrieve the view controllers it contains, which should be a list of navigation controllers. Then you can get your view controller inside the selected navigation controller:

let tabBar = window!.rootViewController as! UITabBarController
let targetTabNav = tabBar.viewControllers![1] as! UINavigationController // change index to what you want
let targetVc = targetTabNav.viewControllers.first!
// Do what you want with the target Vc ...

Upvotes: 0

Related Questions