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

查看:19
本文介绍了来自表视图控制器的多个 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?

推荐答案

您的问题中没有足够的信息可以确定,但这听起来像是一个问题,我称之为自动与手动 segues 以及每个问题的限制.

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,你需要在代码中执行 prepareForSegue:sender: 以便目标视图控制器获取正确的数据.缺点是任何给定的控件(包括原型表单元格)只能有一个传出 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).

通过从源视图控制器拖动,在 IB 中创建了一个 手动 segue.这样做的好处是视图控制器可以有多个传出的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. 使用多个原型表单元格——然后每个单元格都可以有自己的传出自动转场.您需要更改表格视图控制器的 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.

使用手动转场.然后你的表视图控制器可以实现 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天全站免登陆