I have an enum defined in the Constants.h file this way:
typedef enum {
CalendarTypeMonth = 0,
CalendarTypeWeek
} CalendarType;
Then in my view controller I determine what the calendar type should be and store it in a property this way:
@property (nonatomic) CalendarType myCalendarType;
Now I want all the classes in my project have access to the calendar type. How can I set this property to be global/extern so that all classes can read this?
EDIT: I know the definition of the enum will be available across the project. But what I am interested in is the value of myCalendarType
. How can I access the value of myCalendarType across all classes?
You can declare class method to access static variable.
Add such code to your implementation file:
static MyStaticType staticVar = MyStaticTypeDefault;
+(BOOL)myStaticVar
{
return staticVar;
}
+(void)setMyStaticVar:(MyStaticType)newValue
{
staticVar = newValue;
}
And create declarations for this methods in interface file. This is much better then moving all static values to AppDelegate. Anyway, a lot of variants are possible - for example, you can create singletone to store some settings of application or use CoreData.
In your [Project Name]_Prefix.pch file, you can add this statement:
#import "Constants.h"
In so doing, you'll make your enum available to every file in the project.
EDIT: To access the value, I suggest making it a global property of your application delegate. To take it a step further, you can make your application delegate a method of NSObject using a category so it might look something like:
CalendarType current = [self appDelegate].currentCalendarType
Observe that your property is now a globally gettable/settable property.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.