Iphone 从不同类访问NSMutableDictionary
在我的iPhone应用程序中,我使用整数来跟踪许多变量。我在我的AppDelegate文件(这是一个多视图应用程序)中声明并初始化了它们,然后如果我在其他视图(类)中声明了它们,值将保持不变。通过这种方式,我可以在App委托文件中设置Money=200,然后在另一个视图中声明“int Money;”,并且它已经设置为200(或者不管钱是什么) 但是,如果我将所有这些变量存储在一个字典中(我现在正在这样做),我如何从不同的类/视图访问该字典?我不能再简单地“声明”,我已经试过了。我认为这与词典是一个对象有关,所以它需要被引用或其他什么东西 我需要能够从所有不同的视图访问同一本词典Iphone 从不同类访问NSMutableDictionary,iphone,objective-c,nsmutabledictionary,Iphone,Objective C,Nsmutabledictionary,在我的iPhone应用程序中,我使用整数来跟踪许多变量。我在我的AppDelegate文件(这是一个多视图应用程序)中声明并初始化了它们,然后如果我在其他视图(类)中声明了它们,值将保持不变。通过这种方式,我可以在App委托文件中设置Money=200,然后在另一个视图中声明“int Money;”,并且它已经设置为200(或者不管钱是什么) 但是,如果我将所有这些变量存储在一个字典中(我现在正在这样做),我如何从不同的类/视图访问该字典?我不能再简单地“声明”,我已经试过了。我认为这与词典是一
#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文件中初始化并设置字典一次,然后从其他类访问该字典以更改其中的内容。有办法做到这一点吗?您可以将其放入AppDelegate或创建一个单例。涵盖了这个主题和许多可能的选项,包括我提到的两个
单身似乎是更有组织的方法。您可以将所有全局信息存储在一个数据库中,并且可以从任何位置访问它。将NSMutableDictionary实例设置为静态,并编写一个类方法来访问它。把这个放在你的羊圈里。m:
static NSMutableDictionary *theHeart;
+ (NSMutableDictionary*)theHeart
{
if (theHeart == nil) theHeart = [[NSMutableDictionary alloc] init];
return theHeart;
}
并通过以下方式访问其他任何位置:
NSMutableDictionary *dict = [SheepAppDelegate theHeart];
没有理由不将字典作为引用传递给控制器。如果在OverviewController中创建NSMutableDictionary ivar并将其作为属性,则可以在创建控制器或从nib解冻时在其中设置字典 单身汉很有用,但除非你真的需要,否则我不会求助于它。您可以将-applicationdFinishLaunching更改为以下内容:
- (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];
}
这假设您的rootController是OverviewController类型。然后在OverviewController标题中,您应该声明如下属性:
- (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];
}
@属性(赋值)NSMutableDictionary*heartDictionary
然后在.m文件中使用@synthesis-heartDictionary;对其进行@synthesis
再说一次,除非你需要,否则我不会用单身汉。将其作为变量传递给控制器