저는 작은 프로젝트를 진행하고 있습니다. 문제가 있습니다. UISearcBar있는 UITableView 있습니다. 모든 것이 잘 작동하고 검색 결과가 정확하지만 이제는 각 검색 결과에 대한 detailViewController로 이동하기 위해 prepareForSegue 메서드를 사용하려고합니다.prepareForSegue를 사용하는 UISearchBar
예 : 제품 "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 항목이있는 경우 ViewController의 권한을 N 만들 필요가 있습니까? 왜냐하면 저는 두 가지 아이템만을 사용하여 예제를 제공하기 때문에이 프로젝트에서는 아마도 20 개의 아이템이있을 것입니다. –
N 개의 항목에 대해 N 개의보기 컨트롤러가 필요하지 않습니다. 한 개의 대상으로 20 개의 항목을 모두 처리 할 수 있습니다. 원래 질문은 선택한 행을 기반으로 _different_ 대상보기 컨트롤러 클래스를 시작하는 방법을 묻습니다. 설정하려는 _same_ 대상보기 컨트롤러 클래스 (예 : 제목 및 기타 공개 속성)의 속성 일 경우 원래 코드는 정상적으로 작동합니다. – followben
감사. 그러나 잘 이해하지 못합니다. 나는 이름, 크기, 형식, 날짜와 같은 다른 특징들을 가진 20 개의 itens를 가지고있다. 이 정보는 정적이며 각 캔디에 대한 ViewController에 있습니다. 따라서 Candy1을 선택하면 prepareForSegue가 VC1으로 이동하고 Candy 12를 선택하면 VC12로 이동해야합니다. 그래서 나는 TableViewController에서 선택한 사탕과 세부 VC 사이의 어딘가를 만들어야합니다. –