gpt4 book ai didi

ios - 将信息从 UINavigationController 传递到嵌入式 UIViewController

转载 作者:行者123 更新时间:2023-11-28 22:16:50 25 4
gpt4 key购买 nike

概要

由于我没有让我的应用按预期方式传递数据,我会问以下问题:

  • 一般来说,如何将数据从导航 Controller 传递到它的嵌入式 View Controller ?
  • 具体来说,prepareForSegue导航 Controller 中的方法曾经被调用过吗?如果是,怎么做?

设置

我有以下内容:

  • 一个名为 User View Controller 的 View
    链接到一个自定义类 JRMUserViewController(它扩展了 UIViewController).
    这个 View 嵌入在:
  • 导航 View ,标题为Navigation To User View
    链接到自定义类 JRMNavigationToUserViewController(扩展 UINavigationController ).

    重要说明:
    首先,我没有在导航 Controller 中嵌入用户 View Controller 。导航 Controller 及其链接类不存在。

  • 一个名为 Issue Table View Controller 的 TableView
    链接到一个自定义类 JRMIssuesTableViewController(它扩展了 UITableViewController ).
  • 一个按钮链接到一个模态转场,该转场转到用户 View 导航
    转场由文本“IssuesToUserView”。

StoryBoard Setup

预期功能

我的想法是,当我在Issue Table View Controller 上时,我可以点击按钮,它将带我到User View Controller,在那里我将看到当前用户的头像,以及带有当前用户全名的标签。

为了实现这一点(在我将 User View Controller 嵌入导航 Controller 之前),我将数据从 JRMIssuesTableViewController 传递到 JRMUserViewController在前者的 prepareForSegue 方法中:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSLog(@"\nSEGUE IDENTIFIER:\n%@", [segue identifier]);
if([[segue identifier] isEqualToString:@"IssuesToUserView"]) {
JRMUserViewController *vc = [segue destinationViewController];
vc.currentUser = self.currentUser;
NSURL *url = [[NSURL alloc] initWithString:[vc.currentUser.avatarUrls objectForKey:@"48x48"]];
NSLog(@"\nURL:\n%@", url);
NSData *data = [NSData dataWithContentsOfURL:url];
[vc.view addSubview:vc.avatar];
[vc.avatar setImage:[[UIImage alloc] initWithData:data]];
vc.userLabel.text = vc.currentUser.displayName;
}
}

它起作用了。单击第一个 View 上的按钮将我带到第二个 View ,其中显示了当前用户的信息:

Issue Table View

User View -- Working

意外行为

在此之后,出于必要,我不得不将用户 View Controller 嵌入到我之前提到的导航 Controller 中。现在当然,在这样做之后,我在运行时遇到了未捕获的异常,因为在我覆盖的 prepareForSegue 中,我将消息发送到导航 Controller 而不是用户 View Controller 。为了解决这个问题,我扩展了 UINavigationController,将其命名为 JRMNavigationToUserViewController,并将导航 View 链接到它,正如我在设置中提到的那样。

然后,假设 UINavigationController 会在转到其嵌入式 View 之前调用它自己的 prepareForSegue,我覆盖了 中的 prepareForSegue 方法>JRMNavigationToUserViewController:

- (id)passValuesToDesitinationOfSegue:(UIStoryboardSegue *)segue {
NSLog(@"Calling [JRMNavigationToUserViewController passValuesToDestinationOfSegue]");
JRMUserViewController *destination = [segue destinationViewController];
destination.server = self.server;
destination.currentUser = self.currentUser;
destination.avatar = self.avatar;
[destination.view addSubview:destination.avatar];
destination.userLabel = self.userLabel;
return destination;
}

#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSLog(@"Calling [JRMNavigationToUserViewController prepareForSegue]");
[self passValuesToDesitinationOfSegue:segue];
}

这一次,当我点击按钮时,User View Controller 没有收到数据。它看起来像这样:

User View -- Not Working

显然,从未调用 JRMNavigationToUserViewController 中的 prepareForSegue 方法,因为它会记录消息:

Calling [JRMNavigationToUserViewController prepareForSegue]
Calling [JRMNavigationToUserViewController passValuesToDestinationOfSegue]

我无法在控制台的任何地方找到打印的这些行。

总结

这里又是我的问题:

  • 一般来说,如何将数据从导航 Controller 传递到它的嵌入式 View Controller ?
  • 具体来说,prepareForSegue导航 Controller 中的方法曾经被调用过吗?如果是,怎么做?

最佳答案

您在这里滥用了 UINavigationController。看看你的 Storyboard:你有一个表 Controller ,它以某种方式呈现一个导航 Controller ,而导航 Controller 又有一个用户 Controller 作为它的根 Controller 。我想你想要的是让导航 Controller 包含表 Controller ,并在表上点击触发向用户 View Controller 的推送 segue。所以,导航 Controller 应该在左边,表格 Controller 在中间,用户 Controller 在右边。

然后,表格 Controller 中的 -prepareForSegue:sender: 方法将被编写为查看选定的单元格并设置用户 Controller 的相应属性(这是 segue 的目的地) .

关于ios - 将信息从 UINavigationController 传递到嵌入式 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21442353/

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