0

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

程序.h

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

程序.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
   ....
4

2 回答 2

5

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

改用这个:

self.user_title = [array objectAtIndex:0];

这将使用您合成的设置器,并保留该值。

于 2009-10-27T01:30:32.323 回答
2

您需要保留从数组中获得的值。

于 2009-10-27T01:30:10.103 回答