如何在使用UITabBarController时共享ManagedObjectContext [英] How to share a ManagedObjectContext when using UITabBarController

查看:193
本文介绍了如何在使用UITabBarController时共享ManagedObjectContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iPhone应用程序,它有一个MainWindow.xib持有一个UITabBarController,它又有一个UINavigationController和一个自定义UIViewController子类的ViewControllers数组。

I have an iPhone application that has a MainWindow.xib holding a UITabBarController, which in turn has a UINavigationController and a custom UIViewController subclass in its ViewControllers array. The root view controller for the UINavigationController and the custom view controller are both loaded from other xib files.

应用程序使用核心数据,堆栈是在应用程序委托中初始化的根据惯例)。

The app uses core data, the stack is initialized in the app delegate (as per the convention).

应用程式委托将UITabBarController新增至视窗:

The app delegate adds the UITabBarController to the window:

- (void)applicationDidFinishLaunching:(UIApplication *)application {        
    // Configure and show the window
    [window addSubview:[tabBarController view]];
    [window makeKeyAndVisible];
}

我意识到我需要传播一个指向在应用程序中创建的ManagedObjectContext代理,但我不知道如何继续(甚至阅读所有关于这个主题的好评论此处这里):

I realize that I need to propagate a pointer to the ManagedObjectContext created in the app delegate, but I don't know how to proceed (even reading all the good commentary on the topic here and here):


  • 我将ManagedObjectContext传播到UITabBarController,并从那里传播到各个视图控制器,

  • 或者我将ManagedObjectContext直接传播到UINavigationController的根视图控制器和自定义视图控制器,我该如何做?

我想我不太了解如何使用UITabBarController。

I guess I don't understand well enough how to work with the UITabBarController.

推荐答案

理想情况下,您需要传递 NSManagedObjectContext NSFetchedResultsController 或相关的 NSManagedObject down到 UIViewController 中。这允许父控制孩子并确定孩子应该具有什么。这创建了一个更松散耦合的设计,并允许您根据需要轻松重新排列 UIViewController 实例。它还使得更容易重用 UIViewController

Ideally you want to pass either the NSManagedObjectContext, NSFetchedResultsController or the relevant NSManagedObject "down" into the UIViewController. This allows the "parent" to control the "child" and determine what the child should have. This creates a more loosely coupled design and allows you to easily re-arrange UIViewController instances as needed. It also makes it easier to reuse a UIViewController.

在标签视图设计中它没有什么不同。您的AppDelegate将 NSManagedObjectContext 传递给负责创建进入 UIViewController 实例的任何人> UITabBarController 。反过来,创建者传递相关信息( NSManagedObject NSFetchedResultsController 和/或 NSManagedObject instances)插入到 UIViewController 实例中。

In a tab view design it is no different. Your AppDelegate passes the NSManagedObjectContext to whoever is responsible for creating the initial UIViewController instances that go into the UITabBarController. In turn that creator passes the relevant information (NSManagedObject, NSFetchedResultsController, and/or NSManagedObject instances) into the UIViewController instances as it is constructing them.

这篇关于如何在使用UITabBarController时共享ManagedObjectContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆