iOS Objective-C中Singleton类和AppDelegate类之间的关系

时间:2023-01-16 12:15:03

I have a variable declared in AppDelegate class(.h file) whose value gets changed from multiple ViewController classes.Also,single application-wide instance for my AppDelegate class is shared throughout my application as follows :

我有一个在AppDelegate类(.h文件)中声明的变量,其值从多个ViewController类中更改。另外,我的AppDelegate类的单个应用程序范围的实例在我的应用程序*享,如下所示:

AppDelegate *AppD = (AppDelegate *)[[UIApplication sharedApplication] delegate];

AppDelegate * AppD =(AppDelegate *)[[UIApplication sharedApplication] delegate];

As I could access this variable declared in AppDelegate from any ViewController class, is AppDelegate class as an example of Singleton class in this scenario?

因为我可以从任何ViewController类访问AppDelegate中声明的这个变量,AppDelegate类在这种情况下是Singleton类的一个例子吗?

Can anyone help to site out the usage of singleton class with real-life example ?

任何人都可以通过现实生活中的例子来帮助解决单例类的用法吗?

3 个解决方案

#1


3  

AppDelegate is however a singleton class but you show only use it for declaring things that applies globally in your application.
For ex: If you want to change the color of navigation bar in your entire application you can use app delegate and set the color of navigation bar. Also app delegate is an object that handles different state transition in your app.
So if you want to create a variable that can be changed from multiple View controllers you should create a singleton class and declare that variable in that class.

然而,AppDelegate是一个单例类,但您只显示它用于声明应用程序中全局适用的内容。例如:如果要更改整个应用程序中导航栏的颜色,可以使用app delegate并设置导航栏的颜色。 app delegate也是一个处理应用程序中不同状态转换的对象。因此,如果要创建可以从多个View控制器更改的变量,则应创建单个类并在该类中声明该变量。

#2


2  

The app delegate is not supposed to be a repository for all kinds of global variables. The app delegate is supposed to be used for things that affect the whole of the application, like launch / app termination, entering the background and returning from the background, that kind of thing.

应用程序委托不应该是各种全局变量的存储库。应用程序委托应该用于影响整个应用程序的事情,例如启动/应用程序终止,进入后台并从后台返回等等。

If there is state that is shared by multiple view controllers, that should exist only once, but doesn't affect the application as a whole, then you could consider creating a singleton for that state. Then again, global state that is just an artefact of how you write your code should be avoided.

如果有多个视图控制器共享的状态,它应该只存在一次,但不会影响整个应用程序,那么您可以考虑为该状态创建一个单例。然后,应该避免全局状态,这只是你编写代码的一个人工制品。

#3


2  

AppDelegate can be used just like singleton, but I don't recommend it. It's like you can put all your classes declarations and definitions in a class.h and a class.m file. Simply import the class.h file can invoke all classes. But it will be very inconvenient to read, understand and manage.

AppDelegate可以像单身一样使用,但我不推荐它。这就像你可以将所有类声明和定义放在class.h和class.m文件中。只需导入class.h文件即可调用所有类。但阅读,理解和管理会非常不方便。

AppDelegate is mainly used for all kinds of app itself event, through UIApplicationDelegate method. Do not recommend deal with too much logic about global data here. Such as classes named XXManager, XXService, PublicData, is proposed to manage all kinds of singleton data.

AppDelegate主要用于各种app本身的事件,通过UIApplicationDelegate方法。不建议在这里处理关于全局数据的过多逻辑。建议使用名为XXManager,XXService,PublicData的类来管理各种单例数据。

#1


3  

AppDelegate is however a singleton class but you show only use it for declaring things that applies globally in your application.
For ex: If you want to change the color of navigation bar in your entire application you can use app delegate and set the color of navigation bar. Also app delegate is an object that handles different state transition in your app.
So if you want to create a variable that can be changed from multiple View controllers you should create a singleton class and declare that variable in that class.

然而,AppDelegate是一个单例类,但您只显示它用于声明应用程序中全局适用的内容。例如:如果要更改整个应用程序中导航栏的颜色,可以使用app delegate并设置导航栏的颜色。 app delegate也是一个处理应用程序中不同状态转换的对象。因此,如果要创建可以从多个View控制器更改的变量,则应创建单个类并在该类中声明该变量。

#2


2  

The app delegate is not supposed to be a repository for all kinds of global variables. The app delegate is supposed to be used for things that affect the whole of the application, like launch / app termination, entering the background and returning from the background, that kind of thing.

应用程序委托不应该是各种全局变量的存储库。应用程序委托应该用于影响整个应用程序的事情,例如启动/应用程序终止,进入后台并从后台返回等等。

If there is state that is shared by multiple view controllers, that should exist only once, but doesn't affect the application as a whole, then you could consider creating a singleton for that state. Then again, global state that is just an artefact of how you write your code should be avoided.

如果有多个视图控制器共享的状态,它应该只存在一次,但不会影响整个应用程序,那么您可以考虑为该状态创建一个单例。然后,应该避免全局状态,这只是你编写代码的一个人工制品。

#3


2  

AppDelegate can be used just like singleton, but I don't recommend it. It's like you can put all your classes declarations and definitions in a class.h and a class.m file. Simply import the class.h file can invoke all classes. But it will be very inconvenient to read, understand and manage.

AppDelegate可以像单身一样使用,但我不推荐它。这就像你可以将所有类声明和定义放在class.h和class.m文件中。只需导入class.h文件即可调用所有类。但阅读,理解和管理会非常不方便。

AppDelegate is mainly used for all kinds of app itself event, through UIApplicationDelegate method. Do not recommend deal with too much logic about global data here. Such as classes named XXManager, XXService, PublicData, is proposed to manage all kinds of singleton data.

AppDelegate主要用于各种app本身的事件,通过UIApplicationDelegate方法。不建议在这里处理关于全局数据的过多逻辑。建议使用名为XXManager,XXService,PublicData的类来管理各种单例数据。