向将要生成的对象发送消息

发布于 2024-12-06 09:55:43 字数 1036 浏览 0 评论 0原文

我所处的情况是,我需要发送一个对象和一个名为 UIViewController 的方法,将来可能会或可能不会实例化。为了让事情变得更有趣,在推送两个或三个额外的 UIViewController 后,UIViewController 将被推送到 UINavigationController。

最好的方法是什么?一种丑陋的方法是通过中间的控制器中继消息包(一个对象和一个方法名称)。那太丑了。

简单地子类化 UINavigationController 并悬挂对象和方法名称?或许。那么我如何知道何时清除该消息包呢?

不过,在这种情况下 NSInspiration 看起来确实很有前途。有什么想法吗?

不不不单例,或者外部结构请。


编辑:感谢您的评论。

@Akshay,我已经考虑过这一点。问题是如果你想将消息包(一个对象和一个方法名)发送到特定navigationController下的特定对象怎么办? 此外,如果目标对象未实例化,则需要清除单例以供以后使用。但你怎么知道什么时候该清理呢?使用单例只是一团糟。

@Praveen-K,NSNotifcation 仅适用于那些已经存在的人。我说我想发送一个消息包到一个将要生成的对象。

@PragmaOnce,我和另外两个队友现在正在设计架构。希望这能解释。请不要使用数据中心单例。

@Sedate 外星人,没错!

@9dan,感谢您的意见。事实上,我们有 NSClassFromString、NSSelectorFromString 和 PerformSelector,我们可以实例化任何对象并发送消息来执行任意数量对象的任何方法。

问题是如何优雅地跨多个对象发送该消息,在我的例子中是 UIViewController?假设你在一个 UIViewController 上,并且你知道你会在当前的 UIViewController 之上推送更多的 UIViewController。之后,您可能会也可能不会实例化特定类的对象并使用对象执行方法。我们如何在不使用单例的情况下做到这一点?一定有一种方法可以完美地处理这个问题。我认为 NSInspiration 具有这种能力。

I am in a situation in which I need to send an object and a method name a UIViewController that might or might not be instantiated in the future. To make the matter a bit more interesting, the UIViewController will be pushed to UINavigationController after two or three additional UIViewController being pushed.

What would be the best way to do this? An ugly approach would be just relay the message package (an object and a method name) via the controllers in-between. That's ugly.

Simply subclass UINavigationController and dangle the object and method name? Maybe. Then how do I know when to clear up that message pack?

NSInvocation looks really promising in this case though. Any thought?

No no no singleton, or extern struct please.


Edit: Thanks for the comments.

@Akshay, I have thought about that. Question is what if you want to send the message package (an object and a method name) to a specific object under a particular navigationController?
Further, if the target object is not instantiated, you would need to clear your singleton for later use. But then how do you know when to clear up? Using singleton is just a mess.

@Praveen-K, NSNotifcation only works for those who already exist. I said I want to send a message package to a would-be-generated object.

@PragmaOnce, I and two other team mates are designing the architecture now. Hope this explains. Please, no data center singleton.

@Sedate Alien, Exactly!

@9dan, Thanks for your input. In fact, we have NSClassFromString, NSSelectorFromString, and performSelector that we can instantiate any object and send a message to execute any method with any number of objects.

The question is how to elegantly send that message across multiple objects, in my case UIViewController? Say you are at a UIViewController, and you know you would push few more UIViewControllers on top of current one. After that, you might or might not instantiate an object of a specific class and execute a method with an object. How do we do this w/o using singleton? There must be a way to handle this beautifully. I think NSInvocation has that capacity.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文