Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple segues from table view controller

I have a small app that uses multiple section layouts for the initial table view. One section displays the most recent trends from Twitter and the other section displays the most recent stories from Twitter. When I click on an item within the list of trends I transition to a new table view controller that displays the most recent tweets about that trend. Within the root controller for the stories section I wat to be able to display more information in a different view controller that contains images, links, and so forth. The problem is that when I select anything within the stories section, I am being pushed to the table view controller that is set up for the trends section. I have named each segue and have custom classes for both of the views that I want to transition to and I am doing this to check which segue is being called:

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if([[segue identifier] isEqualToString:@"viewTrendsSearch"]) {

        //get the controller that we are going to segue to
        SearchTrendResultsViewController *strvc = [segue destinationViewController];

        //get the path of the row that we want from the table view
        NSIndexPath *path = [self.tableView indexPathForSelectedRow];

        //here we get the trend object from the array we set up earlier to hold all trends
        Trends *results = [currentTrends objectAtIndex:[path row]];

        //pass the object that was selected in the table view to the destination view
        [strvc setQuery: results];
    }

    if([[segue identifier] isEqualToString:@"storyfullDetails"]) {

        StoriesViewController *svc = [segue destinationViewController];

        NSIndexPath *path = [self.tableView indexPathForSelectedRow];

        Stories *results = [currentStories objectAtIndex:[path row]];

        [svc setStory:results];
    }
}

Any suggestions on how to get to the different views?

like image 572
swallace Avatar asked Apr 17 '12 17:04

swallace


1 Answers

There's not quite enough information in your question to be sure, but this sounds like an issue with what I'd call automatic versus manual segues and the restrictions on each.

An automatic segue is created in IB by dragging from a (prototype) table cell or other control. The nice thing about it is that it's, well, automatic -- tapping the control performs the segue, and all you need to do in your code is implement prepareForSegue:sender: so that the destination view controller gets the right data. The downside is that any given control (including prototype table cells) can only have one outgoing segue (otherwise, the storyboard wouldn't know which to automatically perform).

A manual segue is created in IB by dragging from the source view controller. The upside to this is that a view controller can have multiple outgoing segues. On the other hand, they aren't associated with a tappable control, so you have to implement logic that determines which to perform when (and calls performSegueWithIdentifier: to make it happen).

Given those tradeoffs, there are two possible solutions to your problem:

  1. Use multiple prototype table cells -- then each can have its own outgoing automatic segue. You'll need to change your table view controller's tableView:cellForRowAtIndexPath: to check the index path's section number and choose the appropriate identifier for dequeueReusableCellWithIdentifier:, but this might make things more convenient or efficient if your trend and story cells have different content anyway.

  2. Use manual segues. Then your table view controller can implement tableView:didSelectRowAtIndexPath: to call performSegueWithIdentifier: with the appropriate identifier chosen based on the index path's section.

Either way, your prepareForSegue:sender: implementation looks fine.

like image 184
rickster Avatar answered Sep 27 '22 18:09

rickster