iPad Man
iPad Man

Reputation: 50

How do I segue to different detail view controllers?

In a split view controller app,how can I segue to different detail view controllers upon selecting a table row in the master view controller?

Just to be clear, I need the detail view controller to be replaced when I select a row in the master view controller. How do I wire up the view controllers? From the split view controller? or from the detail view navigation controller?

Upvotes: 1

Views: 5661

Answers (5)

IdleGod
IdleGod

Reputation: 369

In your segue, set your style to "Push", and your destination to "Detail". Current will push the destination view controller onto your Master view, whereas Detail will push it into the "Detail" view. It's that simple. Then wire it up the same way you wire everything else up.

But be careful, if you don't implement a way for it to wait for a previous segue, you can get an "Unbalanced calls" error if a new Controller is pushed onto the detail view before it's done dismissing/pushing another one. Double tapping a cell in a table will do it.

Upvotes: 0

user2982524
user2982524

Reputation:

Use this code:

UINavigationController *detailNavigationController =[[[self splitViewController] viewControllers] objectAtIndex:1];                
  [detailNavigationController pushViewController:"your_view_controller" animated:YES];

Upvotes: 0

Ole Begemann
Ole Begemann

Reputation: 135548

Implement tableView:didSelectRowAtIndexPath: in the master table view's delegate. Depending on the value of the indexPath parameter, call [detailViewController performSegueWithIdentifier:sender:] with the segue identifier of your choice.

Upvotes: 2

Leijonien
Leijonien

Reputation: 1413

// Get detail navigation controller
UINavigationController *detailNavigationController = [splitViewController.viewControllers objectAtIndex:1];

// Push the detail view controller
[detailNavigationController pushViewController:anyDetailViewController animated:NO];

// You also might need to set the splitview controller's delegate to this view controller
splitViewController.delegate = anyDetailViewController;

Upvotes: 1

neilco
neilco

Reputation: 8012

In your tableView:didSelectRowAtIndexPath: method, do this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    [self performSegueWithIdentifier:@"YourSegueIdentifier" sender:self];
}

If you need to perform different segues based on the selected row, do this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSString *segueIdentifier = nil;

    switch(indexPath.row) {
        case 0:
            segueIdentifier = @"YourSegueIdentifier";
            break;
        case 1:
            segueIdentifier = @"ADifferentSegueIdentifier";
            break;
        .
        .
        .
    }

    if (segueIdentifier != nil) {
        [self performSegueWithIdentifier:segueIdentifier sender:self];
    }
}

Upvotes: 2

Related Questions