テーブルビューでUIViewController(View Controller)を使用して、別のUIViewController(Detail View Controller)にデータを送信しようとしています。私のコードは次のようになります。
//ViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"grapedetail"];
detail.grape = [self.grapes objectAtIndex:indexPath.row];
currentGrape = [self.grapes objectAtIndex:indexPath.row];
NSLog(@"%@",currentGrape.searchName);
[self.navigationController pushViewController:detail animated:YES];
[self performSegueWithIdentifier:@"toGrapeDetail" sender:self];
}
//DetailViewController.m
- (void)viewDidLoad
{
//obtain grape from table view
//grape = ((ViewController *)self.presentingViewController).currentGrape; //I have also tried this method for obtaining the variable with the same result
NSLog(@"Detail view says: %@",grape.searchName);
//etc...
}
アプリをビルドした後、View Controllerに移動し、テーブルセルを選択してDetailViewControllerに移動します。これが私の出力ログです:
2012-07-23 08:13:27.430 GrapeKeeper[593:f803] Detail view says: (null)
2012-07-23 08:13:27.432 GrapeKeeper[593:f803] Alarije
このことから、変数が正しく渡されるように、元のVCが最初に発生することを明確に望んでいますが、詳細ビューviewDidLoad
は元のVCの前に呼び出されているようです。didSelectRowAtIndexPath
これを修正するにはどうすればよいですか?