Re: Adding new Core Data objects through a form
Re: Adding new Core Data objects through a form
- Subject: Re: Adding new Core Data objects through a form
- From: Kyle Sluder <email@hidden>
- Date: Tue, 27 Oct 2009 21:16:24 -0700
On Tue, Oct 27, 2009 at 6:58 PM, Martin Cote <email@hidden> wrote:
> I'm trying to do something really simple with Core Data, but I can't
> find a graceful way to do it.
Perhaps we can agree on "idiomatic?" Which is quite different from
"idiot-o-matic," though I always seem to read it that way. ;-)
> I would like to show a form to my user that they can fill, and when
> they press 'OK', a new managed object is created. This can be done
> very simply, but I would like to use IB bindings as much as possible.
Rather than binding to a temporary managed object, as in Scenario 1
(which raises even questions about other parts of your app—like entity
mode array controllers—dealing with this transient object being
created), the idiomatic approach would be to bind the fields to
properties of a controller object for your form. (In practice, this
might be an NSWindowController/NSViewController subclass, an
NSObjectController subclass, or a one-off NSObject subclass.) Then
you wire up your button to a method on that controller that commits
editing and creates the managed object.
Alternatively, you could forego bindings and create traditional
outlets to your UI objects, querying them from within your button's
action method. But bindings make the work so much easier, especially
with NSEditorRegistration-conforming controllers!
To be honest, this is a very fundamental Cocoa pattern, yet it took me
the better part of two years to get a handle on it. Here's what it
looks like in action (warning, code typed in compose window!):
@interface NewObjectSheetController : NSWindowController {
NSMutableSet *editors;
}
@property(copy,nonatomic) NSString *firstName; // First name field
bound to this
@property(copy,nonatomic) NSString *lastName; // Last name field bound to this
@property(assign,nonatomic) NSUInteger age; // Age field w/
NSNumberFormatter bound to this
- (IBAction)createObject:(id)sender;
@end
@implementation NewObjectSheetController
@synthesize firstName, lastName, age;
- (void)objectDidBeginEditing:(id)anEditor {
[editors addObject:anEditor];
}
- (void)objectDidEndEditing:(id)anEditor {
[editors removeObject:anEditor];
}
- (BOOL)commitEditing {
NSSet *editorsToRemove = [editors copy];
for (id anEditor in editorsToRemove)
if (![anEditor commitEditing]) // Should cause -objectDidEndEditing:
return NO;
return YES;
}
- (void)discardEditing {
NSSet *editorsToRemove = [editors copy];
for (id anEditor in editorsToRemove)
[anEditor discardEditing];
}
- (void)createObject:(id)sender {
if (![self commitEditing]) {
// Someone refused to save their changes. Prime example: there's
a non-number in
// the Age field, and the NSNumberFormatter is refusing to accept
it, so the Age field
// returns NO from -commitEditing.
return;
}
MyPersonObj *person = [NSEntityDescription insertNewBlahBlahBlah];
person.firstName = self.firstName;
person.lastName = self.lastName;
person.age = self.age;
// Bindings will cause the fields to be emptied.
self.firstName = nil;
self.lastName = nil;
self.age = 0;
}
@end
Hope that helps. I think I'm going to need to write a blog post to
make that clearer. First step would be to get a blog, I guess.
--Kyle Sluder
_______________________________________________
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