我是iOS编程的新手,并尝试通过示例代码组合学习。 你能不能帮我解决一下我可能遇到的具体情况,这会导致错误记录“+ entityForName:nil不是合法的NSManagedObjectContext”。以前的线程都不能帮助我。 我有一个tableview与1. segue到细节和另外2. segue到subtableview。
这是我收到的错误:
2014-04-25 17:18:41.150 PhotoLocations [27319:60b]运行addEvent - APLEvent 2014-04-25 17:18:41.155 PhotoLocations [27319:60b] ***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'+ entityForName:nil不是合法的NSManagedObjectContext参数,用于搜索实体名称'Event''
我想,我没有传递上下文并且segue不够。由于我是新手,我想学习如何使用CoreData从一个TVC启用多个segue?
这是第一个TVC中的segue方法:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
[[segue destinationViewController] setDetailItem:object];
}
}
提前感谢您的帮助!
修改 添加调试NSLog报告:
2014-04-25 19:07:27.059 PhotoLocations[28268:60b] run viewDidLoad
2014-04-25 19:07:27.059 PhotoLocations[28268:60b] run locationManager
2014-04-25 19:07:27.062 PhotoLocations[28268:60b] run viewWillAppear
2014-04-25 19:07:27.062 PhotoLocations[28268:60b] run numberOfSectionsInTableView
2014-04-25 19:07:27.063 PhotoLocations[28268:60b] run numberOfSectionsInTableView
2014-04-25 19:07:27.064 PhotoLocations[28268:60b] run numberOfRowsInSection
2014-04-25 19:07:27.102 PhotoLocations[28268:60b] Location manager failed
2014-04-25 19:07:28.141 PhotoLocations[28268:60b] run addEvent
2014-04-25 19:07:28.141 PhotoLocations[28268:60b] run addEvent - CLLocation
2014-04-25 19:07:28.142 PhotoLocations[28268:60b] run locationManager
2014-04-25 19:07:28.144 PhotoLocations[28268:60b] Getting a random location
2014-04-25 19:07:28.145 PhotoLocations[28268:60b] run addEvent - APLEvent
2014-04-25 19:07:28.150 PhotoLocations[28268:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Event''
*** First throw call stack:
(
0 CoreFoundation 0x023461e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x019578e5 objc_exception_throw + 44
2 CoreData 0x01608a1b +[NSEntityDescription entityForName:inManagedObjectContext:] + 251
最终编辑: 感谢Issues handing over managedObjectContext
内的@svena解决方案准备segue的正确方法如下:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
[super prepareForSegue:segue sender:sender];
if ([[segue identifier] isEqualToString:@"segueNameOfPresentingYourModalViewController"] == YES) {
UINavigationController *navigationController = segue.destinationViewController;
YourCustomViewController *aController = (YourCustomViewController *)navigationController.topViewController;
aController.managedObjectContext = self.managedObjectContext;
}
else if ([segue.identifier isEqualToString:@"segueNameOfPushingYourViewController"] == YES) {
YourCustomViewController *aController = segue.destinationViewController;
aController.managedObjectContext = self.managedObjectContext;
}
}
答案 0 :(得分:0)
您的假设是正确的,问题似乎是您在目标视图控制器中没有托管对象上下文。有几种可能的解决方案: