Pregunta

Here is my mainAppDelegate.h:

#import <UIKit/UIKit.h>

@interface mainAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property NSMutableArray *toDoItems;

@end

and my mainAppDelegate.m:

#import "mainAppDelegate.h"

@implementation mainAppDelegate

- (void)applicationWillTerminate:(UIApplication *)application
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"toDoItems.plist"];
    [self.toDoItems writeToFile:filePath atomically:TRUE];
}
@end

I have another file, XYZToDoListViewController.m with the code:

#import "XYZToDoListViewController.h"
#import "XYZToDoItem.h"
#import "XYZAddItemViewController.h"

@interface XYZToDoListViewController ()
@property NSMutableArray *toDoItems;
@end

@implementation XYZToDoListViewController

- (IBAction)unwindToList:(UIStoryboardSegue *)segue
{
    XYZAddItemViewController *source = [segue sourceViewController];
    XYZToDoItem *item = source.toDoItem;
    if (item != nil) {
        [self.toDoItems addObject:item];
        [self.tableView reloadData];
    }
}

- (IBAction)clearData:(UIBarButtonItem *)sender;
{
    [self.toDoItems removeAllObjects];
    [self.tableView reloadData];
}

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {

}
return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.toDoItems = [[NSMutableArray alloc] init];
    self.navigationController.view.backgroundColor =
    [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_full.png"]];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.contentInset = UIEdgeInsetsMake(-35, 0, -35, 0);
}
@end

This is at least what I think is relevant. My basic framework is something I followed from this tutorial. So as you can see I have an NSMutableArray named .toDoItems. and I want it to remember the list when I exit the application (not just minimizing it; it does that already. I think I have the code which is saving the data, but what do I use to check if the file exists, and if so, display it? And would this affect my clearData method when the app resumes as well?

¿Fue útil?

Solución

As if i understood true you want to save your NSMutableArray even after restart your app. So you may use this codes to save your NSMutableArray with NSUserDefaults.

 NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaultsPhoneId setObject:yourArray forKey:@"yourKey"];
[defaultsPhoneId synchronize];

And it is how you can take it back

yourArray=[[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults]objectForKey:@"yourKey"]];

But dont forget to initialize it before save with NSUserDefaults. By the way you may check yourArray anytime if it is loaded or not with

if([yourArray count]==0){
 //Your array is empty so you have to call it with @"yourKey". (second codes part)
}

Otros consejos

"what do I use to check if the file exists"

NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:your_file_path])

"..and if so, display it?"

Please read about UITableView and UITableViewDataSource https://developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html

"And would this affect my clearData method when the app resumes as well?"

clearData is a button action. It will be called only if user will tap a button. It is not connected with application launch/stopping.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top