這裏是我的mainAppDelegate.h
:如何加載/保存數據的NSMutableArray
#import <UIKit/UIKit.h>
@interface mainAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property NSMutableArray *toDoItems;
@end
和我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
我還有一個文件,XYZToDoListViewController.m
與代碼:
#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 tutorial.開始的。因此,您可以看到我有一個名爲.toDoItems的NSMutableArray。我希望它在退出應用程序時記住列表(不只是最小化它;它已經做到了,我想我有保存數據的代碼,但是我用什麼來檢查文件是否存在,以及如果因此,顯示呢? ,並會影響我的clearData
方法時,應用程序恢復呢?
的可能重複[寫NSMutableArray的一個文件並加載回(http://stackoverflow.com/questions/19594670/你可以檢查yourArray方式write-a-nsmutablearray -a-file-and-load-the-nsmutablearray-from-a-file-comman) –
XYZToDoItem是否非常複雜(我的意思是嵌套了很多自定義對象)?你可以將它保存在[NSUserDefault](http://nshipster.com/nscoding/)aswell – HaneTV
我不知道我在這個問題上看到一個答案,我可以工作。我會檢查發佈的蘋果演練。 @HaneTV他們不是;只是用戶輸入的文本值。我試圖搞砸UserDefault路線,但我不知道我明白這是如何工作的。 –