How to Change the Split View Controller in Your iOS App to a Detail View Controller Relationship - dummies

How to Change the Split View Controller in Your iOS App to a Detail View Controller Relationship

By Jesse Feiler

You may want to delete the Detail View controller object from the iPad storyboard for our iOS app (but be sure not to delete the DetailView controller class files in the Project navigator). You’ll replace the relationship between the Split View controller and the Detail View controller with one to the Weather controller.

The Weather controller was chosen arbitrarily here — you could just as easily have replaced it with any of the other view controllers. This is the Detail view the user will see when the app is launched.

The following steps show you how to replace the Detail View controller.

  1. In the Document Outline or on the canvas, select the Navigation controller associated with the Detail View controller and press Delete.

    image0.jpg

    It may be easiest to select the Detail View controller in the Document Outline in order to highlight it. Then you can probably find the Navigation controller right next to it.

  2. Select the Detail View controller in the Document Outline or canvas and press Delete as well.

    image1.jpg

    You’ll need to create a new relationship between the Split View controller and the Weather controller.

  3. Select the Split View controller on the storyboard or Document Outline, Control-drag to the Weather controller, and then select Relationship – Detail View Controller from the pop-up menu that appears.

    image2.jpg

    The final result of what has been added to the storyboard so far should look like this. Things have been rearranged a bit for the sake of clarity. Make certain that everything is named as you rearrange the view controllers. Then you can zoom in and out to see what makes the most logical arrangement.

    image3.jpg

You’re also going to have to make some changes in application:DidFinishLaunchingWithOptions:. Add the code in bold to that method in AppDelegate.m.

- (BOOL)application:(UIApplication *)application
  didFinishLaunchingWithOptions:
       (NSDictionary *)launchOptions
{
 if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
 UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
 if ([splitViewController.viewControllers[1]
   isKindOfClass:[UINavigationController class]]) {
  UINavigationController *detailNavigationController = [splitViewController.viewControllers
            lastObject];
  splitViewController.delegate =
   (id)navigationController.topViewController;
 }
 else
  splitViewController.delegate =
   [splitViewController.viewControllers lastObject];
… the rest of the method
}

The change you make here is in how you get the Detail View controller that you will assign as the Split View controller delegate. The Split View controller manages two view controllers, with the last one in its list of controllers corresponding to what’s displayed in the Detail view.

You check the last view controller to see whether it’s a Navigation controller (that contains the Detail View controller) by sending it the isKindOfClass: message. This method returns a Boolean indicating whether it is a UINavigationController.

if([splitViewController.viewControllers[1]isKindOfClass:[UINavigationController class]]) {
UINavigationController *detailNavigationController =
[splitViewController.viewControllers lastObject];
splitViewController.delegate =
(id)detailNavigationController.topViewController;
}

If it is a Navigation controller, you need to find its view controller (it’s the one that has adopted the SplitViewControllerDelegate protocol, implemented the protocol methods, and can become the Split View controller’s delegate), which you get by accessing the Navigation controller’s topViewController property. (It points to the first and only view controller on its stack.) Then you’ll assign it as the delegate.

If it’s a view controller, rather than a Navigation controller (which it will be because you just made the Weather controller the Detail View controller, and it has no Navigation controller), you just assign that view controller as the delegate.

else splitViewController.delegate =   [splitViewController.viewControllers lastObject];

If you build and run your project now, it looks like you’ve taken a step backward; all you’ll see is a blank screen — in Portrait orientation no less (unless you’ve kept the Test Drive controller as the initial Detail View controller).