Question

I am developing application that doesn't need only to change image or label when different tableView item from Master window is selected (using only one detailViewController), but to change whole appearance (different xib file for every item) so all tutorials found here weren't helpfull.

Can anyone tell me is it possible to switch initial view of detailViewController with one that I created?

UPDATE:

I managed to resolve this problem. Master-Detail template with storyboard is creating array of your MasterViewController and DetailViewController in that order, so if you want to change detail view, you have to update that array from didSelectRowAtIndexPath method in MasterViewController like this:

switch (indexPath.row) {
    case 0:
    {
        NSArray *newVCs = [NSArray arrayWithObjects:    [self.splitViewController.viewControllers objectAtIndex:0], viewArray[0], nil];
        self.splitViewController.viewControllers = newVCs;
        break;

    }
    case 1:
    {
        NSArray *newVCs = [NSArray arrayWithObjects:[self.splitViewController.viewControllers objectAtIndex:0], viewArray[1], nil];
        self.splitViewController.viewControllers = newVCs;
        break;
    }
    //etc
    default:
        break;

Also I've created array of views:

FirstDetailViewController *DVCA = [[FirstDetailViewController alloc] initWithNibName:@"FirstDetailViewController" bundle:[NSBundle mainBundle]];

SecondDetailViewController *DVCB = [[SecondDetailViewController alloc] initWithNibName:@"SecondDetailViewController" bundle:[NSBundle mainBundle]];
//etc

//Create Array of views
viewArray = [NSArray arrayWithObjects:DVCA, DVCB, nil];

UPDATE 2

There is another way to switch detail view to another view. In your storyboard you can add new View Controller and set Storyboard ID from Identity Inspector to be name of your viewControllerClass, segue needs to be set to replace, and his Identifier needs to be same as your viewControllerClass. After that didSelectRowAtIndexPath should look like this:

NSMutableArray* arr = [[NSMutableArray alloc] initWithArray:self.splitViewController.viewControllers]; 

switch (indexPath.row) {
case 1:
{
    FirstDetailViewController *loginVC = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstDetailViewController"];
    [arr replaceObjectAtIndex:1 withObject:loginVC];
    self.splitViewController.viewControllers = arr;
    break;
}
//etc...
default:
     break;
}
Was it helpful?

Solution

Look for the didSelectRowAtIndexPath method in your Master View Controller. It is called when you tap a table view cell. You are given the indexPath tapped. You can link that with your datasource and tell it which ViewController to instantiate & push.

Something like -

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detailViewController;
    switch (indexPath.row) {
        case 0:
            detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController1.xib" bundle:[NSBundle mainBundle]];
            break;
        case 1:
            detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController2.xib" bundle:[NSBundle mainBundle]];
            break;
        case 2:
            detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController3.xib" bundle:[NSBundle mainBundle]];
            break;
            //etc
        default:
            break;
    }
    [self.navigationController pushViewController:detailViewController animated:YES];
}

OTHER TIPS

Just create several xibs in your storyboard all using the same controller and have different prototype cells point to each one of them that way you have very little coding to do you just pick the right prototype cell for each row.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top