내 iPhone 앱에서 정수를 사용하여 많은 변수를 추적했습니다. AppDelegate 파일 (모두 멀티 뷰 앱)에 선언하고 초기화 한 다음 다른 뷰 (클래스)에서 값을 선언하면 값이 동일하게 유지됩니다. 이 방법으로, 나는 Money = 200을 App Delegate 파일에 설정 한 다음 다른보기에서 "int Money;"라고 선언 할 수 있습니다. 그리고 이미 200으로 설정 될 것입니다. (또는 돈이 어떻게 되었든간에)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 파일에서 사전을 초기화하고 설정 한 다음 다른 클래스의 해당 사전에 액세스하여 사전을 액세스하려고합니다. 이것을 할 수있는 방법이 있습니까?