IOS StoryBoard multiple Segue's from a TableCell

32,368

Solution 1

Don't try to hook up the Segues to a tableviewcell in this case. Hook them up to the View Controller itself.

Solution 2

Don't try to create multiple segues from a TableCell to other view controllers, you want to ctrl+drag from the view controller icon below the view controller in the storyboard interface to the viewcontrollers you want to segue to. Then it will allow you to set up multiple segues.

screenshot showing multiple segues

and then to actually make the segues work, you need to add identifiers to the segues themselves, which you can do by clicking on them and then giving it a name in the property inspector:

giving segue an identifier

then, for the example of TableCells, in your UITableViewDelegate, in

-tableView:didSelectRowAtIndexPath: 

you can use

- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender 

to manually start a segue depending on your own logic of what segue should be chosen.

Solution 3

Here's a sample code from my demo project:

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

    if (type == kCore) {
        segueName = @"segue1";
    } else if (type == kStdlib) {
        segueName = @"segue2";
    }

    [self performSegueWithIdentifier: segueName sender: self];
}

type is a property of view controller, which determines which segue should be performed.

As the above answer said, the key is to create segue by linking two view controllers.

Solution 4

In swift 3.1, with a segment control of two states

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
var segue: String!
if selectedSegment == 0 {
    segue = "segue1"
} else  {
    segue = "segue2"
}
self.performSegue(withIdentifier: segue, sender: self)

}

Share:
32,368
Bear
Author by

Bear

Updated on May 18, 2020

Comments

  • Bear
    Bear about 4 years

    Hi I have a storyboard and am able to show a detail view when clicking on a table cell. I want to add extra functionality so that depending on what cell I click I show a different view controller. I tried dragging two segues from the same cell but it doesn't allow it.

    My thinking was that I would have two segue's from the cell each pointing to a different view and then invoke the desired segue:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        NSInteger row = indexPath.row;
        NSLog(@"Selected Item :-) %@",[NSString stringWithFormat:@"%@",[myData objectAtIndex:row]]);
        if(row %2 ==0){
            NSLog(@"Even");        
            [self performSegueWithIdentifier:@"ShowSecondIndex" sender:self];
        }else{
            [self performSegueWithIdentifier:@"ShowSelectedMovie" sender:self];
            NSLog(@"Odd");
    
        }
    
    } 
    

    I would then handle the segue in prepareForSegue

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
    
        NSLog(@"Prepare For Segue ID:%@",[segue identifier]);
    
        if([[segue identifier] isEqualToString:@"ShowSelectedMovie"]){
            Tab2_ItemViewController *vc = [segue destinationViewController];
            NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
            NSLog(@"Selected Index: %d",selectedIndex);
            [vc setSelectedItem: [NSString stringWithFormat:@"%@",[myData objectAtIndex:selectedIndex]]];
            NSLog(@"String Value: %@",[NSString stringWithFormat:@"%@",[myData objectAtIndex:selectedIndex]]);
            [vc setSelectedIndex:selectedIndex];
    
    
        }else if([[segue identifier] isEqualToString:@"ShowSecondIndex"]){
    
            NSLog(@"Viewing Second Index");
        }
    
    }
    

    However it never shows the second view. Is this because its not possible to have two segues from a single table cell. I also tried dragging both segue's from the controller to each destination rather than one from the cell and one from the controller but sill no luck???

  • Bear
    Bear over 12 years
    I tried this but still no luck, only one of the views was called
  • LJ Wilson
    LJ Wilson over 12 years
    This has to work. I do this all the time. Delete the Segues you have to that VC now and add them back making sure to connect each segue to the VC itself. Then you can call [self performSegueWithIdentifier:@"segueID" sender:nil]; on the didSelectRowAtIndexPath.
  • DonnaLea
    DonnaLea about 12 years
    Sweet! I didn't know you could have segue's from the view controller, I thought they could only go from like a button or similar. :) I'm liking segues a bit more now.
  • Johan Kool
    Johan Kool about 12 years
    @ElJay Great answer, though you probably meant to say View Controller, not just View. (You say as much in your comment, but the abbreviation VC might not be clear to all.)
  • casperOne
    casperOne about 12 years
    Could you flesh this answer out a bit? It's little more than a comment.
  • LJ Wilson
    LJ Wilson about 12 years
    How do you mean? I don't think there is any other way to explain it other than how I did.
  • Sashi
    Sashi about 9 years
    @LJWilson : I want to set couple of variable in the destinationController before performing the segue using self.performSegueWithIdentifier in swift. If it was prepareForSegue i can easily use the segue variable from function and create a controller based on the destination controller as let controller = (segue.destinationViewController as! UINavigationController).topViewController as! PDFDocumentViewController and but how to do this while using self.perforSegue inside didSelectRowAtIndex??
  • LJ Wilson
    LJ Wilson about 9 years
    You would set any properties in the destinationController inside the prepareForSegue method.
  • smoothlandon
    smoothlandon about 9 years
    This is the superior answer as Interface Builder with Storyboards is not super intuitive. Knowing where you control click from is the key :)
  • kjw0188
    kjw0188 over 6 years
    Thank you for describing exactly how to do this in IB!