Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS StoryBoard multiple Segue's from a TableCell

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???

like image 923
Bear Avatar asked Jan 12 '12 15:01

Bear


2 Answers

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

like image 195
LJ Wilson Avatar answered Oct 09 '22 09:10

LJ Wilson


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.

like image 37
mgrandi Avatar answered Oct 09 '22 08:10

mgrandi