在我的iPhone应用程序中,我使用整数来跟踪很多变量。我在我的AppDelegate文件(它是一个多视图应用程序)中声明并初始化它们,然后如果我在其他视图(类)中声明它们并且值将保持不变。通过这种方式,我可以在App Delegate文件中设置Money = 200,然后在另一个视图中声明“int Money”。并且它已经设置为200(或者其他任何Money)从不同的类访问NSMutableDictionary
但是,如果我将所有这些变量存储在字典中(我现在正在这样做),如何从不同的字典中访问该字典类/看法?我不能简单地“再次申报”,我已经试过了。我认为它与作为对象的字典有关,因此它需要被引用或者其他东西。
我需要能够从所有不同的视图访问相同的词典。
#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];
}
初始化字典并添加东西到它工作正常,但在另一个类。
#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];
}
你可以看到我尝试再次为这个类初始化字典,但显然不工作。我只想在AppDelegate文件中初始化并设置字典一次,然后从其他类访问该字典以更改其中的内容。有没有办法做到这一点?