Search code examples
iosobjective-csegueuisplitviewcontrolleruistoryboardsegue

Using "Show detail" segue to replace detail view controller in spilt view controller


I am using a Universal storyboard with adaptive segues to build my interface. I have a UISplitViewController with a UITableViewController set as the master view controller. I am using this table to present two different types of information, so when the user taps on a cell I need to figure out what cell they tapped in order to display one of two different view controllers as appropriate for the selected cell. I have the relationship set up in Storyboard to have the detail view controller be the first of the two view controllers, which is fine since I render UI overtop anyways until they tap a cell. I discovered I can create a segue from the table view controller to my other view controller that should be displayed when an appropriate cell is tapped - this is a "Show detail (e.g. Replace)" segue. I gave it an identifier and I simply call [self performSegueWithIdentifier:ReplaceDetailSegueIdentifier sender:self]; when an appropriate row is selected to go ahead and replace the existing view controller in the detail with the correct view controller. This works great when I select a cell that should show the second view controller. It properly replaces the first detail view controller with the second view controller. So then I set up another segue that will be performed when the user then taps a row that should show the original first view controller. But when that occurs, the app crashes at the line where the segue is performed (but no info is logged to Console - just states EXC_BAD_ACCESS).

So it seems once you have performed a Show detail segue once, subsequent calls to replace the detail view controller crash the app. This is also true if I select a row that replaces the original detail controller with the second view controller and then I select another row that should show that same second view controller - the app crashes.

How would I set it up so that it will properly show the appropriate detail view controller based on the cell they have selected, ensuring the app does not crash?


Solution

  • I discovered the crash is caused by deallocating the detail view controller which is the UISplitViewControllerDelegate, so the master is referencing an object that doesn't exist. The solution is to set the delegate to the new view controller that will be presented (or nil if it doesn't conform to UISplitViewControllerDelegate). Now it works without issue.