2009-10-27 63 views
0

我在頭文件中聲明一個變量並在實現中對其進行合成。當視圖加載(ViewDidLoad)時,我讀取一個plist文件,用變量填充一個值。用我的NSLog我看到變量包含值。但是,在視圖加載之後,我通過按鈕執行一個方法來與用戶進行一些交互。通過這種方法,我再次檢查這個值,它是無效的。爲什麼變量在初始加載後不會保持其值?變量丟失其值(iPhone SDK)

program.h

.... 
NSString * user_title; 
... 
@property (nonatomic, retain) NSString *user_title; 

program.m

@synthesize user_title; 

-(void)viewDidLoad{ 

    NSString *filePath = [self dataFilePath]; 
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) 
{ 
    NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath]; 
    user_title = [array objectAtIndex:0]; 
    [array release]; 
} 
    .... 


-(IBAction)user_touch_screen:(id)sender 
{ 
    user_label.text = user_title; //user_title has an invaliud value at this point 
    .... 

回答

5

user_title = [array objectAtIndex:0]不保留該變量。

使用這個代替:

self.user_title = [array objectAtIndex:0]; 

將使用你合成的二傳手,並保留價值。

2

你需要保留你走出陣列的價值。