IOS StoryBoard Multiple Segue from TableCell

Hi I have a storyboard and I can show a detailed view when I click on a table cell. I want to add additional functionality so that depending on which cell I click, I show a different view controller. I tried to drag two segments from one cell, but this does not allow.

My thinking was that I would have two segue from the cell, each of which would point to a different view, and then call the desired segment:

- (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"); } } 

Then I processed segue in prepareForSegue file

 - (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, he never shows the second performance. This is because it is impossible to have two segments from the same table cell. I also tried dragging as segue from the controller to each destination, not from the cell, and the other from the controller, but failed ???

+44
ios storyboard segue
Jan 12 2018-12-12T00:
source share
4 answers

Do not try to connect Segues to tableviewcell in this case. Connect them to the view controller itself.

+60
Jan 12 '12 at 16:18
source share
— -

Do not try to create several segments from TableCell for other view controllers, you want to ctrl + drag from the view controller icon under the view controller in the storyboard interface to the view controllers that you want to go to. Then it will allow you to configure several segments.

screenshot showing multiple segues

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

giving segue an identifier

then for the example TableCells in your UITableViewDelegate in

 -tableView:didSelectRowAtIndexPath: 

you can use

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

to manually run segue depending on your own segue selection logic.

+40
Jul 25 '12 at 4:50
source share

Here is 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 the view controller that determines which session should be executed.

As stated above, the key is to create a segue by linking two view controllers.

+4
Sep 13 '13 at 0:12
source share

In fast 3.1, with 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) 

}

+1
May 18 '17 at 7:26
source share



All Articles