Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a managedObjectContext through to a UITabBarController's views

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.

like image 529
cannyboy Avatar asked Dec 13 '22 18:12

cannyboy


2 Answers

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.

like image 169
Jordan Avatar answered Feb 01 '23 23:02

Jordan


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."

like image 33
W Dyson Avatar answered Feb 01 '23 23:02

W Dyson