Configuring and Displaying a Popover

To display a popover, you’ll need a UIPopoverController, along with a view controller (UIViewController) whose view the popover will contain. UIPopoverController is not itself a UIViewController subclass. The view controller is the UIPopoverController’s contentViewController. You’ll set this property initially through UIPopoverController’s designated initializer, initWithContentViewController:. Subsequently, if you like, you can swap out a popover controller’s view controller (and hence its contained view) by calling setContentViewController:animated:.

Here’s how the UIPopoverController for the first popover in Figure 22-1 is initialized. I have a UIViewController subclass, NewGameController. NewGameController’s view contains a grouped table (whose code I showed you in Chapter 21) and a UIPickerView (see Chapter 11 and Chapter 25), and is itself the data source and delegate for both. I instantiate NewGameController and use this instance as the root view controller of a UINavigationController, giving its navigationItem a leftBarButtonItem (Done) and a rightBarButtonItem (Cancel). I don’t really intend to do any navigation, but the navigation controller’s navigation bar is a convenient way of adding the two buttons to the interface. That UINavigationController then becomes a UIPopoverController’s view controller:

NewGameController* dlg = [NewGameController new]; UIBarButtonItem* b = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemCancel ...

Get Programming iOS 6, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.