在一个简单的测试应用程序中,我试图将名为“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
见dasblinkenlight的编辑,并更改更新的代码给他。它应该照顾你正在尝试做的事情!:) – lnafziger 2013-05-05 02:21:19
是的,它完成了。谢谢! – AJ112 2013-05-05 04:06:53