これを行う方法は次のとおりです。最初に、City View Controller と呼ばれるプロパティを作成して合成しますselectedValue
@property (nonatomic, strong) NSString *selectedValue;
次に、最後のレベルの tableViewController で値が選択されたら、UINavigationController のナビゲーション スタックで City ビューコントローラーを見つけ、そのビューコントローラーでプロパティを設定し、ナビゲーション スタックをそのビューコントローラーにポップします。
したがって、最後のレベルのviewControllerでは、コードは次のようになります。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *rowValue = [dataArray objectAtIndex:[indexPath row]]; // e.g. McDonald's
// loop through view controller stack to find the city viewcontroller
// (if the index of the city viewcontroller will always be the same you could just go directly to that index)
NSArray *viewControllers = [self.navigationController viewControllers];
for (UIViewController *vc in viewControllers) {
if ([vc isMemberOfClass:[CityViewController class]]) {
CityViewController *cityVC = (CityViewController*)vc;
cityVC.selectedValue = rowValue;
[self.navigationController popToViewController:cityVC animated:YES];
break;
}
}
}