我有一个带有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]];
在“ heads”中,我得到一个数字,该数字必须根据第二个表视图显示的数据传递给第二个表视图。不知道最后一行是否在“ num”中加载“ heads”。我的方法正确吗?
任何帮助/参考表示赞赏
最佳答案
给您的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]];
在第二张表的视图控制器中,可以使用以下数字:
double d = [self.myNumber doubleValue];