我正在做一个小项目,我遇到了一个问题。我有一个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];
}
}
}
谢谢。如果我有N个项目,我必须创建N Viewcontroller的权利?因为我给你的例子只使用两个项目,但在这个项目中可能会有20个项目。 – 2013-02-19 02:34:49
对于N个项目,您不需要N个视图控制器 - 只需一个目标场景就可以为所有20个项目提供服务。您的原始问题将根据所选的行询问如何启动_different_目标视图控制器类。如果它只是您要设置的_same_目标视图控制器类的属性(例如标题和其他公共属性),那么您的原始代码应该工作得很好。 – followben 2013-02-19 02:45:57
谢谢。但是不太明白。我有20个不同的字符,比如名字,大小,格式,日期。这个信息是静态的,并且在每个糖果的ViewController中。所以如果我选择Candy1,prepareForSegue应该去VC1,如果我选择了Candy12,它应该去VC12。所以我需要在TableViewController中的选定糖果和细节VC之间进行匹配。 – 2013-02-19 14:54:06