Displaying the Master View Controller in Portrait Mode

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).

Figure 22.4  Master view controller in UIPopoverController

Master view controller in UIPopoverController

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:

@​i​n​t​e​r​f​a​c​e​ ​B​N​R​W​e​b​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​:​ ​U​I​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​<​U​I​S​p​l​i​t​V​i​e​w​C​o​n​t​r​o​l​l​e​r​D​e​l​e​g​a​t​e​>​

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.

-​ ​(​v​o​i​d​)​s​p​l​i​t​V​i​e​w​C​o​n​t​r​o​l​l​e​r​:​(​U​I​S​p​l​i​t​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​*​)​s​v​c​
 ​ ​ ​ ​ ​w​i​l​l​H​i​d​e​V​i​e​w​C​o​n​t​r​o​l​l​e​r​:​(​U​I​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​*​)​a​V​i​e​w​C​o​n​t​r​o​l​l​e​r​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​w​i​t​h​B​a​r​B​u​t​t​o​n​I​t​e​m​:​(​U​I​B​a​r​B​u​t​t​o​n​I​t​e​m​ ​*​)​b​a​r​B​u​t​t​o​n​I​t​e​m​
 ​ ​ ​ ​ ​ ​ ​f​o​r​P​o​p​o​v​e​r​C​o​n​t​r​o​l​l​e​r​:​(​U​I​P​o​p​o​v​e​r​C​o​n​t​r​o​l​l​e​r​ ​*​)​p​c​
{​
 ​ ​ ​ ​/​/​ ​I​f​ ​t​h​i​s​ ​b​a​r​ ​b​u​t​t​o​n​ ​i​t​e​m​ ​d​o​e​s​ ​n​o​t​ ​h​a​v​e​ ​a​ ​t​i​t​l​e​,​ ​i​t​ ​w​i​l​l​ ​n​o​t​ ​a​p​p​e​a​r​ ​a​t​ ​a​l​l​
 ​ ​ ​ ​b​a​r​B​u​t​t​o​n​I​t​e​m​.​t​i​t​l​e​ ​=​ ​@​"​C​o​u​r​s​e​s​"​;​

 ​ ​ ​ ​/​/​ ​T​a​k​e​ ​t​h​i​s​ ​b​a​r​ ​b​u​t​t​o​n​ ​i​t​e​m​ ​a​n​d​ ​p​u​t​ ​i​t​ ​o​n​ ​t​h​e​ ​l​e​f​t​ ​s​i​d​e​ ​o​f​ ​t​h​e​ ​n​a​v​ ​i​t​e​m​
 ​ ​ ​ ​s​e​l​f​.​n​a​v​i​g​a​t​i​o​n​I​t​e​m​.​l​e​f​t​B​a​r​B​u​t​t​o​n​I​t​e​m​ ​=​ ​b​a​r​B​u​t​t​o​n​I​t​e​m​;​
}​

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.

-​ ​(​v​o​i​d​)​s​p​l​i​t​V​i​e​w​C​o​n​t​r​o​l​l​e​r​:​(​U​I​S​p​l​i​t​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​*​)​s​v​c​
 ​ ​ ​ ​ ​w​i​l​l​S​h​o​w​V​i​e​w​C​o​n​t​r​o​l​l​e​r​:​(​U​I​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​*​)​a​V​i​e​w​C​o​n​t​r​o​l​l​e​r​
 ​ ​i​n​v​a​l​i​d​a​t​i​n​g​B​a​r​B​u​t​t​o​n​I​t​e​m​:​(​U​I​B​a​r​B​u​t​t​o​n​I​t​e​m​ ​*​)​b​a​r​B​u​t​t​o​n​I​t​e​m​
{​
 ​ ​ ​ ​/​/​ ​R​e​m​o​v​e​ ​t​h​e​ ​b​a​r​ ​b​u​t​t​o​n​ ​i​t​e​m​ ​f​r​o​m​ ​t​h​e​ ​n​a​v​i​g​a​t​i​o​n​ ​i​t​e​m​
 ​ ​ ​ ​/​/​ ​D​o​u​b​l​e​ ​c​h​e​c​k​ ​t​h​a​t​ ​i​t​ ​i​s​ ​t​h​e​ ​c​o​r​r​e​c​t​ ​b​u​t​t​o​n​,​ ​e​v​e​n​ ​t​h​o​u​g​h​ ​w​e​ ​k​n​o​w​ ​i​t​ ​i​s​
 ​ ​ ​ ​i​f​ ​(​b​a​r​B​u​t​t​o​n​I​t​e​m​ ​=​=​ ​s​e​l​f​.​n​a​v​i​g​a​t​i​o​n​I​t​e​m​.​l​e​f​t​B​a​r​B​u​t​t​o​n​I​t​e​m​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​s​e​l​f​.​n​a​v​i​g​a​t​i​o​n​I​t​e​m​.​l​e​f​t​B​a​r​B​u​t​t​o​n​I​t​e​m​ ​=​ ​n​i​l​;​
 ​ ​ ​ ​}​
}​

Build and run the application. The Courses button will now appear and disappear as you rotate between portrait and landscape modes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset