我读过很多类似的帖子,但两天后,我想我应该问自己的问题。
我有一个单独的 CoreData 控制器。这会将实体对象从 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 中初始化 Category VC,那么我肯定会通过托管对象,因为我从 Detail VC 调用视图。当我这样做时,我收到错误“应用程序试图在目标上推送一个 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];
}