-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyUIViewController.m
121 lines (80 loc) · 2.84 KB
/
MyUIViewController.m
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//
// MyUIViewController.m
// ToDoManager
//
// Created by Алексей on 08.04.16.
// Copyright © 2016 Alexey. All rights reserved.
//
#import "MyUIViewController.h"
@interface MyUIViewController ()
@property (strong,nonatomic) NSManagedObjectContext *managedObjectContext;
@property (strong,nonatomic) ToDoEntity *localToDoEntity;
@property (weak, nonatomic) IBOutlet UITextField *titleField;
@property (weak, nonatomic) IBOutlet UITextView *detailsField;
@property (weak, nonatomic) IBOutlet UIDatePicker *dueDateField;
@property (nonatomic,assign) BOOL wasDeleted;
@end
@implementation MyUIViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void) viewWillAppear:(BOOL)animated{
self.wasDeleted = NO;
//set up form
self.titleField.text = self.localToDoEntity.toDoTitle;
self.detailsField.text = self.localToDoEntity.toDoDetails;
NSDate *dueDate = self.localToDoEntity.toDoDueDate;
if (dueDate != nil)
{
[self.dueDateField setDate:dueDate];
}
//Detect edit ends
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidEndEditing:) name:UITextViewTextDidEndEditingNotification object:self];
}
- (IBAction)trashTapped:(id)sender {
self.wasDeleted = YES;
[self.managedObjectContext deleteObject:self.localToDoEntity];
[self saveMyToDoEntity];
[self.navigationController popToRootViewControllerAnimated:YES];
}
-(void) receiveMOC:(NSManagedObjectContext *)incomingMOC{
self.managedObjectContext = incomingMOC;
}
-(void) receiveToDoEntity:(ToDoEntity *)incomingToDoEntity{
self.localToDoEntity = incomingToDoEntity;
}
-(void) textViewDidEndEditing:(NSNotification *)notification
{
if ([notification object] == self)
{
self.localToDoEntity.toDoDetails = self.detailsField.text;
[self saveMyToDoEntity];
}
}
- (IBAction)titleFieldEditted:(id)sender {
self.localToDoEntity.toDoTitle = self.titleField.text;
[self saveMyToDoEntity];
}
- (IBAction)dueDateEditted:(id)sender {
self.localToDoEntity.toDoDueDate = self.dueDateField.date;
[self saveMyToDoEntity];
}
-(void) viewDidDisappear:(BOOL)animated{
if (self.wasDeleted == NO)
{
//Save Everything
self.localToDoEntity.toDoTitle = self.titleField.text;
self.localToDoEntity.toDoDetails = self.detailsField.text;
self.localToDoEntity.toDoDueDate = self.dueDateField.date;
[self saveMyToDoEntity];
}
//Remove detection
[[NSNotificationCenter defaultCenter]removeObserver:self name:UITextViewTextDidEndEditingNotification object:self];
}
-(void)saveMyToDoEntity{
NSError *err;
BOOL saveSuccess = [self.managedObjectContext save:&err];
if (!saveSuccess)
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Couldn't save" userInfo:nil];
}
@end