开发者

Making an UIButton add data to Core Data Model

开发者 https://www.devze.com 2023-02-25 18:57 出处:网络
Hey all, been scratching my head at this for a couple of days now, but still have no idea how to do it.

Hey all, been scratching my head at this for a couple of days now, but still have no idea how to do it.

I have an UIButton, by clicking it I would like it to add data to my Core Data model. At the moment I'm using navigationItem.rightBarButtonItem开发者_开发百科 = addButton; from my Core Data UITableViewController to add data, but instead I like to connect a UIViewController to the core data model and have an UIButton add data to it.

For example, when this UIbutton is click, it would change to another view while also adding data to the core data model.

-(IBAction)changeviewandadddata {

SecondViewController *screen = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:screen animated:YES];
[screen release];

}

Any help or suggestions on how to do this would be appreciated.


Here is a sample to insert data into a Core Data store.

NSManagedObject *object = [NSEntityDescription insertNewObjectForEntityForName:@"EntityName" 
                                                        inManagedObjectContext:managedObjectContext];
[object setValue:@"test" forKey:@"something"];
NSError *error = nil;
if (![managedObjectContext save:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

    //handle the error
    [managedObjectContext rollback];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消