如何将变量从tableview传递到第二个tableview?

时间:2011-12-19 09:46:19

标签: iphone xcode4.2 tableview storyboard

我有一个带x行的TableViewController。当用户点击一行时,我想向第二个tableViewController传递一个变量,该变量将确定第二个tableViewController应该加载哪些数据。我该怎么做?

我正在使用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]];

在“head”中,我得到一个数字,我必须传递到第二个tableview,具体取决于哪个第二个tableview显示数据。不知道最后一行是否在“num”中加载“head”。我的方法是否正确? 任何帮助/参考表示赞赏

2 个答案:

答案 0 :(得分:2)

为您的TestTable提供您想要过去的变量的属性,例如

@property (retain, nonatomic) NSNumber *myNumber;
你的didSelectRowAtIndexPath中的

分配如下:

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]];

在第二个表的viewcontroller中,您可以使用以下数字:

double d = [self.myNumber doubleValue];

答案 1 :(得分: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];
}