使用prepare for segue将托管对象上下文从ViewController传递到TarBarViewController

时间:2012-03-08 20:22:08

标签: ios core-data nsmanagedobjectcontext segue

我试图从ViewController传递核心数据管理对象上下文(它能够从AppDelegate加载到TabBarViewController。

AppDelegate.m

// Pass the managed object context to the root view controller (the login view)
LoginViewController *rootViewController = (LoginViewController *)self.window.rootViewController;
rootViewController.managedObjectContext = self.managedObjectContext;

LoginViewController.m

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"LoginSegue"]){
        UITabBarController *tabBarViewController = (UITabBarController *) [segue destinationViewController];
        ReceiptsListViewController *receiptsListViewController = (ReceiptsListViewController *) [[tabBarViewController viewControllers] objectAtIndex:5];
        receiptsListViewController.managedObjectContext = self.managedObjectContext;
    }
}

When i run it i encounter this error

// Pass the managed object context to the root view controller (the login view)
LoginViewController *rootViewController = (LoginViewController *)self.window.rootViewController;
rootViewController.managedObjectContext = self.managedObjectContext;

2 个答案:

答案 0 :(得分:3)

我不明白你想如何将MOC从VC传递给TBVC。通常RootViewController是TBVC。如果你的情况也是如此,那么就像这样通过MOC:

#import "AppDelegate.h"
#import "MasterViewController.h"

@implementation AppDelegate

@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;

    UINavigationController *navigationController = (UINavigationController *)[[tabController viewControllers] objectAtIndex:0];
    MasterViewController *controller = (MasterViewController *)[[navigationController viewControllers] objectAtIndex:0];
    controller.managedObjectContext = self.managedObjectContext;

    navigationController = (UINavigationController *)[[tabController viewControllers] objectAtIndex:1];
    controller = (MasterViewController *)[[navigationController viewControllers] objectAtIndex:0];
    controller.managedObjectContext = self.managedObjectContext;

    navigationController = (UINavigationController *)[[tabController viewControllers] objectAtIndex:2];
    controller = (MasterViewController *)[[navigationController viewControllers] objectAtIndex:0];
    controller.managedObjectContext = self.managedObjectContext;

    return YES;
}

BTW:此示例基于Xcode中的Master-Detail应用程序templante。默认情况下它没有TBVC,但是如果您在屏幕截图中添加TBVC,则上面的代码将起作用。

enter image description here

答案 1 :(得分:0)

您正在尝试在UINavigationController上设置NSManagedObjectContext。

ReceiptsListViewController *receiptsListViewController = (ReceiptsListViewController *) [[tabBarViewController viewControllers] objectAtIndex:5] 是你的问题,因为它返回UINavigationController。