2013-05-28 14 views
0

我創造了我的.h文件的主要的viewController的NSMutableDictionary稱爲*temp,並添加該代碼中的信息把從我.plist文件。的NSMutableDictionary的空隙沒有工作,IBActions

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]; 
    temp=[NSMutableDictionary dictionaryWithContentsOfFile:path]; 
} 

在同一視圖控制器,我添加一個按鈕動作,並添加以下代碼:

-(IBAction)mathButton:(UIButton *)_sender 
{ 
    label1.text = [temp objectForKey:@"m1name"]; 
} 

其中「label1的是在.xib文本字段,和m1name是在鍵中的一個在.plist

但是當我運行它,它不工作,並強調label1.text = [temp objectForKey:@"m1name"];並調用它壞的訪問。

我已經堅持了幾天,並嘗試了很多東西。答案會很有幫助。

感謝

回答

0
temp=[NSMutableDictionary dictionaryWithContentsOfFile:path]; 

你不保留通過dictionaryWithContentsOfFile:path創建的字典。您應該更改該行:

temp = [[NSMutableDictionary dictionaryWithContentsOfFile:path] retain]; 

(並確保它在dealloc發佈),或者,如果temp是一個屬性,通過

self.temp = [NSMutableDictionary dictionaryWithContentsOfFile:path]; 
+0

謝謝你這麼多! – user2430463

0

在.H設置:

@interface ... 
{ 
    NSMutableDictionary* temp; 
} 

在.M:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSString* path = [[NSBundle mainBundle] pathForResource: @"Data" 
                ofType: @"plist"]; 

    BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath: path]; 

    if (exists) 
    { 
     temp = [[NSMutableDictionary alloc] initWithContentsOfFile: path]; 
     NSLog(@"%@", [temp description]); 
    } 
} 

- (IBAction) mathButton: (UIButton *)_sender 
{ 
    label1.text = [temp objectForKey: @"m1name"]; 
} 

如果MRC:

- (void) dealloc 
{ 
    [temp release]; 

    [super dealloc]; 
}