0

iOS 新手,在 NSMutable Array 中添加对象并在 App 内的另一个视图上显示该数组时遇到了一个问题。数据在 TableView 中的其他视图上显示正常,但是当我向数组添加另一个项目(使用下面的代码)时,它只是替换了那里的内容,而不是添加到数组中。

- (void) postArray {
tableData = [[NSMutableArray alloc] initWithCapacity:10];
tableData = [[NSMutableArray alloc] initWithObjects: nil];
[tableData addObject:favShot]; }

-(NSString *) saveFilePath {

NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, TRUE);
return [[path objectAtIndex:0] stringByAppendingPathComponent:@"savefile.plist"]; }

- (void) viewWillDisappear:(BOOL)animated: (UIApplication *) application  {
NSArray *values = [[NSArray alloc] initWithObjects:tableData, nil];
[values writeToFile:[self saveFilePath] atomically: TRUE]; }

- (void)viewDidLoad  {

tableData = [[NSMutableArray alloc] initWithObjects: nil];

NSString *myPath = [self saveFilePath];

BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:myPath];

if (fileExists)
{

    NSArray *values = [[NSArray alloc] initWithContentsOfFile:myPath];
    tableData = [values mutable copy];
}

UIApplication *myApp = [UIApplication sharedApplication];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(applicationDidEnterBackground:)
                                             name:UIApplicationDidEnterBackgroundNotification
                                           object:myApp];

[super viewDidLoad];  }

谢谢你。

4

2 回答 2

0

尝试

- (void) viewWillDisappear:(BOOL)animated: (UIApplication *) application 
{
    NSString *filePath = [self saveFilePath];

    NSMutableArray *savedArray = [NSMutableArray arrayWithContentsOfFile:filePath];
    if (!savedArray) savedArray = [NSMutableArray array];

    [savedArray addObject:tableData];

    [savedArray writeToFile:filePath atomically:YES];
}
于 2013-04-27T15:13:11.323 回答
0

每次调用时,postArray您都在创建 的实例NSMutableArray,然后将其丢弃(如果您不使用 ARC,则将其泄漏)。然后你正在创建另一个实例NSMutableArray。然后,您将向第二个实例添加一个对象 (favShot)。

下次您调用postArray它时,它将丢弃您的旧数组并创建 2 个新数组。

您要做的是tableData在创建控制器实例或加载视图时创建实例。然后,不要设置tableData = ...,因为那样会丢弃旧实例。只需添加和删除对象。

编辑

- (void) postArray {
[tableData addObject:favShot];
}

- (NSString *)saveFilePath {
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, TRUE);
return [[path objectAtIndex:0] stringByAppendingPathComponent:@"savefile.plist"];
}

- (void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];

[tableData writeToFile:[self saveFilePath] atomically: TRUE];
}

- (void)viewDidLoad  {
NSString *myPath = [self saveFilePath];

BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:myPath];

if (fileExists)
{
    tableData = [[NSMutableArray alloc] initWithContentsOfFile:myPath];
} else {
    tableData = [[NSMutableArray alloc] initWithObjects: nil];
}

UIApplication *myApp = [UIApplication sharedApplication];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(applicationDidEnterBackground:)
                                             name:UIApplicationDidEnterBackgroundNotification
                                           object:myApp];

[super viewDidLoad];
}
于 2013-04-27T13:24:45.323 回答