简体   繁体   English

如何使用 Singleton 来允许各种视图控制器之间的通信?

[英]How can a Singleton be used to allow communication between various view controllers?

I have been suggested to use the Singleton model at:有人建议我在以下位置使用 Singleton model:

http://www.galloway.me.uk/tutorials/singleton-classes/ http://www.galloway.me.uk/tutorials/singleton-classes/

Some of the questions I have are:我的一些问题是:

where do I declare the singleton?我在哪里申报 singleton? Do I release it anywhere or does it terminate with the application?我是在任何地方释放它还是随着应用程序终止? Any tips in terms of the Singletons in apps?关于应用程序中的单例的任何提示?

Thanks谢谢

It is just a class having only one object.它只是一个 class,只有一个 object。 That's why its called singleton.这就是为什么它被称为 singleton。

We don't release it.我们不发布它。 It gets when the app terminates.它在应用程序终止时获取。

Singleton object can be accessed from any where in your application. Singleton object 可以从应用程序的任何位置访问。

You initialize it at the first time get call (via static method).您在第一次调用时初始化它(通过 static 方法)。 Then the instance will remain in the memory.然后该实例将保留在 memory 中。

You releases it when the application terminated.您在应用程序终止时释放它。

A Singleton is a construct allowing a class to be instantiated only once. Singleton 是一个允许 class 仅被实例化一次的结构。

I am not an expert in iPhone programming, but Singleton doesn't mean the object is accessible from anywhere in your application imho.我不是 iPhone 编程方面的专家,但 Singleton 并不意味着 object 可以从您的应用程序中的任何位置访问,恕我直言。 You can still manage it's accessiblity through visibility.您仍然可以通过可见性管理它的可访问性。 And you definitely should.你绝对应该。

Overuse of the Singleton can lead to tight coupling of your classes to a static instance (your Singleton) and that is usually what you are trying to avoid in object oriented programming.过度使用 Singleton 可能导致您的类与 static 实例(您的单例)紧密耦合,这通常是您在面向 object 的编程中试图避免的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM