2009-11-11 9 views
2

내 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 파일에서 사전을 초기화하고 설정 한 다음 다른 클래스의 해당 사전에 액세스하여 사전을 액세스하려고합니다. 이것을 할 수있는 방법이 있습니까?

답변

6

NSMutableDictionary 인스턴스를 정적으로 만들고 액세스 할 클래스 메서드를 작성하십시오.

static NSMutableDictionary *theHeart; 
+ (NSMutableDictionary*)theHeart 
{ 
    if (theHeart == nil) theHeart = [[NSMutableDictionary alloc] init]; 

    return theHeart; 
} 

을 그리고 사용하여 다른 곳에서는 액세스 : 당신의 SheepAppDelegate.m에 넣고

NSMutableDictionary *dict = [SheepAppDelegate theHeart]; 
2

AppDelegate에 넣거나 싱글 톤을 만들 수 있습니다. This article은이 주제와 내가 언급 한 두 가지를 포함한 가능한 옵션 중 많은 부분을 다룹니다.

싱글 톤은보다 체계적인 방법 인 것처럼 보입니다. 모든 글로벌 정보를 하나로 저장할 수 있으므로 어느 곳에서나 액세스 할 수 있습니다.

2

단지 참고로 컨트롤러에 따라 사전을 통과하지 않는 좋은 이유가 없다. OverviewController에 NSMutableDictionary ivar를 만들어 속성을 만들면 컨트롤러를 만들 때 또는 nib에서 해동 될 때 사전을 설정할 수 있습니다.

싱글 톤은 유용하지만 실제로 필요하지 않으면 나는 그것에 의지하지 않을 것입니다. 당신은 이런 식으로 -applicationDidFinishLaunching을 변경할 수 있습니다

- (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 헤더에서 다음과 같은 속성을 선언해야합니다.

@property (assign) NSMutableDictionary * heartDictionary;

그리고 @synthesize heartDictionary;로 .m 파일에서 @synthesize.

다시 말하면, 내가 필요로하지 않는 한, 나는 싱글 톤을 사용하지 않을 것이다. 대신 컨트롤러에 변수로 전달하십시오.

관련 문제