基于故事板的应用程序中视图控制器之间的任意通信

发布于 2025-01-01 01:27:07 字数 437 浏览 1 评论 0原文

给定一个基于故事板的应用程序,任何一个视图控制器如何调用任何其他视图控制器的方法(以访问其封装的信息)而不执行prepareForSegue方法(该方法仅通过segue参数传递目标vc的引用)?我知道信息可以从父 vc 传递到子 vc,但我需要一种更通用的方法,让任何 vc 可以在任何时间点将信息传递给任何其他 vc。

换句话说,在 vc A 中,我想调用 vc B 中的方法(未在 A 的任何 segue 中命名),但不明白如何从 A 获取对 B 的引用。 创建一个新的 segue它将创建一个新的 vc;不是我想做的。

我认为有某种方法可以通过故事板或其关联的命名 segue 函数来追踪任何其他 vc 的引用,但一直在努力寻找一种方法来做到这一点。在我通常生活的深度嵌入式世界中,我只需创建一个单例类,以便可以从任何地方实例化它,但这对于视图控制器的情况似乎不合适(如果可能的话)。

任何帮助将不胜感激。

Given a storyboard-based application, how can any one view controller invoke the methods of any other (to gain access to its encapsulated information) without executing the prepareForSegue method (which only passes the destination vc's reference via the segue argument)? I understand that information can be passed from a parent vc to a child vc but I need a more generic approach for any vc to pass information to any other at any point in time.

Put another way, while in vc A, I would like to invoke a method in vc B (that is not named in any segue from A), but don't understand how to get a reference to B from A. Creating a new segue to it would create a new vc; not what I want to do.

I would think there is some way via the storyboard or its associated named segue functions to track down a reference to any vc from any other but have struggled to find a way to do so. In the deeply embedded world I normally live in, I would simply create a singleton class so it could be instantiated from anywhere but that seems inappropriate (if even possible) here for the case of a view controller.

Any help would be greatly appreciated.

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

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

发布评论

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

评论(1

久光 2025-01-08 01:27:07

我建议您不要尝试通过故事板 segue 函数来执行此操作。尝试使用 NSNotifications。您必须自己编写这些代码(即不在 Storyboard 中),但它们将允许您根据您在通知中传递的信息来广播 VC-B 调用方法的“请求”。 VC-B 必须监听(观察),但这是一种非常好的解耦类的方法,但仍然以任意方式进行通信。

I would suggest you not try to do this via storyboard segue functions. Try using NSNotifications. You'll have to code these yourself (i.e. not in Storyboard), but they will allow you to broadcast a "request" that VC-B invoke a method, based on what information you pass it in the notification. VC-B has to be listening for it (observing), but this is a very nice way of de-coupling your classes, but still communicating in an arbitrary way.

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