Graphics Programs Reference
In-Depth Information
Here's the important part to understand right now: We can put the code to reload the table
view into a block and pass it to
dismissViewControllerAnim-
ated:completion:
. Then, that code will be executed right after the modal view con-
troller is dismissed.
In
DetailViewController.h
, add a new property for a pointer to a block.
@property (nonatomic, copy) void (^dismissBlock)(void);
This says
DetailViewController
has a property named
dismissBlock
that
points to a block. Like a C function, a block has a return value and a list of arguments.
These function-like characteristics are included in the declaration of a block. This particu-
lar block returns
void
and takes no arguments.
Synthesize this property in
DetailViewController.m
.
@implementation DetailViewController
@synthesize dismissBlock;
We can't create the block itself in
DetailViewController
, though. We have to cre-
ate it in
ItemsViewController
because the
ItemsViewController
is the only
object that knows about its
tableView
.
In
ItemsViewController.m
, create a block that reloads the
ItemsViewCon-
troller
's table and pass the block to the
DetailViewController
. Do this in the
addNewItem:
method in
ItemsViewController.m
.
- (IBAction)addNewItem:(id)sender
{
// Create a new BNRItem and add it to the store
BNRItem *newItem = [[BNRItemStore sharedStore] createItem];
DetailViewController *detailViewController =
[[DetailViewController alloc] initForNewItem:YES];
[detailViewController setItem:newItem];
[detailViewController setDismissBlock:^{
[[self tableView] reloadData];
}];
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:detailViewController];
Now when the user taps a button to add a new item, a block that reloads the
Item-
sViewController
's table is created and set as the
dismissBlock
of the
De-
tailViewController
. The
DetailViewController
will hold on to this block
until the
DetailViewController
needs to be dismissed.