Passing a managedObjectContext through to a UITabBarController's views

左心房为你撑大大i 提交于 2020-01-13 12:16:12

问题


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.


回答1:


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.




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



来源:https://stackoverflow.com/questions/3970952/passing-a-managedobjectcontext-through-to-a-uitabbarcontrollers-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!