***** EDIT ***** What I'm not sure of is how to access an entity from the model in the code, and how to access a specific instance of an entity in that code. That sums up the main issues I'm having.
***** END EDIT *****
I have a tableview with a button to add to it. When the button is clicked, the user is presented with an open dialog where they select a file(s). A new Object is added to the array controller. What I'm not sure how to do is to edit the core data attributes for this new object. There are two attributes, filename and pathname, and I'm not sure how to edit them. If you look at the bottom of the openPanelDidEnd:returnCode:contextInfo: function you'll see what I'm trying to accomplish.
- (IBAction)addAttachment:(id)sender
{
panel = [NSOpenPanel openPanel];
[panel beginSheetForDirectory:nil
file:nil
modalForWindow:[NSApp mainWindow]
modalDelegate:self
didEndSelector:@selector(openPanelDidEnd:
returnCode:
contextInfo:)
contextInfo:NULL];
}
- (void)openPanelDidEnd:(NSOpenPanel *)openPanel
returnCode:(int)returnCode
contextInfo:(void *)x
{
if (returnCode == NSOKButton)
{
NSArray *files = [openPanel filenames];
int i;
for (i = 0; i < [files count]; i++)
{
NSString *file = [files objectAtIndex:i];
[attachmentController add:x];
// How do I add filenames here?
// I'm assuming it involves KVC like
// [something setValue:@"file" forKey:@"filename"];
// But I don't know hot to get the something
// i.e. since I have multiple attachments,
// how do I get the one I just created
}
}
}
*********** EDIT ************** Simplified, my model has 2 entities: Attachment and Item. Item has a to-many relationship with Attachment, as each Item may have many Attachment's.
My openPanelDidEnd:returnCode:contextInfo: method now looks like this:
NSString *filename = [files objectAtIndex:i];
MySchoolPlanner_AppDelegate *myAppDelegate = [[MySchoolPlanner_AppDelegate init] alloc];
[NSEntityDescription insertNewObjectForEntityForName:@"Attachment"
inManagedObjectContext:[myAppDelegate managedObjectContext]];
[myAppDelegate release];
For some reason, the table view bound to the Attachment array controller does not add any. Also, I'm not sure how to access the attachment I just created to use KVC on it.