Skip to content

Instantly share code, notes, and snippets.

@ironfounderson
Created November 3, 2010 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ironfounderson/661529 to your computer and use it in GitHub Desktop.
Save ironfounderson/661529 to your computer and use it in GitHub Desktop.
Presenting a modal view controller using delegate pattern
- (void)insertNewObject {
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
Book *newBook = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
BookViewController *bookViewController = [[BookViewController alloc] init];
bookViewController.book = newBook;
bookViewController.delegate = self;
[self presentModalViewController:bookViewController animated:YES];
[bookViewController release];
}
- (void)bookViewControllerDidSave:(BookViewController *)controller {
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[self.tableView reloadData];
[self dismissModalViewControllerAnimated:YES];
}
- (void)bookViewControllerDidCancel:(BookViewController *)controller {
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
[context rollback];
[self dismissModalViewControllerAnimated:YES];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment