来自表视图控制器的多个segue [英] Multiple segues from table view controller

查看:73
本文介绍了来自表视图控制器的多个segue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小应用程序,它使用多个部分布局作为初始表格视图。一个部分显示Twitter的最新趋势,另一部分显示Twitter的最新故事。当我点击趋势列表中的项目时,我转换到一个新的表格视图控制器,显示有关该趋势的最新推文。在故事部分的根控制器中,我看到能够在包含图像,链接等的不同视图控制器中显示更多信息。问题是,当我在故事部分中选择任何内容时,我被推送到为趋势部分设置的表视图控制器。我已经命名了每个segue并为我想要转换到的两个视图都有自定义类,我这样做是为了检查调用哪个segue:

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?

推荐答案

你的问题中没有足够的信息可以肯定,但这听起来像是我称之为自动的问题与手动分段和每个分区的限制。

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.

通过从(原型)表格单元格或其他控件拖动,在IB中创建自动 segue 。关于它的好处是,它是自动的 - 点击控件执行segue,你需要在代码中执行的只需实现 prepareForSegue:sender: so目标视图控制器获取正确的数据。缺点是任何给定的控件(包括原型表格单元格)只能有一个传出的segue(否则,故事板不知道自动执行哪个)。

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通过从源视图控制器拖动,在IB中创建手动 segue。这样做的好处是视图控制器可以有多个传出的段。另一方面,它们与可点击控件无关,因此您必须实现确定何时执行的逻辑(并调用 performSegueWithIdentifier:以使其发生) 。

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. 使用多个原型表格单元格 - 然后每个单元格都可以拥有自己的传出自动segue。您需要更改表视图控制器的 tableView:cellForRowAtIndexPath:以检查索引路径的节号并为 dequeueReusableCellWithIdentifier选择适当的标识符:,但如果趋势和故事单元格的内容不同,这可能会使事情变得更方便或更有效。

  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.

使用手动segues。然后你的表视图控制器可以实现 tableView:didSelectRowAtIndexPath:来调用 performSegueWithIdentifier:,并根据索引选择适当的标识符路径的部分。

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.

无论哪种方式,你的 prepareForSegue:sender:实施看起来很好。

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

这篇关于来自表视图控制器的多个segue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆