Skip to content

Instantly share code, notes, and snippets.

@ironfounderson
Created November 3, 2010 19:23
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/661555 to your computer and use it in GitHub Desktop.
Save ironfounderson/661555 to your computer and use it in GitHub Desktop.
Presenting a modal view controller using blocks
- (void)insertNewObject {
__block 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;
__block __typeof__(self) blockSelf = self;
[bookViewController setCancelBlock:^(BookViewController *controller) {
[context rollback];
[blockSelf dismissModalViewControllerAnimated:YES];
}];
[bookViewController setSaveBlock:^(BookViewController *controller) {
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[blockSelf.tableView reloadData];
[blockSelf dismissModalViewControllerAnimated:YES];
}];
[self presentModalViewController:bookViewController animated:YES];
[bookViewController release];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment