Search code examples
iosios6uinavigationcontrollerios7storyboard

Master detail ViewController when detailViewController is dynamic


I'm building an app for iPhone, I want to use storyboard in XCode to do a simple selection from a tableView (let's call it master table view controller with couple rows) then through navigation it goes to next page and shows a detail view for that selection. I have in my code a base class representing my detailViewController, and have 2-3 driver classes of this base class representing what I want to show in detail view controller in second page, can someone give me an idea how to set destination viewController (detailViewController) dynamically to one of my child class based on the selected row in master view controller using storyboard?

Usually you assign a class to destination class in storyboard for your detail view controller with using segue, but since my destination class could be a different (child class) how you set this in storyboard?

Thank you, Kam


Solution

  • There are a few ways that you could go about this:

    Storyboard Method

    • Use storyboards to build relationships between your master table view controller and the detail view controllers. In the storyboard, you can totally have more than one segue going from the MasterTableViewController to various other View Controllers.

      `- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

      [self performSegueWithIdentifier:@"detail view identifier" sender:self];

      }

    • Be sure to name your segues inside your storyboard.

    Code Method

    1. Inside your MasterTableViewController:

      `- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

      MyViewController *vc = [[UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:@"view controller identifier"]];

      [self presentViewController:vc animated:YES completion^{}];

      }