0
votes

I changed my Navigation Controller to Tab Bar Controller, then I wanted to compile the app. But it does not load my managedContext anymore.

This is my launching code when I load the app:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    guard let navController = window?.rootViewController as? UINavigationController,
        let viewController = navController.topViewController as? ViewController else {
            return true
    }
    viewController.managedContext = coreDataStack.managedContext
    // Override point for customization after application launch.
    return true
}

Therefore I tried to edit the code like this:

Edit:

       func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateInitialViewController()
        //let controller = storyboard.instantiateViewController(withIdentifier: "TabBarVC")
        self.window?.rootViewController = controller



        guard let tabController = window?.rootViewController as? UITabBarController,
            let navController = tabController.viewControllers![0] as? UINavigationController,
            let viewController = navController.topViewController as? ViewController else {
                return true

        }
//  Version that works for UINavigationCOntroller 
//        guard let navController = window?.rootViewController as? UINavigationController,
//            let viewController = navController.topViewController as? ViewController else {
//                return true
//        }
        viewController.managedContext = coreDataStack.managedContext
        // Override point for customization after application launch.
        return true
    }

I changed the code, but I still can not load managedContext, and it returns nil when I want to use it in ViewController, I also set the initial Viewcontroller to the tabBarController.

1
Where are you setting rootviewcontroller for your window - Umair Aamir
Thanks for answering. I marked "Is initial View Controller" on the Storyboard. - PaFi
Is that storyboard is main? - Umair Aamir
yes , i only have one storyboard - PaFi
Your rootviewcontroller is still not Tabbarcontroller - Umair Aamir

1 Answers

0
votes

Set rootviewcontroller for your window first and then right your logic afterwards.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    let storyboard = UIStoryboard(<StoryboardName>, bundle: nil)
    let controller = storyboard.initializeInitialViewController()
    window.rootViewController = controller
        guard let navController = window?.rootViewController as? UITabBarController,
            let viewController = navController.moreNavigationController as? ViewController else {
                return true
        }
        viewController.managedContext = coreDataStack.managedContext
        // Override point for customization after application launch.
        return