I have a core-data with 2 entities: 'CarEntity' and 'PassengerEntity'.
Now, each entity has an attribute called 'name'. CarEntity has a to-many relationship with PassengerEntity called 'passengers' and PassengerEntity has the inverse relationship 'inCar'.
So, I did this interface to insert a new passenger.
I have one NSTextField for the person's name and one NSPopUpButton to chose the car. The popup button has a "content values" binding to a NSControllerArray that allows me to get all the cars.
Then I have one button to save everything. The header code goes like this:
IBOutlet NSTextField *newPassengerNameField;
IBOutlet NSPopUpButton *newPersonCarField;
And the implementation goes like this:
- (IBAction)saveNewPassenger:(id)sender {
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSManagedObject *newPassengerObject = [
NSEntityDescription
insertNewObjectForEntityForName:@"PassengerEntity"
inManagedObjectContext:managedObjectContext
];
//Here we have all the fields to be inserted.
[newPassengerObject setValue:[newPassengerNameField stringValue] forKey:@"name"];
//Car ?????
}
Okay, this code works just fine.. for the name. But I can't figure out how to insert the car relationship.
My application forces the user to create a car before coming to this stage, so I have objects in the CarEntity.
The question is: How do I get the value of the popup button and send it to this insert code?
Thanks!