【问题标题】:UISearchBar using prepareForSegueUISearchBar 使用 prepareForSegue
【发布时间】:2013-02-19 09:05:03
【问题描述】:

我正在做一个小项目,但遇到了问题。我有一个带有 UISearcBar 的 UITableView。一切正常,搜索给了我正确的结果,但现在我想使用 prepareForSegue 方法来为每个搜索结果转到 detailViewController。

例如。 if I search for product "A", and found it, when choose that produt it goes for a ViewController_A, if I search and chose product "B" it should go for ViewControler_B.

此时,这段代码没有我选择的任何东西,它总是转到同一个 Viewcontroller。

#pragma mark - TableView Delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Perform segue to candy detail
    [self performSegueWithIdentifier:@"candyDetail" sender:tableView];


}

#pragma mark - Segue
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"candyDetail"]) {
        UIViewController *candyDetailViewController = [segue destinationViewController];



        // In order to manipulate the destination view controller, another check on which table (search or normal) is displayed is needed
        if(sender == self.searchDisplayController.searchResultsTableView) {
            NSIndexPath *indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
            NSString *destinationTitle = [[filteredCandyArray objectAtIndex:[indexPath row]] name];
            [candyDetailViewController setTitle:destinationTitle];
        }
        else {
            NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
            NSString *destinationTitle = [[candyArray objectAtIndex:[indexPath row]] name];
            [candyDetailViewController setTitle:destinationTitle];
        }

    }
        }

【问题讨论】:

    标签: ios search searchbar


    【解决方案1】:

    那是因为你总是调用相同的 segueId,“candyDetail”。

    相反,您应该在UIStoryBoard 中连接两个手动转场,每个都指向不同的场景(一个 id 为“showViewControllerA”的,指向ViewControllerA,另一个“showViewControllerB”指向ViewControllerB) .然后您可以执行以下操作:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if ([[self.candyArray objectAtIndex:indexPath.row] isKindOfClass:[CandyA class]]) {
            [self performSegueWithIdentifier:@"showViewControllerA" sender:self];
        } else if ([[self.candyArray objectAtIndex:indexPath.row] isKindOfClass:[CandyB class]]) {
            [self performSegueWithIdentifier:@"showViewControllerB" sender:self];
        };
    }
    
    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([[segue identifier] isEqualToString:@"showViewControllerA"]) {
            ViewControllerA *viewControllerA = [segue destinationViewController];
            // configure viewControllerA here...
        } else if ([[segue identifier] isEqualToString:@"showViewControllerA"]) {
            ViewControllerB *viewControllerB = [segue destinationViewController];
            // configure viewControllerB here...
        }
    }
    

    另一种选择是您可以将动作转场直接连接到不同的单元格,并根据源数组中的糖果类型切换您在-tableView:cellForRowAtIndexPath: 中出列的单元格类型。无论哪种方式,您都需要两个指向不同场景的转场。

    【讨论】:

    • 谢谢。如果我有 N 个项目,我必须创建 N 个 Viewcontroller 对吗?因为我给你的例子只使用了两个项目,但在这个项目中可能会有 20 个项目。
    • 您不需要 N 个视图控制器来处理 N 个项目 - 您可以使用单个目标场景很好地服务所有 20 个项目。您最初的问题询问如何根据所选行启动 不同 目标视图控制器类。如果它只是您想要设置的 same 目标视图控制器类的属性(例如标题和其他公共属性),那么您的原始代码应该可以正常工作。
    • 谢谢。但是不是很懂。我有 20 个项目,具有不同的特征,如名称、大小、格式、日期。此信息是静态的,并且位于每个糖果的 ViewController 中。所以如果我选择 Candy1,prepareForSegue 应该去 VC1,如果我选择 Candy 12,它应该去 VC12。所以我需要在 TableViewController 中选择的糖果和细节 VC 之间进行匹配。
    • 对于那个设计,我的回答是正确的:你需要对每个视图控制器场景使用不同的 segue。 然而,问题是这样的:“信息是静态的,并且在每个糖果的 ViewController 中”。将糖果信息保存为模型数组或NSDictionary 对象数组并拥有一个带有公共candy 属性的segue + 视图控制器会更好,您在-prepareForSegue 中设置了目标视图控制器-viewWillAppear。请参阅Introducing Interface Builder Storyboarding 上的 2011 WWDC 视频以了解更多信息。
    猜你喜欢
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    • 2017-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多