2009-10-27 61 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

你需要保留你走出阵列的价值。