Database Reference
In-Depth Information
Here we see the value of using the -prepareForSegue: sender: method only for
branching. Had we put all of the flow logic into this one method, it would
easily be 100 lines of code or more and be a mess to maintain.
All of the UIViewController instances that are accessed from the edit UITableViewCon-
troller fall into one of two categories: edit something or select something. Let's
look at an example of both kinds of instance. See Figure 47, Reused text edit
view controller , on page 217 .
Text Edit View Controller
The PPRTextEditViewController is easily the most reused UIViewController . The bulk of
a recipe is text, and the text is probably going to need to be edited. As a result,
the process of editing in our application must be highly reusable. This is also
a great opportunity to use a block callback to assist in the reusability of the
PPRTextEditViewController .
RecipesV1/PPRecipes/PPREditRecipeViewController.m
- ( void )prepareForEditRecipeNameSegue:(UIStoryboardSegue *)segue
sender:(id)sender
{
id editRecipeNameViewController = [segue destinationViewController];
NSString *name = [[self recipeMO] valueForKey:@ "name" ];
[[editRecipeNameViewController textField] setText:name];
[editRecipeNameViewController setTextChangedBlock:^ BOOL (NSString *text,
NSError **error) {
NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0];
UITableViewCell *cell = [[self tableView] cellForRowAtIndexPath:path];
[[cell detailTextLabel] setText:text];
[[self recipeMO] setValue:text forKey:@ "name" ];
return YES;
}];
}
The most interesting part of this segue preparation is the callback. The PPR-
TextEditViewController is actually quite dumb. Its entire job is to consume the text
entered into a text field and listen for either the Done button to be clicked or
the Return key to be tapped. When either of those things happens, it takes
the text from the UITextField (or the UITextView ) and passes it to the callback
block.
Note that the block accepts both an NSString and an NSError pointer . The parent,
which defines the block, then validates the text and sets the error if there is
a problem. The PPRTextEditViewController receives a pass/fail from the block. In
the event of a failure, it displays the error.
 
 
 
Search WWH ::




Custom Search