如何在TabBased应用程序中将NSManagedObjectContext从AppDelegate分配给ViewController? [英] How to assign NSManagedObjectContext from AppDelegate to ViewControllers in TabBased application?

查看:83
本文介绍了如何在TabBased应用程序中将NSManagedObjectContext从AppDelegate分配给ViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新手的问题在这里。

因为XCode不提供项目模板的TabBased应用程序与Core Data,所以我必须去切割和粘贴Core Data堆栈从其他模板到我的TabBased应用程序。

Since XCode doesn't provide project template for TabBased application with Core Data, so I have to go cutting and pasting Core Data stack from other templates to my TabBased application.

这是如何分配 NSManagedObjectContext 应用程序模板中的到MasterViewController

This is how to assign NSManagedObjectContext object from AppDelegate to MasterViewController in Master-Detail application template:

// AppDelegate.m (Master-Detail template)

- (BOOL)application:(UIApplication *)application 
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    UINavigationController *navigationController = 
            (UINavigationController *)self.window.rootViewController;
    MasterViewController *controller = 
            (MasterViewController *)navigationController.topViewController;
    controller.managedObjectContext = self.managedObjectContext;
    return YES;
}

如何分配 NSManagedObjectContext

推荐答案

如果标签页bar控制器是根视图控制器,
主视图控制器在第一个选项卡上:

If the tab bar controller is the root view controller and the "master view controller" is on the first tab:

UITabBarController *tbc = (UITabBarController *)self.window.rootViewController;
MasterViewController *controller = (MasterViewController *)tbc.viewControllers[0];
controller.managedObjectContext = self.managedObjectContext;

更新:如果第一个标签使用导航控制器,插入一个步骤:

Update: If the first tab uses a navigation controller, you just have to insert one step:

UITabBarController *tbc = (UITabBarController *)self.window.rootViewController;
UINavigationController *nc = tbc.viewControllers[0];
YourViewController controller = (YourViewController *)nc.topViewController;
controller.managedObjectContext = self.managedObjectContext;

这篇关于如何在TabBased应用程序中将NSManagedObjectContext从AppDelegate分配给ViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆