iosuitabbarcontrollermanagedobjectcontext

Passing managedobjectContext to tabbarcontroller with navigation controllers in modal view


For a small app, I have a login screen. On Auth, A tab bar controller with two views (one with navigation controller) is presented. I am following this tutorial. It uses core data. http://maybelost.com/2011/12/tutorial-storyboard-app-with-core-data/

enter image description here The tutorial calls a segue. But I would like to use presentModalViewController. It works, except I am wondering how to pass a managedObjectContext to the View inside Navigation Control inside tab bar controller.

I read this Passing ManagedObjectContext to view controllers using storyboards with a root UITabBarController, but the comments under second answer say it is not the right method.

Can someone tell me the correct way to do it? I am looking to know how to get a reference to a view inside the tabbar controller so that I can set the managedobjectcontext for that view.

Thanks

EDIT In appDelegate.h:

@interface AppDelegate : UIResponder <UIApplicationDelegate>
   @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;

My appDelegate.m :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    LoginViewController *rootView = (LoginViewController *)self.window.rootViewController;
    rootView.managedObjectContext = self.managedObjectContext;
}

Solution

  • Actually what I tend to do is grab the context from the tabBarController of the view you are in. For instance, in your viewWillAppear from the view in the tab bar just create an instance of self.tabBarController and set your views context to that context. That way you can just pass the context to the tabBar and each view can reference that tabBar's context.

    Something like this.

    MainTabBar *parentTabBarController = (MyTabBar *)self.tabBarController;
    self.managedObjectContext = parentTabBarController.managedObjectContext;