Graphics Programs Reference
In-Depth Information
The
Cancel
button has a little bit more going on. When the user taps the button on the
ItemsViewController
to add a new item to the list, a new instance of
BNRItem
is
created, added to the store, and then the
DetailViewController
slides up to edit
this new item. If the user cancels the item's creation, then that
BNRItem
needs to be re-
moved from the store. At the top of
DetailViewController.m
, import the header
for
BNRItemStore
.
#import "DetailViewController.h"
#import "BNRItem.h"
#import "BNRImageStore.h"
#import "BNRItemStore.h"
@implementation DetailViewController
Now implement the action method for the
Cancel
button in
DetailViewControl-
ler.m
.
- (void)cancel:(id)sender
{
// If the user cancelled, then remove the BNRItem from the store
[[BNRItemStore sharedStore] removeItem:item];
[[self presentingViewController] dismissViewControllerAnimated:YES
completion:nil];
}
Build and run the application. Create a new item and tap the
Cancel
button. The instance
of
DetailViewController
will slide off the screen, and nothing will be added to the
table view. Then, create a new item and tap the
Done
button. The
DetailViewCon-
troller
will slide off the screen, and your new
BNRItem
will appear in the table view.
Note that the
cancel:
and
save:
methods are not declared anywhere. This is okay. Re-
member that declaring a method lets the compiler know that the method exists. For most
methods, the compiler will give you an error when you try to send a message whose meth-
od has not been declared. However, when setting the action of a
UIBarButtonItem
or
UIControl
, the compiler does not validate the action message because it isn't being
called at that point. Instead, the message is validated at runtime when it is actually sent. If
the method is defined, all goes well; if not, you get an unrecognized selector exception.
There is one final note to make. We've said that the
ItemsViewController
presents
the
DetailViewController
modally. This is true in spirit, but the actual relation-
ships are more complicated than that. The
DetailViewController
's
present-
ingViewController
is really the
UINavigationController
that has the
ItemsViewController
on its stack. You can tell this the case because when the
De-