Re: NSPopupButton with NSArrayController
Re: NSPopupButton with NSArrayController
- Subject: Re: NSPopupButton with NSArrayController
- From: "I. Savant" <email@hidden>
- Date: Sat, 6 Jan 2007 22:29:48 -0500
On Jan 6, 2007, at 9:49 PM, Ferhat Ayaz wrote:
- (IBAction)makeInvoice:(id)sender
{
NSManagedObjectContext *moc = [invoiceArrayController
managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Invoice" inManagedObjectContext:moc];
NSString *className = [entity managedObjectClassName];
Class entityClass = NSClassFromString(className);
NSManagedObject *invoice = [[entityClass alloc]
initWithEntity:entity
insertIntoManagedObjectContext:moc];
[invoiceArrayController addObject:invoice];
NSLog(@"Invoice nr: %@",[invoice valueForKey:@"nr"]);
}
Ah, now we're getting somewhere.
This is ... a bit overcomplicated. Try this:
- (IBAction)makeInvoice:(id)sender
{
NSManagedObjectContext * moc = [self managedObjectContext]; // 1
NSManagedObject * invoice = [NSEntityDescription
insertNewObjectForEntityForName:@"Invoice"
inManagedObjectContext:moc]; // 2
[invoiceArrayController fetch:sender]; // 3
}
1: The managed object context is usually referenced this way, if
you've used an XCode "Core Data" project template. It could easily be
[myControllerReferencedFromSomewhereElse managedObjectContext];
2: This properly creates and inserts a new instance of the entity of
your choice (by name) into the specified context. The documentation I
referred you to shows you this quite clearly. This is why I referred
you to it.
3: Adding the object to your array controller is unnecessary since
your 'invoice' instance has already been inserted into the context.
If a newly-inserted instance of an entity doesn't show up in your
interface when it's been added, just send the relevant array
controller a -fetch: message. This will force the controller to re-
fetch the matching entity instances.
That should be all you need for the 'inserting' question.
--
I.S.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden