Graphics Programs Reference
In-Depth Information
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:detailViewController];
[self presentViewController:navController animated:YES completion:nil];
}
Build and run the application and tap the
New
button to create a new item. An instance of
DetailViewController
will slide up from the bottom of the screen with a
Done
button and a
Cancel
button on its navigation item. (Tapping these buttons, of course, will
throw an exception since you haven't implemented the action methods yet.)
Dismissing modal view controllers
To dismiss a modally-presented view controller, you must send the message
dismis-
sViewControllerAnimated:completion:
to the view controller that presented
it. You've done this before with
UIImagePickerController
- the
De-
tailViewController
presented it, and when the image picker told the
De-
tailViewController
it was done, the
DetailViewController
dismissed it.
Now, we have a slightly different situation. When a new item is created, the
Item-
sViewController
presents the
DetailViewController
modally. The
De-
tailViewController
has two buttons on its
navigationItem
that will dismiss it
when tapped:
Cancel
and
Done
. There is a problem here: the action messages for these
buttons are sent to the
DetailViewController
, but it is the responsibility of the
ItemsViewController
to do the dismissing. The
DetailViewController
needs a way to tell the view controller that presented it, “Hey, I'm done, you can dismiss
me now.”
Fortunately, every
UIViewController
has a
presentingViewController
property that points to the view controller that presented it. The
DetailViewCon-
troller
will grab a pointer to its
presentingViewController
and send it the
message
dismissViewControllerAnimated:completion:
. Implement the ac-
tion method for the
Save
button in
DetailViewController.m
.
- (void)save:(id)sender
{
[[self presentingViewController] dismissViewControllerAnimated:YES
completion:nil];
}