Define a variable and access it in the entire project
there are some values that I want to read from user preferences on application initialization. I need them in quite all classes of my project.
Is there way to define these variables just once?
I started using static variables but I am not sure if this is the best approach. E.g. I have two controllers, the first controller creates the second controller. To h开发者_运维技巧ave a static variable that is used in both controllers I had to define it in the second controller. I don't know if this is a good code design.
Any ideas or suggestions?
I would recommend reading these values into instance variables in your Application Delegate, and then accessing them via properties from the other classes. You can get to your app delegate from any class by calling [NSApp delegate]
(AppKit) or [[UIApplication sharedApplication] delegate]
(UIKit).
Have a 3rd class that implements the singleton pattern? That is, a class method that allocates self if not allocated yet or the one that was prev allocated. Its like a global var that comes alive when its first referenced
精彩评论