Database Reference
In-Depth Information
5.3
Exporting Recipes
In our first demonstration of multithreading, we'll add the ability to export
recipes from our database so that they can be shared. In this new section of
the application, we will create an NSOperation , which will create its own NSMan-
agedObjectContext and use it to copy the selected recipes into a JSON structure,
which can then be used by the application in several ways (uploaded to a
server, emailed to a friend, and so on).
To implement this addition to our application, we need to make a few changes
to the user interface. We want to add a button to the UINavigationBar that is a
generic action item. When the button is tapped, it will display an action sheet
and give the user an option to mail the recipe (other options can be added
later), as shown in Figure 15, Linking the Import menu item , on page 82 .
To accomplish this, we first add the button in the storyboard and associate
the button with a new method called -action: . Inside the -action: method, we
construct a UIActionSheet and present it to the user.
Baseline/PPRecipes/PPRDetailViewController.m
- (IBAction)action:(id)sender;
{
UIActionSheet *sheet = [[UIActionSheet alloc] init];
[sheet addButtonWithTitle:@ "Mail Recipe" ];
[sheet addButtonWithTitle:@ "Cancel" ];
[sheet setCancelButtonIndex:([sheet numberOfButtons] - 1)];
[sheet setDelegate:self];
[sheet showInView:[self view]];
}
Once the user makes a choice with the UIActionSheet , our PPRDetailViewController
will get a callback as its delegate. If the user clicked Cancel, we simply return.
Otherwise, we drop into a switch statement to handle the choice the user has
made. In this example, we have only one choice available.
Baseline/PPRecipes/PPRDetailViewController.m
- ( void )actionSheet:(UIActionSheet*)actionSheet
didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == [actionSheet cancelButtonIndex]) return ;
switch (buttonIndex) {
case 0: //Mail Recipe
[self mailRecipe];
break ;
default :
ALog(@ "Unknown index: %i" , buttonIndex);
}
}
 
 
 
Search WWH ::




Custom Search