Created
November 3, 2010 19:23
-
-
Save ironfounderson/661555 to your computer and use it in GitHub Desktop.
Presenting a modal view controller using blocks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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