视图控制器之间反向通信的最佳方法是什么?

发布于 2024-09-27 11:43:06 字数 736 浏览 4 评论 0原文

我一直在思考这个问题,并阅读了另一个 stackoverflow 问题 关于视图控制器之间通信的最佳推荐方式。然而,问题/答案似乎并没有解决反向行为的最佳方法。

即要将数据从ParentController 传递到其ModalController,我们可以像initWithDataToProcess: 一样初始化ModalController。

但如果我们想要相反呢?我如何通知先前的控制者有关新数据的信息?

例如,用户点击 ParentController 上的“新人”按钮。我启动一个新的 ModalController 并通过 presentModalViewController: 向用户呈现一个人员编辑器视图。用户单击“完成”即可添加新人。我 dismissModalViewController: 并且 UI 返回到 ParentController 的视图。

在单例对象(应用程序委托或其他)中使用全局字段引用是不好的。 委托(通过正式协议)和通知(通过 NSNotificationCenter)似乎太过分了。有什么建议吗?

I've been thinking about this and have read through another stackoverflow question regarding the best recommended way to communicate between view controllers. However, the question/answer for that doesn't seem to address the best approach for the reverse behavior.

i.e. to pass data from ParentController to its ModalController, we could initialize ModalController like initWithDataToProcess:.

But what if we want to do the reverse? How would I notify the previous controller about a new data?

e.g. User clicks on 'new person' button on the ParentController. I initiate a new ModalController and present the user with a person editor view via presentModalViewController:. User clicks on 'done' to add a new person. I dismissModalViewController: and UI returns to the ParentController's view.

Using a global field reference in a singleton object (app delegate or other) is bad. delegation (via formal protocol) and notifications (via NSNotificationCenter) seems overkill. Any suggestions?

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

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

发布评论

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

评论(2

从来不烧饼 2024-10-04 11:43:07

使用通知通常更干净。只需像这样添加您的观察者......

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(somethingHappened:) name:@"MyNotification" object:nil];

并且在代码中的其他地方您可以在需要时发布通知。

[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:self];

在这个例子中,我传递的是 self,但你可以传递你真正想要的任何对象,它将被提供给你的 SomethingHappened: 函数

重要的是保持 @"MyNotification" 非常具有描述性和独特性。将项目名称添加到开头是保持项目独特的好方法......例如。 @“ProjAXViewHasGotData”

It is generally cleaner to use notifications. Just add your observer like this....

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(somethingHappened:) name:@"MyNotification" object:nil];

and elsewhere in your code you'd post the notification whenever you need to.

[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:self];

In the example I'm passing self but you can pass any object you wish really and it will be fed to your somethingHappened: function

The important thing is to keep the @"MyNotification" very descriptive and unique. Adding your project name to the beginning is a good way to keep things unique...eg. @"ProjAXViewHasGotData"

小嗷兮 2024-10-04 11:43:07

委托几乎是您可以做的最低限度的事情。如果您认为为此声明一个新协议太麻烦,只需传入父视图控制器并让模态控制器调用其上的方法即可。

A delegate is pretty much the minimum you can do. If you think it is too much of a hassle to declare a new protocol for this, just pass in the parent view controller and have the modal one call a method on it.

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