On 4/18/08 6:46 PM, Ron Lue-Sang said: >> Consider an app that uses bindings and Core Data. It has a tableview >> bound to an array controller. There is an 'Add' button that creates a >> new entity to be shown in the table. It could be implemented by: >> >> 1) sending add: to the array controller. >> 2) using a custom IBAction method that creates a new entity and adds >> it >> to the managed object context. >> >> #1 is nice because the array controller will automatically select the >> newly added object (if selectsInsertedObjects is YES). However, I >> find >> myself often needing to use method #2 because I must supply some >> initial >> data to create the new entity. In that case, the array controller >> sees >> the new entity but does not select it. >> >> What is the "right" way to get it selected? Should I just pass the >> new >> NSManagedObject up to my view-controller and call >> [theArrayController:setSelectedObjects:[NSArray >> arrayWithObject:newManagedObject]]? If so, how do I do it in the tree >> controller case? There is no setSelectedObjects:. > >Subclass the controller and override newObject: - declared in >NSObjectController.h
Ronzilla, Thanks for your reply. Forgive my delayed followup. :) Your technique works great. I subclass NSArrayController and override newObject: like so: - (id)newObject { MyCustomManagedObject* thing = [super newObject]; // Set attributes that depend on other UI or are // otherwise impossible in awakeFromInsert:. [thing setSpecialAttribute:...]; return thing; } Then my UI can invoke the usual NSArrayController IBAction methods like add: and insert:. and I get the 'selects inserted objects' behaviour for free, which is great. The only problem is the NSTreeController case. The above works except that newObject is not invoked by NSTreeController's addChild: and insertChild: IBAction methods. From debugging, I see that there is a method named newChildObject that seems to be exactly analogous to newObject. Using it analogously works great. But this method is undocumented. Doh. Any further advice? Perhaps you guys just forgot to add it to the headers? Thanks, -- ____________________________________________________________ Sean McBride, B. Eng [EMAIL PROTECTED] Rogue Research www.rogue-research.com Mac Software Developer Montréal, Québec, Canada _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]