prepareForSegue 和代表

发布于 2024-12-22 04:40:37 字数 910 浏览 3 评论 0原文

我有一个有两个 segue 的应用程序。在其中一个 Segue 中,当前视图控制器成为委托,而另一个则不是。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"MoreOptions"]) {
        UINavigationController *navigationController = segue.destinationViewController;
        MoreOptionsViewController *controller = (MoreOptionsViewController *)navigationController.topViewController;
        controller.delegate = self;
    } else if ([segue.identifier isEqualToString:@"FullStoryView"]) {
        SingleStoryViewController *detailViewController = segue.destinationViewController;
        detailViewController.urlObject = sender;
    }
}

所有这些都工作正常,但我想尝试更好地理解代码。我不明白的是,我必须通过从 navigationController.topViewController 获取对 MoreOptionsViewController 的引用,而不是像我在第二个 if 条件中那样简单地从 segue.destinationViewController 获取它。是因为我将当前视图控制器(自身)设置为委托吗?再说一遍,我并不是想解决问题,只是想更好地了解正在发生的事情。

I have an application with two segues. In one of the segues, the current view controller becomes a delegate and the other does not.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"MoreOptions"]) {
        UINavigationController *navigationController = segue.destinationViewController;
        MoreOptionsViewController *controller = (MoreOptionsViewController *)navigationController.topViewController;
        controller.delegate = self;
    } else if ([segue.identifier isEqualToString:@"FullStoryView"]) {
        SingleStoryViewController *detailViewController = segue.destinationViewController;
        detailViewController.urlObject = sender;
    }
}

All of this is working fine, but I would like to try and understand the code better. What I don't understand is that I have to get a reference to the MoreOptionsViewController by grabbing it from navigationController.topViewController rather than simply getting it from segue.destinationViewController like I do in the second if condition. Is it because I'm setting the current view controller (self) as the delegate? Again, I'm not trying to solve a problem, just trying to get a better understanding of what's going on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赢得她心 2024-12-29 04:40:37

看看你的故事板,就会明白为什么会出现这种情况。您已将 MoreOptionsViewController 嵌入到 UINavigationController 中,并将 Segue 连接到导航控制器,从而使其成为 destinationViewController。这是相当常见的。

Take a look at your storyboard and it should be evident why this is the case. You have embedded MoreOptionsViewController in a UINavigationController and connected a segue to the navigation controller, thus making it the destinationViewController. This is fairly common.

情释 2024-12-29 04:40:37

该代表在很大程度上与您的问题无关。

您的第一个 Segue 的目的地是导航控制器,其中包含您真正感兴趣的视图控制器。因此,要到达该视图,您需要通过导航控制器,因为它没有任何属性你对设置感兴趣。

您的第二个 Segue 直接进入单个视图控制器,因此您可以直接访问它。

The delegate is largely irrelevant in the context of your question.

Your first segue's destination is a navigation controller, which contains the view controller you are really interested in. Therefore to get to that view, you need to go through the navigation controller since that won't have any properties you are interested in setting.

Your second segue goes directly to a single view controller, so you can access it directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文