I'm very new in iOS programming and trying to learn by sample codes combining. Would you please help me with a specific situation I may have, which caused a well documented error "+entityForName: nil is not a legal NSManagedObjectContext". None of the previous threads could help me out here. I have one tableview with 1. segue to detail and additional 2. segue to subtableview.
This is the error I received:
2014-04-25 17:18:41.150 PhotoLocations[27319:60b] run addEvent - APLEvent 2014-04-25 17:18:41.155 PhotoLocations[27319:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Event''
I guess, I do not pass the context across and the segue is insufficient. Since I'm newbie, I want to learn how to enable multiple segues from one TVC with CoreData?
This is how the segue method in the first TVC:
- (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];
}
}
Thanks in advance for your help!
EDIT: Adding Debug NSLog reports:
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
FINAL EDIT: I found the solution thanks to @svena within Issues handing over managedObjectContext
The correct method for prepare for segue is like below:
- (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;
}
}