0
votes

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?

1
Hi. So it seems that the window!.rootViewController is UITabBarController. Why do you expect it to be navigation controller?aunnnn
I see. Can you update it with the code that encounters the crash you mentioned? Btw you can access all of the VCs through the tabBar.viewControllers (if tabBar is your current window.root), the order of VC will be the same as shown in your UI. Then you will get a navigation controller for each of that, then you can access the actual content of navigation with nav.viewControllers.firstaunnnn
Hello and thanks for the response. I understand it is a UITabBarController. I was merely posting the original AppDelegate code when it was functional. My question is what the correct code would be to replace that section in order to create the favorites library at launch and allow the view to continue accessing the favorites VC to use the library with the new TabBar and NavBar arrangement.Zohak Hartoonian

1 Answers

0
votes

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 ...