objective-c - 来自 TableView Controller 的多个 segue

标签 objective-c ios storyboard ios5

我有一个小应用程序,它为初始表格 View 使用多个部分布局。一个部分显示来自 Twitter 的最新趋势,另一个部分显示来自 Twitter 的最新故事。当我点击趋势列表中的一个项目时,我会转换到一个新的 TableView Controller ,它显示关于该趋势的最新推文。在故事部分的根 Controller 中,我希望能够在包含图像、链接等的不同 View Controller 中显示更多信息。问题是,当我在故事部分中选择任何内容时,我被推送到为趋势部分设置的 TableView Controller 。我已经为每个 segue 命名,并为我想要转换到的两个 View 都有自定义类,我这样做是为了检查正在调用哪个 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];
    }
}

关于如何获得不同的 View 有什么建议吗?

最佳答案

您的问题中没有足够的信息可以确定,但这听起来像是我所说的自动与手动转场以及对每个转场的限制的问题。

通过从(原型(prototype))表格单元格或其他控件拖动,在 IB 中创建了一个自动 segue。它的好处是它是自动的——点击控件执行 segue,你需要在代码中做的就是实现 prepareForSegue:sender: 以便目标 View Controller 得到正确的数据。缺点是任何给定的控件(包括原型(prototype)表格单元格)只能有一个传出的 segue(否则, Storyboard将不知道自动执行哪个)。

手动转场是通过从源 View Controller 拖动在 IB 中创建的。这样做的好处是 View Controller 可以有多个传出 segues。另一方面,它们不与可点击控件相关联,因此您必须实现逻辑来确定何时执行哪个(并调用 performSegueWithIdentifier: 来实现)。

鉴于这些权衡,您的问题有两种可能的解决方案:

  1. 使用多个原型(prototype)表格单元格——然后每个表格单元格都可以有自己的传出自动转场。您需要更改 TableView Controller 的 tableView:cellForRowAtIndexPath: 以检查索引路径的部分编号并为 dequeueReusableCellWithIdentifier: 选择适当的标识符,但这可能会使事情变得更如果您的趋势和故事单元无论如何都具有不同的内容,那么方便或高效。

  2. 使用手动转场。然后您的 TableView Controller 可以实现 tableView:didSelectRowAtIndexPath: 来调用 performSegueWithIdentifier:,并根据索引路径的部分选择适当的标识符。

无论哪种方式,您的 prepareForSegue:sender: 实现看起来都不错。

关于objective-c - 来自 TableView Controller 的多个 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10196031/

相关文章:

ios - 在 subview 上绘制矩形?

iphone - 以类似网格的方式布局不同尺寸图像的算法

ios - 如何仅在设置数据后调用完成处理程序?

ios - 还有一个 EXC_BAD_ACCESS,不知道为什么

ios - Segue 已配置但未触发

objective-c - 停止 Xcode 自动为 Objective-C 头文件生成 Swift 接口(interface)

ios - Storyboard View 调整大小问题

ios - 快速查看生成器 iOS

wpf - 绑定(bind)到 DoubleAnimation.To

ios - 带有 TextField 的静态单元格 - 将类属性连接到 viewcontroller