多个segues导致+ entityForName:nil不是合法的NSManagedObjectContext

时间:2014-04-25 14:36:31

标签: ios objective-c core-data

我是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;
    }
}

1 个答案:

答案 0 :(得分:0)

您的假设是正确的,问题似乎是您在目标视图控制器中没有托管对象上下文。有几种可能的解决方案:

  1. 准备segue时通过MOC(干净,要记住很多,可以转到一个不需要它的课程,只是为了传递给另一个人。
  2. 从细节项目中获取MOC(厚颜无耻,不是很透明)。
  3. 使用单身所有者作为核心数据堆栈并从那里获取mor(干净,透明,有些人讨厌单身......)