Search code examples
iosobjective-cuisplitviewcontrolleriphone-6-plus

Split View Controller for iPhone 6 Plus


I want to adapt my application to the iPhone 6+ by using a Split View Controller. Currently, my app is a simple Table View, listing my articles and a Detail View.

I want my app to switch to a split view when in landscape mode on iPhone 6 Plus, but I don't know how to make that properly.

I created a new storyboard and integrated a Split View controller and after that, I'm stuck. I don't know how to push my news to the Detail View Controller when I tap a cell on the Master View (which is a Table View). I tried to add this in the didSelectRowAtIndexPath method :

[self.navigationController pushViewController:viewController animated:YES];

but when I do that, the detail view is pushed inside the left part of the Split View (the Master View).

I also tried to add a "show details" segue between the Table View Controller cell and the Detail View Controller, and add in the didSelectRowAtIndexPath method :

[self performSegueWithIdentifier:@"showDetails" sender:self];

but same problem, the view is pushed inside the Master View.

Other problem, in portrait mode, the default View is the Detail View. How I can show the Master View ?

Thanks in advance for your help !


Solution

  • From within your Master view controller, you should be able to access the SplitViewController itself usingself.splitViewController. The SplitViewController has a property, viewControlers, which is an array with two elements: the first is the view controller for the master, the second is the view controller for the detail side. In each case, the top controller in the hierarchy is normally a navigation controller. So, from your master controller (tableView) code, you can access the detailController with something like:

    UINavigationController *detailNav = self.splitViewController.viewController[1];
    [detailNav pushViewController:viewController animated:YES];
    

    In practice you should probably use something more sophisticated than a push, to avoid the stack building up terribly as you click on different cells in the master view. But this should get you going. Also, create a test master/detail app using Apple's templates and analyse how they work. That has some code for showing the master view controller in a popup controller, which is how it is presented when in portrait mode.