Re: NSArrayController setSelectedObjects: not working with newly created objects
Re: NSArrayController setSelectedObjects: not working with newly created objects
- Subject: Re: NSArrayController setSelectedObjects: not working with newly created objects
- From: mmalcolm crawford <email@hidden>
- Date: Sun, 07 Dec 2008 08:49:06 -0800
On Dec 7, 2008, at 8:29 AM, Matteo Manferdini wrote:
NSManagedObject *newObject = [arrayController newObject];
[arrayController setSelectedObjects:[NSArray
arrayWithObject:newObject]];
If I call [arrayController selectedObject] just before these two
lines, the array returned contains the actual selection, but if I call
it just after them, it returns an empty array.
I'm not sure why this is "weird"? -- it's certainly not a bug, it's
behaving correctly.
newObject doesn't insert the object into the array.
Assuming that you have automatically prepares content set for the
array controller, then newObject will be added later when the array
controller receives an
NSManagedObjectContextObjectsDidChangeNotification notification:
- (void)setAutomaticallyPreparesContent:(BOOL)flag
"If flag is YES and a managed object context is set, the initial
content is fetched from the managed object context using the current
fetch predicate. The controller also registers as an observer of its
managed object context. It then tracks insertions and deletions of its
entity using the context's notifications, and updates its content
array as appropriate."
So when you invoke 'setSelectedObjects:[NSArray
arrayWithObject:newObject]', newObject isn't in the array...
If you want to immediately insert and select the new object, then do
so, e.g. per <http://lists.apple.com/archives/Cocoa-dev/2008/Dec/msg00534.html
>
- (IBAction)newAtTop:sender {
id newObject = [arrayController newObject];
[arrayController insertObject:newObject atArrangedObjectIndex:0];
[tableView editColumn:0 row:0 withEvent:nil select:YES];
[newObject release];
}
mmalc
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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