Lesson 20

Popovers and Modal Views

Popovers and modal views provide ways to temporarily display some information to users. The information that is displayed is usually contextual and related to an action performed by the user.

Both popovers and modal views interrupt the user's journey through your application; the user must interact with the popover/modal view before using the rest of the application. Popovers are dismissed by tapping outside the bounds of the popover; modal views are dismissed by using a user-defined cancel button located in the modal view. Popovers are only available on iPads, whereas modal views are available on both the iPad and iPhone.

Popovers

A popover view is one that is revealed when a control is tapped. A popover appears attached to the control that was tapped to reveal it (see Figure 20.1).

Screenshot of iOS device with a flower with yellow petals, with a box on left bottom, with the Height (pixels) set to 451.0, Width (pixels) set to 676.0, and Colorspace set to RGB.

Figure 20.1

Popovers are only supported on the iPad, and should be used to display additional information related to the control that displays it. When presenting a popover, you do not provide a Done or Cancel button; popovers are dismissed when the user taps outside the popover.

To present a scene in your storyboard in a popover, simply create a popover presentation segue from a button in one of the other scenes of your view controller to the scene you wish to use within the popover (see Figure 20.2).

Screenshot of a storyboard screen with image of flower in center section, with an arrow from the words Image Information to Action Segue menu.

Figure 20.2

You can use the Attribute inspector to configure the popover presentation segue (see Figure 20.3). The Anchor attribute references a button (or bar button item) in the presenting view controller. The popover will be anchored to this control. By default, popovers are dismissed as soon as the user taps outside them. If you do not want the popover to be dismissed when some controls are tapped, you can set up the Passthrough attribute to reference them.

Screenshot of Storyboard Segue menu, with image information selected in blue.

Figure 20.3

Modal Views

A modal view can be created in a couple of different ways; the most common is to use a Present Modally segue from a button in a scene to another scene (see Figure 20.4).

Screenshot of a storyboard screen with Present Modally seg... grayed under View Controller Scene and Segue under Storyboard Segue set to Present Modally, encircled.

Figure 20.4

The Present Modally segue has a few presentation styles that can be set up using the Attribute inspector:

The default setting is to present the modal view so that it takes up the entire screen. A commonly used presentation style is form sheet. On an iPhone, the form sheet presentation style and the full screen presentation style achieve identical effects. However, on the iPad, the form sheet presentation style causes the modal view to appear as a self-contained form centered in the presenting view (see Figure 20.5).

Screenshot of iPad screen with the words This view is presented modally. It prevents the user from interacting with any other view in the application until the modal view has been dismissed and Dismiss Modal View below.

Figure 20.5

When a view is presented modally, there is no system-provided means to dismiss it and return back to the presenting view. If you do not provide a way to dismiss the modal view, then your user will be unable to use the rest of your application (see Figure 20.6).

Screenshot of iPhone screen with the words This view is presented modally. It prevents the user from interacting with any other view in the application until the modal view has been dismissed.

Figure 20.6

Modal views are used when your app needs to collect some vital information before proceeding. Typically you add a close button in the modal view that, when tapped, will dismiss it. You cannot use a segue to dismiss a modal view; instead you must call the dismissViewControllerAnimated method on the modal view controller. This method allows you to provide an optional completion handler that is executed when the modal view is dismissed.

@IBAction func onDismissModalView(sender: AnyObject) {
        self.dismissViewControllerAnimated(true, completion: nil);
    } 

If you do not wish to use a segue, you can alternately present a scene in your storyboard modally by using the following code snippet attached to a UIButton instance in the presenting view controller:

@IBAction func onPresentModalView(sender: AnyObject) {
        let modalViewController:ModalViewController =
(self.storyboard!.instantiateViewControllerWithIdentifier
("ModalViewController") as? ModalViewController)!
        modalViewController.modalPresentationStyle =
 UIModalPresentationStyle.FormSheet
        self.presentViewController(modalViewController,
animated: true, completion: nil)
    }

Try It

In this Try It, you create a simple iPad-only application based on the Single View Application template called PopoverTest, which displays an image and some information on the image in a popover.

Lesson Requirements

  • Launch Xcode.
  • Create a new iPad-only project based on the Single View Application template.
  • Create a storyboard with multiple scenes.
  • Use Interface Builder to create segues between scenes.
  • Present a scene in a popover.

Hints

  • When creating a new project, you can use your website's domain name as the Company Identifier in the Project Options dialog box.
  • To show the Object library, select View arrow Utilities arrow Show Object Library.
  • To show the assistant editor, select View arrow Assistant Editor arrowShow Assistant Editor.

Step-by-Step