Databases Reference
In-Depth Information
application uses when going from one view controller to another. A segue can
happen when we select a row in a table, a button in the navigation bar, a
button on the tab bar, or virtually anything that causes the UI to move to
another view controller.
RecipesV1/PPRecipes/PPRMasterViewController.m
- ( void )prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@ "showRecipe" ]) {
[self prepareForDetailSegue:segue sender:sender];
return ;
} else if ([[segue identifier] isEqualToString:@ "addRecipe" ]) {
[self prepareForAddRecipeSegue:segue sender:sender];
return ;
}
ALog(@ "Unknown segue: %@" , [segue identifier]);
}
When a segue is activated, the current UIViewController receives a call to -prepare-
ForSegue:sender: . That is our opportunity to prepare the next UIViewController for display.
It's a good idea to use this method as a branching point for each potential segue.
Taking this step aids with code clarity and maintenance. With one or two segues,
this method is fairly easy to maintain. However, when we have many possible
segues, the code quickly becomes unwieldy. Instead of waiting for the code to
become unwieldy and refactoring it, we start off by using this method as a
branching point.
RecipesV1/PPRecipes/PPRMasterViewController.m
- ( void )prepareForDetailSegue:(UIStoryboardSegue*)segue sender:(id)sender
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
id object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
[[segue destinationViewController] setRecipeMO:object];
}
The first segue brings in the PPRDetailViewController and displays a single recipe
in detail. The method -prepareForDetailSegue:sender: demonstrates the dependency
injection pattern . We obtain a reference to the about-to-be exposed UIViewCon-
troller , and we inject the information that it needs. We'll use this pattern
extensively throughout the topic.
RecipesV1/PPRecipes/PPRMasterViewController.m
- ( void )prepareForAddRecipeSegue:(UIStoryboardSegue*)segue sender:(id)sender
{
NSManagedObjectContext *context = nil;
NSEntityDescription *entity = nil;
NSManagedObject *newMO = nil;
context = [[self fetchedResultsController] managedObjectContext];
entity = [[[self fetchedResultsController] fetchRequest] entity];
 
 
 
Search WWH ::




Custom Search