Graphics Programs Reference
In-Depth Information
Before you changed its presentation style, the modal view controller took up the entire
screen, which caused the view of the ItemsViewController to disappear. When the
modal view controller was dismissed, the ItemsViewController was sent the mes-
sages viewWillAppear: and viewDidAppear: and took this opportunity to reload
its table to catch any updates to the BNRItemStore .
With the new presentation style, the ItemsViewController 's view doesn't disappear
when it presents the view controller. So it isn't sent the re-appearing messages when the
modal view controller is dismissed, and it doesn't get the chance to reload its table view.
We have to find another opportunity to reload the data. The code for the Item-
sViewController to reload its table view is simple. It looks like this:
[[self tableview] reloadData];
What we need to do is to package up this code and have it executed right when the modal
view controller is dismissed. Fortunately, there is a built-in mechanism in dismis-
sViewControllerAnimated:completion: that we can use to accomplish this.
Completion blocks
In both dismissViewControllerAnimated:completion: and
presentViewController:animated:completion: , we've been passing nil
as the last argument. Take a look at the type of that argument in the declaration for dis-
missViewControllerAnimated:completion: .
- (void)dismissViewControllerAnimated:(BOOL)flag
completion:(void (^)(void))completion;
Looks strange, huh? This method expects a block as an argument, and passing a block
here is the solution to our problem. So we need to talk about blocks. However, the con-
cepts and syntax of blocks can take a while to get used to, so we're just going to introduce
them briefly. We will return to blocks in Chapter 27 , which is entirely dedicated to blocks.
A block is both a chunk of code and an object at the same time. Blocks are a lot like C
functions, but they are defined inside another method. Once defined, you can have a vari-
able point at that block - just like a variable points at an object. Because you have this
variable, you can pass a pointer to a block as an argument to a method or keep it as an in-
stance variable - just like you can pass a pointer to an object as an argument to a method
or keep a pointer to an object as an instance variable.
Search WWH ::




Custom Search