从 Navigation 更改为 TabBar,CoreData 不加载

Posted

技术标签:

【中文标题】从 Navigation 更改为 TabBar,CoreData 不加载【英文标题】:Changed from Navigation to TabBar, CoreData does not load 【发布时间】:2018-01-18 15:27:43 【问题描述】:

我将导航控制器更改为标签栏控制器,然后我想编译应用程序。但它不再加载我的 managedContext。

这是我加载应用程序时的启动代码:

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

因此我尝试像这样编辑代码:

编辑:

       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
    

我改了代码,还是加载不上managedContext,在ViewController中使用时返回nil,我还把初始的Viewcontroller设置为tabBarController。

【问题讨论】:

你在哪里为你的window设置rootviewcontroller 感谢您的回答。我在 Storyboard 上标记了“是初始视图控制器”。 那个故事板是主要的吗? 是的,我只有一个故事板 你的rootviewcontroller还不是Tabbarcontroller 【参考方案1】:

首先为您的窗口设置 rootviewcontroller,然后再调整您的逻辑。

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

【讨论】:

再次感谢您的帮助。我编辑了问题并尝试了您的解决方案,但仍然无法正常工作。

以上是关于从 Navigation 更改为 TabBar,CoreData 不加载的主要内容,如果未能解决你的问题,请参考以下文章

无法将颜色更改为 tabBar?

TableViewController 导致 TabBar 项目标题更改为导航栏标题

如何将 TabBar + Navigation 与 XCode 结合使用

tabbar隐藏 重写navigation

将 NavigationBar Tint/Color 更改为 Navigation Stack 中每个 ViewController 的不同值

导航 navigationbar 与 tabbar