4

I have an app which is based on the Utility template (where you flip over the view to see another). On the first view there is a login screen, then it flips over to reveal a UITabBar style interface.

I'm having trouble working out how to pass the managedObjectContext from the App Delegate (where it is created) all the way through to each of the Tab Bar's views.

App Delegate's managedObjectContext get passed to FrontLoginViewController which gets passed to BackViewTabBarViewController .. where next?

The BackViewTabBarViewController nib has a UITabBarController with a UINavigationController for each tab.

cannyboy
  • 24,180
  • 40
  • 146
  • 252

2 Answers2

11

Sounds like the managedObjectContext is defined in your AppDelegate. If so, then...

From whatever viewController you want... just call

MyApplicationDelegate *appDelegate = (MyApplicationDelegate *)[[UIApplication sharedApplication] delegate];

Then use...

appDelegate.managedObjectContext

whenever you need the managedObjectContext. Change the MyApplicationDelegate to your AppDelegate and you should be good to go.

Jordan
  • 21,746
  • 10
  • 51
  • 63
  • 2
    Fine for a really trivial app I suppose, but can we stop promoting this anti-pattern? Is not good object oriented design, it's not what Apple recommends and all it does is demote your app delegate to a glorified service locator/global. – Luke Redpath Dec 15 '12 at 01:20
  • @LukeRedpath your comment is not useful. Why don't you describe an alternative? – MatterGoal Mar 01 '14 at 13:42
  • 2
    @MatterGoal the alternative is to pass your dependencies around as needed. Apple provide examples of doing this and dependency injection over global dependencies is just general good engineering practice. It makes your apps harder to test and turns your code into spaghetti. If this was used in any app I was working on, it wouldn't pass code review. – Luke Redpath Mar 05 '14 at 15:39
  • @LukeRedpath thanks! Are you sure that an App using this method doesn't pass the code review?! In that case... Jordan, it would be better accept another answer as correct. – MatterGoal Mar 07 '14 at 09:48
  • By code review I mean an internal code review, nothing to do with the App Store review process. – Luke Redpath Mar 09 '14 at 17:15
2

I've ran into this same problem, i'll share my solution.

First you need a reference to the Nav Controller in the Tab Bar in the nib file, make sure you connect it up.

IBOutlet UINavigationController *navigationController;

Then, get the Controller as recommended in the support docs and send it the managedObjectContext:

SavedTableViewController *saved = (SavedTableViewController *)[navigationController topViewController];
saved.managedObjectContext = self.managedObjectContext;

Alex (from another post) is right, "You should generally stay away from getting shared objects from the app delegate. It makes it behave too much like a global variable, and that has a whole mess of problems associated with it."

W Dyson
  • 4,604
  • 4
  • 40
  • 68