Search code examples
iosobjective-cipaduisplitviewdelegate

UISplitView with Multiple Detail Views (with Storyboard)


I've been trying to create a version of this code using a storyboard:

I want to be able to switch between two different detail views, depending on the cell selected in the navigation table. I've tried to implement this by creating a SplitViewManager with a custom setter method that swaps out the detail views each time a different cell is selected. This is the same approach that Apple's sample code uses. The SplitViewManager follows the delegate.

I think my issue is that I haven't connected my splitViewController.delegate to anything, so I can't assign the splitViewManager to anything either. But I can't figure out what I would even connect the delegate to in the storyboard. Please let me know if I'm being an idiot here (almost definitely). Thanks!

My code is below:

DFMAppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.splitViewController = (UISplitViewController *)self.window.rootViewController;
    self.splitViewManager = (DFMSplitViewManager *)self.splitViewController.delegate;

    NSLog(@"split view controller: %@", self.splitViewController); // not null
    NSLog(@"split view controller delegate: %@", self.splitViewController.delegate); // is null
    NSLog(@"split view manager: %@", self.splitViewManager); // is null.

    // But i'm not sure how to assign splitViewController.delegate or splitViewManager in the storyboard.

    return YES;
}

DFMSplitViewManager.m:

- (void)setDetailViewController:(UIViewController<SubstitutableDetailViewController> *)detailViewController
{
    self.detailViewController = detailViewController;

    // Update the split view controller's view controllers array.
    // This causes the new detail view controller to be displayed.
    UIViewController *navigationViewController = [self.splitViewController.viewControllers objectAtIndex:0];
    NSArray *viewControllers = [[NSArray alloc] initWithObjects:navigationViewController, self.detailViewController, nil];
    self.splitViewController.viewControllers = viewControllers;
}

DFMMasterViewController.m:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DFMAppDelegate *appDelegate = (DFMAppDelegate *)[[UIApplication sharedApplication] delegate];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    if (indexPath.row == 0) {
        NSLog(@"clicked cell 1");

        DFMDetailViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];

        [appDelegate.splitViewManager setDetailViewController:detailViewController];

    }
    else {
        NSLog(@"clicked cell 2");

        DFMDetailCollectionViewController *detailCollectionViewController = [storyboard instantiateViewControllerWithIdentifier:@"CollectionViewController"];

        [appDelegate.splitViewManager setDetailViewController:detailCollectionViewController];
    }
}

Solution

  • Turns out you can use the interface builder to add NSObjects to View Controllers. Once I did that, I changed the NSObject's class to DFMSplitViewManager, set it as the SplitViewController's delegate, and it was pretty straight forward from there.