在视图控制器之间传递数据DidSelectRowsAtIndexPath Storyboards
在一个简单的测试应用程序中,我试图将名为“thisArray”的数组对象从MasterViewController
传递到DetailViewController
中名为“passedData”的字符串。我正在使用故事板,并且UIViewController
嵌入在导航控制器中。使用prepareForSegue
方法,我顺利地通过了数据UIViewController
之间:在视图控制器之间传递数据DidSelectRowsAtIndexPath Storyboards
- (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
,做我需要删除的故事板UIViewController
之间的连接赛格瑞?
3)如果视图控制器之间真的没有segue连接怎么办?我怎么能仍然使用didSelectRowAtIndexPath
来传递它们之间的数据?
谢谢!
更新:根据答案,我收到的意见,我已经写了以下内容:
首先,我已经去掉了控制器之间的SEGUE连接,设置故事板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];
}
但它越来越与以下错误崩溃:
*** Terminating app due to uncaught exception
NSInvalidArgumentException
, reason: 'Could not find a storyboard namedDetailViewController
in bundle
你的第二次尝试是接近直角的。唯一不正确的是你从故事板实例化视图控制器的方式:你使用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"];
有关详细信息,请参阅this question。
一旦你进行了替换,你的代码应该按预期工作。
编辑::这里是如何你的方法看起来:下面
- (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];
}
显然,如果转到另一个故事板,该语法非常有用。但如果相同的故事板,只需使用'self.storyboard',例如'UIViewController * vc = [self.storyboard instantiateViewControllerWithIdentifier:@“DetailViewController”];' – Rob 2013-05-05 00:12:40
我应该只是删除这些视图控制器之间的segue连接?以及如何使用“vc”传递数据? – AJ112 2013-05-05 00:14:09
@ AJ112是的,你需要删除赛格。你的方法的其余部分不会改变,从使用'detailViewController.passedData = object;'开始传递数据。 – dasblinkenlight 2013-05-05 00:15:52
见dasblinkenlight的编辑,并更改更新的代码给他。它应该照顾你正在尝试做的事情!:) – lnafziger 2013-05-05 02:21:19
是的,它完成了。谢谢! – AJ112 2013-05-05 04:06:53