在视图控制器之间传递数据DidSelectRowsAtIndexPath Storyboards
在一个简单的testing应用程序中,我试图从MasterViewController
传递名为“thisArray”的数组对象到DetailViewController
名为“passedData”的string。 我正在使用故事板和UIViewController
embedded导航控制器。 使用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
来replaceprepareForSegue
?
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]; }
但它得到坠毁与以下错误:
***终止应用程序由于未捕获的exception
NSInvalidArgumentException
,原因:'无法find一个名为DetailViewController
的故事板捆绑
你的第二次尝试几乎是正确的。 唯一不太正确的是你从故事板实例化视图控制器的方式:你使用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"];
看到这个问题的细节。
一旦你做了replace,你的代码应该按预期工作。
编辑:这是你的方法看起来如何:
- (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]; }