在视图控制器之间传递数据DidSelectRowsAtIndexPath故事板

AJ1*_*112 3 objective-c storyboard uitableview didselectrowatindexpath ios

在一个简单的测试应用程序中,我试图将一个名为"thisArray"的数组对象从MasterViewController一个名为"passedData"的字符串传递给DetailViewController.我正在使用Storyboard,UIViewController它们嵌入在导航控制器中.使用该prepareForSegue方法,我成功传递了以下数据UIViewController:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"pushData"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        DetailViewController *destViewController = segue.destinationViewController;
        destViewController.passedData = [thisArray objectAtIndex:indexPath.row];
    }
}
Run Code Online (Sandbox Code Playgroud)

现在由于某些原因我想用didSelectRowsAtIndexPath而不是prepareForSegue.我用过这个:

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableString *object = thisArray[indexPath.row];
    detailViewController.passedData = object;   
}
Run Code Online (Sandbox Code Playgroud)

但它没有用.我使用了以下内容:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

    NSMutableString *object = thisArray[indexPath.row];
    detailViewController.passedData = object;
    [self.navigationController pushViewController:detailViewController animated:YES];  
}
Run Code Online (Sandbox Code Playgroud)

但它也没有用.

问题:

1)如何正确写入didSelectRowsAtIndexPath替换prepareForSegue

2)如果我使用didSelectRowsAtIndexPath,我是否需要删除UIViewController故事板中的segue连接?

3)如果视图控制器之间确实没有segue连接,我怎样才能使用它们之间传递数据didSelectRowAtIndexPath呢?

谢谢!

更新:根据我收到的答案和评论,我写了以下内容:

首先我删除了控制器之间的segue连接,设置了StoryBoard id DetailViewController,类的名称也是DetailViewController.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIStoryboard*  sb = [UIStoryboard storyboardWithName:@"DetailViewController"
                                                  bundle:nil];
    UIViewController* vc = [sb instantiateViewControllerWithIdentifier:@"DetailViewController"];

    NSMutableString *object = thisArray[indexPath.row];
    detailViewController.passedData = object;
    [self.navigationController pushViewController:detailViewController animated:YES];
}
Run Code Online (Sandbox Code Playgroud)

但它因以下错误而崩溃:

***由于未捕获的异常而终止应用程序NSInvalidArgumentException,原因是:'无法找到以DetailViewController捆绑包命名的故事板

das*_*ght 10

你的第二次尝试几乎是对的.唯一不太正确的是从故事板中实例化视图控制器的方式:您使用的initWithNibNamed:,这是您使用NIB而不是故事板.对于故事板,您需要:

UIStoryboard*  sb = [UIStoryboard storyboardWithName:@"theStoryboardId"
                                          bundle:nil];
// If this code is inside a method called by a controller that is itself instantiated
// from a storyboard, you can replace the above line with this.storyboard
UIViewController* detailViewController = [sb instantiateViewControllerWithIdentifier:@"DetailViewController"];
Run Code Online (Sandbox Code Playgroud)

有关详情,请参阅此问题.

完成替换后,您的代码应按预期工作.

编辑::这是你的方法的样子:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController* detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
    NSMutableString *object = thisArray[indexPath.row];
    detailViewController.passedData = object;
    [self.navigationController pushViewController:detailViewController animated:YES];
}
Run Code Online (Sandbox Code Playgroud)

  • 显然,如果转到另一个故事板,该语法很有用.但是如果是相同的故事板,只需使用`self.storyboard`,例如,`UIViewController*vc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"]; (2认同)
  • @gotnull这并不奇怪,考虑到Swift的问题已经过了将近一年.如果您正在寻找Swift的答案,请考虑另外提问. (2认同)