4

在一个简单的测试应用程序中,我尝试将名为“thisArray”的数组对象MasterViewControllerDetailViewController. 我正在使用 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];
    }
}

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

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

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

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

但它也没有奏效。

问题

1)如何正确写didSelectRowsAtIndexPath替换prepareForSegue

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

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

但它因以下错误而崩溃:

*** 由于未捕获的异常而终止应用程序NSInvalidArgumentException,原因:'找不到DetailViewController包中命名的情节提要

4

1 回答 1

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

有关详细信息,请参阅此问题

进行替换后,您的代码应该可以按预期工作。

编辑: : 这是您的方法的外观:

- (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];
}
于 2013-05-05T00:07:16.093 回答