Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - UISplitViewController with storyboard - multiple master views and multiple detail views

I'm trying to put together an iPad app using UISplitViewController and storyboards. The master view starts with a navigation controller linked to a table view of 6 menu options. Each cell in the table pushes a different table view controller onto the navigation stack. This is working fine for the master view. Each master view has a table list which when clicked needs to display a different view controller in the detail pane. I've currently done this with a segue set to 'Replace' and 'Detail Split' which works the first time a row is clicked, but as soon as you click another row in the master view, or rotate the device then the app crashes with EXC_BAD_ACCESS.

I'm fairly sure my problems are to do with how the delegate is setup for the UISplitViewController. I'm confused as to how this should be used when I have multiple master VCs and multiple detail VCs. Where should the delegate code be placed - master or detail? Do I have to implement the UISplitViewControllerDelegate protocol events in every view controller?

Any help appreciated.

like image 810
Jonathan Wareham Avatar asked May 10 '12 16:05

Jonathan Wareham


1 Answers

If the split view controller delegate was the detail view controller that had been replaced, this is the cause of the crash. The replaced detail view controller is being dealloc'd and so the split view controller delegate is no longer a reference to a valid object.

You can update the delegate in prepareForSegue:sender:. For example:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"MySegue"]) {
        UIViewController *destinationViewController = [segue destinationViewController];
        if ([destinationViewController conformsToProtocol:@protocol(UISplitViewControllerDelegate)]) {
            self.splitViewController.delegate = destinationViewController;
        }
        else {
            self.splitViewController.delegate = nil;
        }
    }
}

Which view controllers you use for delegates is dependent on your view controller hierarchy. In the simplest case, any view controllers that are assigned to splitVC detail will probably need to be delegates. You may want to base them all on a common super class that handles the shared split view controller delegate logic.

like image 160
Chris Miles Avatar answered Nov 08 '22 16:11

Chris Miles