Hey all,
So, 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 lblSheep;
@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 iniializing the dictionary again for this class, but that obviously itsn'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? Thanks!