在视图控制器之间传递数据DidSelectRowsAtIndexPath故事板 [英] Passing Data Between View Controllers DidSelectRowsAtIndexPath Storyboards
问题描述
在一个简单的测试应用程序中,我试图将一个名为thisArray的数组对象从 MasterViewController
传递给<$ c中名为passedData的字符串$ C> DetailViewController 。我正在使用Storyboard,并且 UIViewController
嵌入在导航控制器中。使用 prepareForSegue
方法,我成功传递了 UIViewController
之间的数据:
In a simple test app, i have tried to pass an array object named "thisArray" from the MasterViewController
to the a string named "passedData" in the DetailViewController
. I am using Storyboards and the UIViewController
are embedded inside the navigation controller. Using the prepareForSegue
method, i successfully passed the data between the 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
。我用过这个:
Now for some reasons i want to use didSelectRowsAtIndexPath
instead of prepareForSegue
. I have used this:
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableString *object = thisArray[indexPath.row];
detailViewController.passedData = object;
}
但它不起作用。我使用了以下内容:
But it didn't work. I have used the following:
- (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
之间的segue连接?
2) If i use didSelectRowsAtIndexPath
, do i need to remove the segue connection between the UIViewController
in the Storyboard?
3)如果视图控制器之间确实没有segue连接怎么办,我怎样才能使用 didSelectRowAtIndexPath <在它们之间传递数据/ code>?
3) What if there is really no segue connection between the view controllers, how can i still pass the data between them using didSelectRowAtIndexPath
?
谢谢!
更新:根据我收到的答案和评论,我写了以下内容:
UPDATE: Based on the answer and the comments i received, i have wrote the following:
首先我删除了控制器之间的segue连接,设置了StoryBoard id DetailViewController
,类的名称也是 DetailViewController
。
First i have removed the segue connection between the controllers, set the StoryBoard id DetailViewController
, the name of the class is also 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
in bundle
*** Terminating app due to uncaught exception
NSInvalidArgumentException
, reason: 'Could not find a storyboard namedDetailViewController
in bundle
推荐答案
你的第二次尝试几乎是正确的。唯一不太正确的是从故事板中实例化视图控制器的方式:使用 initWithNibNamed:
,这是您使用NIB而不是故事板。对于故事板,你需要这个:
Your second attempt is nearly right. The only thing that's not quite correct is the way you instantiate a view controller from a storyboard: you use initWithNibNamed:
, which is what you use with NIBs but not with storyboards. For storyboards, you need this:
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"];
请参阅这个问题了解详情。
一旦你做了替换,你的代码应该按预期工作。
Once you make the replacement, your code should work as expected.
编辑::以下是您的方法的外观:
EDIT :: Here is how your method would look:
- (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];
}
这篇关于在视图控制器之间传递数据DidSelectRowsAtIndexPath故事板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!