2011-12-19 10 views
2

私はx行のTableViewControllerを持っています。ユーザーが1つの行をタップすると、2番目のtableViewControllerがロードするデータを決定する変数を2番目のtableViewControllerに渡します。どうすればいい?変数をtableviewから2番目のtableviewに渡す方法は?

私はXcodeの4.2を使用しています、ストーリーボード はここのコードです:「ヘッド」私はその二テーブルビューショーのデータに応じて、第二テーブルビューに渡す必要があり数を取得

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *__strong)indexPath 
{ 
    // Navigation logic may go here. Create and push another view controller. 

    TestTable *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"TestTable"]; 

    [self.navigationController pushViewController:detail animated: YES]; 

    detail.num = [NSString stringWithFormat:[heads objectAtIndex:indexPath.row]]; 

で。最後の行が "num"に "heads"をロードしているかどうかはわかりません。私のアプローチは正しいのですか? ヘルプ/リファレンスは評価されています

+0

ヘッドは何ですか?あなたの質問をより明確にする。 – Sarah

+0

headsは可変配列(単純に整数を含む)です。 – jason

+0

cell.textLabel.text = [heads objectAtIndex:indexPath.row]; – jason

答えて

1
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *__strong)indexPath { 
    // Navigation logic may go here. Create and push another view controller. 

    TestTable *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"TestTable"]; 

    detail.num = [[NSString stringWithFormat:[heads objectAtIndex:indexPath.row]] intValue]; 
    [self.navigationController pushViewController:detail animated: YES]; 
} 
+0

私のコードでは間違いがありました。プッシュコマンドの前に変数を渡さなければなりませんでした。また、新しいコードで使用されているintValueをメモしてください。 – jason

2

TestTableに過去の変数のプロパティを指定します。

あなたdidSelectRowAtIndexPathで
@property (retain, nonatomic) NSNumber *myNumber; 

はそれが好き割り当てる:次のように

TestTable *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"TestTable"]; 

// this assumes that you only have one section in your table 
detail.myNumber = [NSNumber numberWithDouble:[heads objectAtIndex:indexPath.row]]; 

[self.navigationController pushViewController:detail animated: YES]; 

detail.num = [NSString stringWithFormat:[heads objectAtIndex:indexPath.row]]; 

を2番目のテーブルののViewControllerでは、あなたが番号を使用することができます。

double d = [self.myNumber doubleValue]; 
+0

ok.how次のテーブルビューで取得できますか? – jason

+0

使用法を対象テーブルに含めるための回答を編集しました。 – Marko

関連する問題