3

我将 atab bar controller与 结合使用core dataNSManagedObjectContext每个不同的选项卡都需要访问app delegate. app delegate有没有办法从应用程序加载时以编程方式将它们传递给视图控制器?

4

1 回答 1

2

如果您在情节提要中有一个基于选项卡的应用程序,则窗口的 rootViewController 将是UITabBarController. 从那里您可以获取构成选项卡的视图控制器。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UIViewController *firstVC = tabBarController.viewControllers[0];
    firstVC.managedObjectContext = self.managedObjectContext;
    UIViewController *secondVC = tabBarController.viewControllers[1];
    secondVC.managedObjectContext = self.managedObjectContext;
    // more...
}
于 2013-08-04T11:47:04.923 回答