In my iPhone app I was using integers to keep track of a lot of variables. I declared and initialized them all in my AppDelegate file (it's a multiview app), and then if I declared them in the other views (classes) and the values would stay the same. In this way, I could set Money = 200 in the App Delegate file, and then in another view just declare, "int Money;" and it would be set to 200 already (or whatever Money happened to be.)
However, if I'm storing all these variables in a Dictionary (which I am doing now), how can I access that dictionary from different classes/views? I can't simple "declare" it again, I've already tried that. I think it has to do with the dictionary being an object, and so it needs to be referenced or something.
I need to be able to access the same dictionary from all the different views.
#import "SheepAppDelegate.h"
@implementation SheepAppDelegate
@synthesize window;
@synthesize rootController;
//Initialize the Dictionary to store all of our variables
NSMutableDictionary *theHeart;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
//Set the values for the Variables and Constants, these are
//accessed from the different classes.
NSMutableDictionary *theHeart = [[NSMutableDictionary alloc] init];
[theHeart setObject:[NSNumber numberWithInt:200] forKey:@"Money"];
[theHeart setObject:@"Number two!" forKey:@"2"];
[window addSubview:rootController.view];
[window makeKeyAndVisible];
}
Initialize the dictionary and adding stuff to it works fine, but in another class.
#import "OverviewController.h"
@implementation OverviewController
@synthesize lblMoney;
@synthesize lb开发者_运维知识库lSheep;
@synthesize lblWool;
@synthesize lblFatness;
@synthesize lblCapacity;
@synthesize lblShepherds;
int varMoney;
NSMutableDictionary *theHeart;
- (void)viewWillAppear:(BOOL)animated {
varMoney = [[theHeart objectForKey:@"Money"] intValue];
}
You can see I try initializing the dictionary again for this class, but that obviously isn't working. I just want to Initialize and setup the dictionary once, in the AppDelegate file, and then access that dictionary from the other classes to change stuff in it. Is there a way to do this?
Make your NSMutableDictionary instance static and write a class method to access it. Put this in your SheepAppDelegate.m:
static NSMutableDictionary *theHeart;
+ (NSMutableDictionary*)theHeart
{
if (theHeart == nil) theHeart = [[NSMutableDictionary alloc] init];
return theHeart;
}
And access it anywhere else by using:
NSMutableDictionary *dict = [SheepAppDelegate theHeart];
You can either put it in your AppDelegate or create a Singleton. This article covers this topic and many of the possible options including the two I mentioned.
Singletons seem to be the more organized method. You can store all of your global information in one and you'll be able to access it from anywhere.
There's no good reason not to just pass the dictionary along to your controller as a reference. If you create an NSMutableDictionary ivar in your OverviewController making it a property, you can then set the dictionary there when you create your controller or when it gets thawed from the nib.
Singletons are useful, but I wouldn't resort to it unless you really need it. You can change your -applicationDidFinishLaunching to something like this:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
//Set the values for the Variables and Constants, these are
//accessed from the different classes.
NSMutableDictionary *theHeart = [NSMutableDictionary dictionary];
[theHeart setObject:[NSNumber numberWithInt:200] forKey:@"Money"];
[theHeart setObject:@"Number two!" forKey:@"2"];
[rootController setHeartDictionary:theHeart];
[window addSubview:rootController.view];
[window makeKeyAndVisible];
}
This assumes your rootController is of type OverviewController. Then in your OverviewController header you should declare a property like this:
@property (assign) NSMutableDictionary *heartDictionary;
and then @synthesize it in the .m file with @synthesize heartDictionary;.
Again, I wouldn't use a Singleton unless you need it. Pass it as a variable to your controller instead.
精彩评论