I’m having a lot of difficulty getting core data to work in my application. I’m perfectly comfortable with core data in a table view controller and even with core data in a single view application. I’m unable however to get my program to function properly in with multiple view controllers.
I’ve read through Zarra’s core data book and bought pro core data for iOS and have gone through the projects listed but every one of them is used in a TableView controller. with the exception of the shapes application in pro core data for iOS.
Does anyone know of any examples (code or tutorials) that would demonstrate how to do a program with multiple view controllers and core data?
What I would like to do is have buttons on the first (instead of tableview cells) that will segue to the the next viewController. On the second view controller I would like that information populated with information from the set of the first entity,
so I have something like this so far where the first entity is:
House
- houseName (attribute)
- occupants (relationship)
Person
- personName (attribute)
- household (relationship)
occupants <–>>household (one to many )
{
...
int i = //house selected on previous view controller;
NSManagedObject *people = [[self sortOccupants] objectAtIndex:i];
textField01.text = [NSString stringWithFormat:@"%@",[[people valueForKey:@"personName"]description]];
}
the sort occupants looks like this:
-(NSArray *)sortOccupants
{
NSSortDescriptor *sortPeopleInHouse = [[NSSortDescriptor alloc] initWithKey:@"personName" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortPeopleInHouse, nil];
return [[(NSSet *)[house valueForKey:@"occupants"] allObjects] sortedArrayUsingDescriptors:sortDescriptors];
}
Any Ideas would be great but if you can point me to sample code that would show this I would be most appreciative.
Thanks,
The way I would approach this is to generate NSManagedObject subclasses for your entities (makes it much more readable and type-safe).
Then, I would create a new init method in the second view controller. initWithHouse:(House *)house or something:
Hope that helps.