iOS - 传递空托管对象 -

时间:2012-12-02 19:34:51

标签: core-data ios6 nsmanagedobjectcontext

我读了很多类似的帖子,但两天后,我想我应该问自己的问题。

我有一个单独的CoreData Controller。这将实体对象从AppDelegate传递到RootViewController。它没有将它传递给特定的(类别)视图控制器,我无法弄清楚原因。

App Delegate中我尝试传递对象的代码是:

rootViewController.managedObjectContext = self.coreDataController.mainThreadContext;
    categoryListViewController.managedObjectContext = self.coreDataController.mainThreadContext;
    NSLog(@"AD/core data controller is %@", coreDataController.mainThreadContext);
    NSLog(@"AD- rootVC is %@", rootViewController.managedObjectContext);
    NSLog(@"AD/category list is %@", categoryListViewController.managedObjectContext);

并且日志显示核心数据控制器和根vc已填充,但类别vc不会。

2012-12-02 14:28:33.187 [50351:907] AD/coredatacontroller moc is <NSManagedObjectContext: 0x21065160>
2012-12-02 14:28:33.188 [50351:907] AD/categorycontroller moc is (null)
2012-12-02 14:28:33.190 [50351:907] AD- rootVC moc is <NSManagedObjectContext: 0x21065160>

任何想法为什么?

更新

如果按照Valentin的建议执行,并在App Delegate中初始化类别VC,我当然会通过详细VC调用视图来获取托管对象。当我这样做时,我得到错误&#34;应用程序试图在目标&#34;上推送一个nil视图控制器。

如果我尝试在详细VC中初始化类VC(并加载上下文),它不会传达,并且日志显示上下文为零。

初始化VC(在App Delegate中):

categoryListViewController = [[CategoryListViewController alloc] initWithNibName:@"CategoryList-iPad" bundle:nil];

                        // we have loaded from our xib, so has our CoreDataController,
    // so connect as its delegate and setup its persistent store
    //
    self.coreDataController.delegate = self;
    [self.coreDataController loadPersistentStores];



    UINavigationController *rootNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
    UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController];



    // Set up MASTER and DETAIL delegation so we can send messages between views
    rootViewController.detailViewController = detailViewController;
    detailViewController.rootViewController = rootViewController;

    splitViewController = [[UISplitViewController alloc] init];

    splitViewController.viewControllers = @[rootNavigationController, detailNavigationController];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

    splitViewController.delegate = detailViewController;

    rootViewController.managedObjectContext = self.coreDataController.mainThreadContext;
    categoryListViewController.managedObjectContext = self.coreDataController.mainThreadContext;


    NSLog(@"AD - coreDataController is %@", coreDataController.mainThreadContext);
    NSLog(@"AD - rootViewController is %@", rootViewController.managedObjectContext);
    NSLog(@"AD - categoryListVC is %@", categoryListViewController.managedObjectContext);

调用视图(在DetailViewController中):

 -(void)categoryButtonTapped {
     NSLog(@"%s", __FUNCTION__);

     //categoryListViewController = [[CategoryListViewController alloc] initWithNibName:@"CategoryList-iPad" bundle:nil];
     //categoryListViewController.managedObjectContext = coreDataController.mainThreadContext;
     //categoryListViewController.managedObjectContext = self.coreDataController.mainThreadContext;

 UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:categoryListViewController];
     nc.modalPresentationStyle = UIModalPresentationFormSheet;


      NSLog(@"DVC FRC is %@", self);
      NSLog(@"DVC FRC/moc is %@", coreDataController.mainThreadContext);
      NSLog(@"DVC FRC/self.moc is %@", self.coreDataController.mainThreadContext);

     [self presentViewController:nc animated:YES completion:nil];

     //[self.navigationController pushViewController:categoryListViewController animated:YES];
}

1 个答案:

答案 0 :(得分:1)

很可能你的categoryListViewController也是零。试着看看它是否正确分配/初始化。