Passing a managedObjectContext through to a UITabB

2019-05-07 18:49发布

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.

2条回答
Ridiculous、
2楼-- · 2019-05-07 18:56

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.

查看更多
姐就是有狂的资本
3楼-- · 2019-05-07 19:07

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

查看更多
登录 后发表回答