4

我有一个基于实用程序模板的应用程序(您可以在其中翻转视图以查看另一个)。在第一个视图中有一个登录屏幕,然后它翻转以显示 UITabBar 风格的界面。

我无法弄清楚如何将 managedObjectContext 从 App Delegate(它的创建位置)一直传递到 Tab Bar 的每个视图。

App Delegate 的 managedObjectContext被传递给FrontLoginViewController,后者被传递给BackViewTabBarViewController .. 下一步在哪里?

BackViewTabBarViewController nib 有一个 UITabBarController,每个选项卡都有一个 UINavigationController。

4

2 回答 2

11

听起来像 managedObjectContext 是在您的 AppDelegate 中定义的。如果是这样,那么...

从您想要的任何视图控制器...只需调用

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

然后用...

appDelegate.managedObjectContext

每当您需要 managedObjectContext 时。将 MyApplicationDelegate 更改为您的 AppDelegate,您应该一切顺利。

于 2010-10-19T18:29:47.573 回答
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."

于 2011-02-05T16:19:54.790 回答