我正在玩Core Data,对它很陌生。这段代码看起来很重要:
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
MasterViewController *controller = (MasterViewController *)navigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
它将managedObjectContext
设置为所需的视图,对吗?(此时为MasterViewController
现在我已经玩了,但似乎不能让它工作。如果我想从应用程序的其他地方启动MasterViewController
,而不是直接从/作为根视图?
编辑
我在InterFace Builder中看到的视图:
UINavigationController
>关系> UIViewController
>Push Segue> UITableViewController
在Interface builder中,UINavigationBar是空白的,第一个控制器然后UIViewController连接到它并且是用户在应用中看到的第一个视图,然后我想从那里到我的UITableViewController那里有我的CoreData用法
您有两个解决方案:
你必须在所有的UIViewController
NSManagedObjectContext
或
从任何地方获取NSManagedObjectContext
:
[[[UIApplication sharedApplication] delegate] mainManagedObjectContext];
确保:
- 你的视图控制器(不是视图)可以访问正确的
managedObjectContext
。 - 表视图
datasource
从self.fetchedResultsController
获取数据。 - 你配置你的fetchedresultscontroller来为你的tableview获取正确的数据。