2013-02-19 60 views
0

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

例如。如果我搜索产品“A”,并找到它,当选择该产品时,它将用于ViewController_A,如果我搜索并选择了产品“B”,它应该用于ViewControler_B。

在这一刻,这个代码没有mather我选择,它总是去相同的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]; 
     } 

    } 
     } 

回答

0

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

相反,你应该有迷上了两个手动塞格斯在UIStoryBoard,每个都指向不同的场景(一个与“showViewControllerA”去ViewControllerA和其他的“showViewControllerB”在ViewControllerB指向的ID)。然后可以执行以下操作:

- (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:出列根据源阵列中的糖果型的细胞类型。无论哪种方式,您都需要两个指向不同场景的细节。

+0

谢谢。如果我有N个项目,我必须创建N Viewcontroller的权利?因为我给你的例子只使用两个项目,但在这个项目中可能会有20个项目。 – 2013-02-19 02:34:49

+0

对于N个项目,您不需要N个视图控制器 - 只需一个目标场景就可以为所有20个项目提供服务。您的原始问题将根据所选的行询问如何启动_different_目标视图控制器类。如果它只是您要设置的_same_目标视图控制器类的属性(例如标题和其他公共属性),那么您的原始代码应该工作得很好。 – followben 2013-02-19 02:45:57

+0

谢谢。但是不太明白。我有20个不同的字符,比如名字,大小,格式,日期。这个信息是静态的,并且在每个糖果的ViewController中。所以如果我选择Candy1,prepareForSegue应该去VC1,如果我选择了Candy12,它应该去VC12。所以我需要在TableViewController中的选定糖果和细节VC之间进行匹配。 – 2013-02-19 14:54:06