来自表视图控制器的多个segue

时间:2012-04-17 17:25:52

标签: objective-c ios storyboard ios5

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

-(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];
    }
}

有关如何获取不同观点的任何建议?

1 个答案:

答案 0 :(得分:17)

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

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

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

考虑到这些权衡,您的问题有两种可能的解决方案:

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

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

  3. 无论哪种方式,您的prepareForSegue:sender:实施都可以。

相关问题