PDA

View Full Version : Insert New Record and Immediately Edit




KieferThomas
Mar 9, 2013, 09:01 PM
Hi All,

I am new to Xcode programming. I am inserting a new record using the following code connected to my button:

NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *entity = [NSEntityDescription
insertNewObjectForEntityForName:@"Entity"
inManagedObjectContext:context];

This code inserts a new record in my database but that's all it does...


What I cannot figure out is how do I get the newly inserted record moved to automatically so I can start editing the new blank fields.

I have asked this on several different forums and no one has given any help...

This is driving me nuts any help would greatly appreciated.

Thanks,
Keith



ElectricSheep
Mar 10, 2013, 09:38 AM
You are going to need to provide some more details. From your post, I gather you have a button connected to an IBAction somewhere that runs this little bit of code, but I can't address your question without knowing things such as:

- What do you expect this new entity to be displayed in? Table View? Custom View? A series of NSTextFields you have in a window somewhere?

- Are you using any of the standard Cocoa controller objects in your project?

The answers to these questions greatly influence how you would automatically get the newly created entity populated somewhere where you can begin editing it.

KieferThomas
Mar 10, 2013, 03:55 PM
Hey Thanks For The Reply ElectricSheep,

What I am attempting to do is: Get the the newly inserted record moved to and select a NSTextField for editing when my button is clicked.

The full code of what I am doing is:
- (IBAction)myButton:sender
{
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"EEEE, MMMM d, yyyy -- h:mm a"];
NSString *dateTime = [NSString stringWithFormat:@"%@",[outputFormatter stringFromDate:[NSDate date]]];

NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *entity = [NSEntityDescription
insertNewObjectForEntityForName:@"Entity"
inManagedObjectContext:context]; <--- New record is add to database

//The new record is not active...
//What I need to know is how do I move to this new record using code...?

[entity setValue:dateTime forKey:@"date"]; <--- This gets inserted in record
[textField becomeFirstResponder]; <--- This takes focus
}

The record of the database is the first record in the database and the program is expecting me to edit record #1 not the new one which was inserted. When my button is clicked...

- Are you using any of the standard Cocoa controller objects in your project?
I have looked through many of the Controller Objects and I am confused as ever by them. I have searched through many things to either search for the newly inserted record or find the pointer of the last row of the database and fetch it for editing and I was unable to get them to work.

I am just not sure of what I am missing. I know this has to be something simple, I hope I have explained myself clearer...

Thanks,
Keith

PS:
If, you want here is a link to download my project: http://www.filehosting.org/file/details/424989/My%20Personal%20Journal.zip ,this will allow you to see what it is doing. This is just a simple journal program but it is a way for me to learn. When you push the add button you will see what is happening...

Ap0ks
Mar 11, 2013, 09:00 AM
I took a quick look at your project and while it's different to how I'd do it (I'd enter data then create the record, rather than creating an almost empty record and editing it) I found the following changes should help you out:

Bind the add: action of your second arrayController to the Add button so that core data takes over handling the insert and view updates.

Then override the awakeFromInsert method in your model object (MyPersonalJournal.m) and add the date when the item is created
- (void) awakeFromInsert
{
[super awakeFromInsert];

NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"EEEE, MMMM d, yyyy -- h:mm a"];
NSString *dateTime = [NSString stringWithFormat:@"%@",[outputFormatter stringFromDate:[NSDate date]]];
[self setData: dateTime];
}

Finally you can remove the add button code from your app delegate.

Hope that helps.