Search code examples
iosdelegatesuinavigationcontrollerstoryboardmoc

How to pass a managedObjectContext from the appDelegate to the first ViewController when their is a navigation controller between the two views


I've been beating my head against the keyboard for a better of 3 days now in researching and trying to figure out how i can solve the following problem.

I have a story board that looks like this:

Initial app launch arrow -> to a Navigation Controller -> to the Main View Controller.

My appDelegate is creating the managedObjectContext and it populates some entities with data (for testing purpose only atm, it will be removed once I'm ready to integrate with an outside source). This work fine. Now my problem is that I don't know how i can pass the MOC from the appDelegate to my first ViewController because the Navigation controller is in the way. My current code in the appDidFinish Method looks like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    // Pass the managed object context to the root view controller
    MainMenuViewController *rootView = (MainMenuViewController *)self.window.rootViewController;
    rootView.managedObjectContext = self.managedObjectContext; 

    //My actual Core data setup and adding data to it, it works I've tested it.

    return YES;
     }

Now my code works when I change where the app launch arrow in the storyboard to point to my mainMenuViewController, but than I do however lose my navigation bar in all my views. I also know how to pass the MOC from my mainMenu to another view via the - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)senderbut I just can't seem to figure out how to do the initial pass from the appDelegate to the MainViewController because of the darn navigation controller being in-between the two.

I've already searched numerous threads on this site (and others) and i've only found the solution for a "Tabbed application" and others want me to serialize the object, but for my purposes I can't do that. (only a few views will receive the MOC and other will be passes data that one view has created and altered to be tailored for specific purposes in the unique views)

Any help to this nub in iOS and Objective-C is greatly appreciated. Thank you in advance.

EDIT: THe error i get is "terminating app due to uncaught exception ... [UINavigationController setManagedObjectContext] unrecognized selector sent to instance...


Solution

  • If you create a new application from the "Master-Detail" app template in Xcode 4.3 (and 4.2 as well, I think), with the "Use Storyboard" and "Use Core Data" options checked, you'll find the following in AppDelegate.m:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // Override point for customization after application launch.
        UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
        MasterViewController *controller = (MasterViewController *)navigationController.topViewController;
        controller.managedObjectContext = self.managedObjectContext;
        return YES;
    }
    

    This seems to be exactly what you're looking for. The key bit is that you can query a navigation controller for its view controllers.