简体   繁体   English

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

[英]Relation between Singleton class & AppDelegate class in iOS Objective-C

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 ? 任何人都可以通过现实生活中的例子来帮助解决单例类的使用问题吗?

AppDelegate is however a singleton class but you show only use it for declaring things that applies globally in your application. 然而,AppDelegate是一个单例类,但您只显示它用于声明应用程序中全局适用的内容。
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. 例如:如果要更改整个应用程序中导航栏的颜色,可以使用app delegate并设置导航栏的颜色。 Also app delegate is an object that handles different state transition in your app. app delegate也是一个处理应用程序中不同状态转换的对象。
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. 因此,如果您想创建一个可以从多个View控制器更改的变量,您应该创建一个单例类并在该类中声明该变量。

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. 然后,应该避免全局状态,这只是你编写代码的一个人工制品。

AppDelegate can be used just like singleton, but I don't recommend it. AppDelegate可以像单身一样使用,但我不推荐它。 It's like you can put all your classes declarations and definitions in a class.h and a class.m file. 就像你可以将所有类声明和定义放在class.h和class.m文件中一样。 Simply import the class.h file can invoke all classes. 只需导入class.h文件即可调用所有类。 But it will be very inconvenient to read, understand and manage. 但阅读,理解和管理会非常不方便。

AppDelegate is mainly used for all kinds of app itself event, through UIApplicationDelegate method. AppDelegate主要用于各种app本身事件,通过UIApplicationDelegate方法。 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. 例如,名为XXManager,XXService,PublicData的类被提议用于管理各种单例数据。

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

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