2014-04-16 44 views
0

這裏是我的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方法時,應用程序恢復呢?

+0

的可能重複[寫NSMutableArray的一個文件並加載回(http://stackoverflow.com/questions/19594670/你可以檢查yourArray方式write-a-nsmutablearray -a-file-and-load-the-nsmutablearray-from-a-file-comman) –

+0

XYZToDoItem是否非常複雜(我的意思是嵌套了很多自定義對象)?你可以將它保存在[NSUserDefault](http://nshipster.com/nscoding/)aswell – HaneTV

+0

我不知道我在這個問題上看到一個答案,我可以工作。我會檢查發佈的蘋果演練。 @HaneTV他們不是;只是用戶輸入的文本值。我試圖搞砸UserDefault路線,但我不知道我明白這是如何工作的。 –

回答

0

就好像我明白你真的想要保存你的NSMutableArray,即使重新啓動你的應用程序後。 所以你可以使用這段代碼來保存你的NSMutableArray和NSUserDefaults。

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

而且它是你可以把它送回去

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

但不要忘記之前初始化它保存NSUserDefaults的。 通過任何時候如果加載或不

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

謝謝。我終於找到了代碼放置位置以及我曾經犯過的錯別字。 –

+0

@Deannakov歡迎。 –

0

「我用什麼來檢查文件是否存在」

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

」 ..和如果是的話,顯示它?「

請閱讀關於UITableView和UITableViewDataSource https://developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html

「並會影響我的clearData方法時,應用程序恢復呢?」

clearData是一個按鈕動作。只有當用戶點擊一個按鈕 時,纔會調用它。它與應用程序啓動/停止無關。

+0

我對clearData的想法是這樣的:手機在那裏有一個存儲列表,並且用戶退出,所以信息被保存。當應用程序重新打開時,用戶清除數據,然後再次退出應用程序。這會重新顯示上次保存的數據嗎? –

+0

所以試圖遵循這個邏輯,我簡單地添加了:'NSFileManager * fileManager = [NSFileManager defaultManager]; if([fileManager fileExistsAtPath:@「toDoItems.plist」]) { self.toDoItems = [[NSArray arrayWithContentsOfFile:@「toDoItems.plist」] init]; } else { self.toDoItems = [[NSMutableArray alloc] init]; }'我的'viewDidLoad'方法,但無濟於事...... –

+0

對不起,我不知道我理解你。這裏'[self.toDoItems removeAllObjects];'你只清理運行時數組。文件不受影響。 – Avt

相關問題