How to share a ManagedObjectContext when using UITabBarController

前端 未结 9 2373
一整个雨季
一整个雨季 2020-12-23 10:57

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

9条回答
  •  被撕碎了的回忆
    2020-12-23 11:22

    Good, I looked long and hard at the CoreDataBooks sample application and did it like this:

    • Created IBOutlets to the RootViewController (the top view controller of the UINavigationController) and the MapViewController (the custom view controller) in the app delegate.
    • Connected the outlets to the view controllers in the MainWindow.xib
    • Added the following code to applicationDidFinishLaunching:

      // pass the managed object context to the view controllers
      RootViewController *rootViewController = (RootViewController *)[navigationController topViewController];
      rootViewController.managedObjectContext = self.managedObjectContext;
      
      mapViewController.managedObjectContext = self.managedObjectContext;
      

    And now it works like a charm.

提交回复
热议问题