Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 *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?

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

like image 977
Alen Alexander Avatar asked Jan 31 '26 01:01

Alen Alexander


1 Answers

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.

like image 136
Akshay Agrawal Avatar answered Feb 02 '26 19:02

Akshay Agrawal