forked from jsonmodel/jsonmodel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorageViewController.m
More file actions
66 lines (50 loc) · 1.49 KB
/
StorageViewController.m
File metadata and controls
66 lines (50 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// StorageViewController.m
// JSONModelDemo
//
// Created by Marin Todorov on 02/12/2012.
// Copyright (c) 2012 Underplot ltd. All rights reserved.
//
#import "StorageViewController.h"
#import "MyDataModel.h"
@interface StorageViewController ()
{
IBOutlet UITextView* txtContent;
IBOutlet UILabel* lblTimes;
NSString* filePath;
MyDataModel* data;
}
@end
@implementation StorageViewController
-(void)viewDidAppear:(BOOL)animated
{
NSString* libraryDir = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory , NSUserDomainMask, YES)[0];
filePath = [libraryDir stringByAppendingPathComponent:@"saved.plist"];
[self loadFromFile];
}
-(void)loadFromFile
{
//load from file
NSDictionary* object = [NSDictionary dictionaryWithContentsOfFile:filePath];
//initialize model with data
JSONModelError* initError;
data = [[MyDataModel alloc] initWithDictionary: object error:&initError];
if (!data) {
data = [[MyDataModel alloc] init];
}
//update the UI
lblTimes.text = [NSString stringWithFormat:@"Times saved: %i", data.timesSaved];
txtContent.text = data.content;
}
-(IBAction)actionSave:(id)sender
{
[txtContent resignFirstResponder];
//update model
data.timesSaved++;
data.content = txtContent.text;
//update UI
lblTimes.text = [NSString stringWithFormat:@"Times saved: %i", data.timesSaved];
//save to disc
[[data toDictionary] writeToFile:filePath atomically:YES];
}
@end