iphone - 如何将变量从 TableView 传递到第二个 TableView ?

标签 iphone xcode4.2 tableview storyboard

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

我正在使用 Xcode 4.2, Storyboard 这是代码:

- (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”中,我得到一个数字,我必须将其传递给第二个表格 View ,具体取决于哪个第二个表格 View 显示数据。不知道最后一行是否在“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]];

在第二个表的 View Controller 中,您可以按如下方式使用该数字:

double d = [self.myNumber doubleValue];

关于iphone - 如何将变量从 TableView 传递到第二个 TableView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8559488/

相关文章:

iphone - 在哪里自定义 UINavigationController 后退按钮?

iphone - 硬件加速图像比较/搜索?

iphone - 设备上的 UISlider setMinimumTrackTintColor 崩溃

iphone - 从 plist 数组加载 MapView 注释时出现问题 - Xcode 4.2

swift - 滚动后 Tableview 部分展开折叠更新

iphone - 如何访问 ios 邮件应用程序右上角使用的箭头按钮?

ios - 为什么将 UITableViewCell 连接到 UInavigationcontroller 时没有任何反应

ios - Xcode 4.2 不输出退出代码?

objective-c - 等到 UITableView 完成 reloadData

Swift/Firebase 从 TableView 更改值