While in portrait mode, the master view controller is missing in action. It would be nice if you could see the master view controller to select a new post from the list without having to rotate the device. UISplitViewController lets you do just that by supplying its delegate with a UIBarButtonItem. Tapping this button shows the master view controller in a specialized UIPopoverController (Figure 22.4).
In your code, whenever a detail view controller was given to the split view controller, that detail view controller was set as the split view controller’s delegate. As the delegate, the detail view controller will get a pointer to the UIBarButtonItem when rotating to portrait mode.
In BNRWebViewController.h, add this declaration:
@interface BNRWebViewController : UIViewController <UISplitViewControllerDelegate>
Build and run the application. The behavior will be the same, but there will not be any warnings.
In BNRWebViewController.m, implement the following delegate method to place the bar button item in the BNRWebViewController’s navigation item.
- (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc { // If this bar button item does not have a title, it will not appear at all barButtonItem.title = @"Courses"; // Take this bar button item and put it on the left side of the nav item self.navigationItem.leftBarButtonItem = barButtonItem; }
Notice that you explicitly set the title of the button. If the button does not have a title, it will not appear at all. (If the master view controller’s navigationItem has a title, then the button will be automatically set to that title. )
Build and run the application. Rotate to portrait mode, and you will see the bar button item appear on the left of the navigation bar. Tap that button, and the master view controller’s view will appear in a UIPopoverController.
This bar button item is why we always had you put the detail view controller inside a navigation controller. You do not have to use a navigation controller to put a view controller in a split view controller, but it makes using the bar button item much easier. (If you do not use a navigation controller, you can instantiate your own UINavigationBar or UIToolbar to hold the bar button item and add it as a subview of the BNRWebViewController’s view.)
There are two small issues left to address with your Courses button. First, when the device is rotated back to landscape mode, the button is still there. To remove it, the delegate needs to respond to another message from the UISplitViewController. Implement this delegate method in BNRWebViewController.m.
- (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem { // Remove the bar button item from the navigation item // Double check that it is the correct button, even though we know it is if (barButtonItem == self.navigationItem.leftBarButtonItem) { self.navigationItem.leftBarButtonItem = nil; } }
Build and run the application. The Courses button will now appear and disappear as you rotate between portrait and landscape modes.