swift - 从Navigation改为TabBar,CoreData不加载

标签 swift uinavigationcontroller uitabbarcontroller appdelegate

我将导航 Controller 更改为选项卡栏 Controller ,然后我想编译该应用程序。但它不再加载我的 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
    }

我改了代码,但还是无法加载managementContext,当我想在ViewController中使用它时,它返回nil,我还将初始Viewcontroller设置为tabBarController。

最佳答案

首先为您的窗口设置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

关于swift - 从Navigation改为TabBar,CoreData不加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48324782/

相关文章:

ios - 删除每个 UICollectionViewCell 之间的间距并将其移动到 UICollectionView 的外部单元格

iphone - 使用 Storyboard时如何子类化导航 Controller ?

ios - 我可以使用一个 View Controller 来处理选项卡栏 Controller 中的多个 View 吗

ios - swift 语言中的 null/nil

xcode - 如何使用 UIAlertAction 处理程序 : 转到另一个 Storyboard

swift - 使用 Firebase 异步编写代码

ios - 是否可以在 Swift 中为协议(protocol)实例设置观察者(KVO)

ios - 设置后退按钮的名称和 View Controller 的标题

ios - 具有大标题的 UISearchController 在选项卡栏中崩溃并显示 "Only one palette with a top boundary edge can be active outside of a transition"

ios - 哪个选项卡 Controller 被选中?