gpt4 book ai didi

ios - 在 View Controller 之间传递数据 DidSelectRowsAtIndexPath Storyboard

转载 作者:可可西里 更新时间:2023-11-01 06:22:04 24 4
gpt4 key购买 nike

在一个简单的测试应用程序中,我尝试将名为“thisArray”的数组对象从 MasterViewController 传递到 DetailViewController 中名为“passedData”的字符串。我正在使用 Storyboard ,并且 UIViewController 嵌入在导航 Controller 中。使用 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,我是否需要删除 Storyboard 中 UIViewController 之间的 segue 连接?

3) 如果 View Controller 之间真的没有 segue 连接怎么办,我如何仍然使用 didSelectRowAtIndexPath 在它们之间传递数据?

谢谢!

更新:根据我收到的回答和评论,我写了以下内容:

首先我移除了 Controller 之间的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];
}

但它因以下错误而崩溃:

*** Terminating app due to uncaught exception NSInvalidArgumentException, reason: 'Could not find a storyboard named DetailViewController in bundle

最佳答案

您的第二次尝试几乎是正确的。唯一不太正确的是从 Storyboard 中实例化 View Controller 的方式:您使用 initWithNibNamed:,这是用于 NIB 而不是 Storyboard 中的方法。对于 Storyboard,您需要:

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

关于ios - 在 View Controller 之间传递数据 DidSelectRowsAtIndexPath Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16380092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com