变量失去价值(iPhone SDK)

Kev*_*den 0 iphone sdk cocoa touch

我在头文件中声明了一个变量,并在实现中合成它.当视图加载(ViewDidLoad)时,我读取了一个plist文件,用一个值填充变量.在我的NSLog中,我看到该变量包含该值.但是,在视图加载后,我通过按钮执行一个方法与用户进行一些交互.用那个方法我再次检查值,它是无效的.为什么变量在初始加载后不会保持其值?

program.h

....
NSString * user_title;
...
@property (nonatomic, retain) NSString *user_title;
Run Code Online (Sandbox Code Playgroud)

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
   ....
Run Code Online (Sandbox Code Playgroud)

Tho*_*ler 5

user_title = [array objectAtIndex:0] 不保留变量.

请改用:

self.user_title = [array objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)

这将使用您合成的setter,并将保留该值.