Passing an ObjectContext managed object through a UITabBarController view

I have an application based on the Utility template (where you view a view to see something else). The first view is the login screen, then it flips to open the UITabBar style interface.

I'm having problems with how to pass managedObjectContext from the App Delegate (where it was created) completely to all kinds of tables.

The delegated application managedObjectContext is passed to FrontLoginViewController , which is passed to BackViewTabBarViewController .. where next?

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

+4
source share
2 answers

It looks like managedObjectContext is defined in AppDelegate. If yes, then...

From any viewController you want ... just call

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

Then use ...

 appDelegate.managedObjectContext 

whenever you need a managedObjectContext file. Change MyApplicationDelegate to AppDelegate and you should be good to go.

+11
source

I ran into the same problem, I will share my solution.

First you need a link to the Nav controller in the tab bar in the nib file, make sure you have connected it.

 IBOutlet UINavigationController *navigationController; 

Then grab the controller as recommended in the support docs and send it a managedObjectContext:

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

Alex (from another post) is right: β€œNormally, you should avoid getting shared objects from the application delegate. This makes it behave too much like a global variable, and it has a whole mess of problems associated with it.”

+2
source

All Articles