-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCoreEditingDetailView.m
227 lines (163 loc) · 5.54 KB
/
CoreEditingDetailView.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//
// CoreEditingDetailView.m
// MusicOneCoreData
//
// Created by Bobby Wallace on 02/16/2010.
// Copyright 2010 Total Managed Fitness. All rights reserved.
//
#import "CoreEditingDetailView.h"
@implementation CoreEditingDetailView
@synthesize cancelButton;
@synthesize sortDescriptors;
@synthesize managedObject;
@synthesize managedObjectContext;
@synthesize detailDelegate;
@synthesize appDelegate;
@synthesize entityName;
@synthesize canEdit;
@synthesize fetchedResultsController, fetchPredicate;
- (void) viewDidLoad {
// self.managedObjectContext = appDelegate.rootViewController.managedObjectContext;
self.appDelegate = (MusicPlayerAppDelegate *)[[UIApplication sharedApplication] delegate];
// NSLog(@"Predicate=%@", self.fetchPredicate);
NSFetchedResultsController * frc = self.fetchedResultsController;
[frc.fetchRequest setPredicate:self.fetchPredicate];
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
[appDelegate processError:error];
return;
} else {
NSInteger count = [fetchedResultsController.fetchedObjects count];
if (count) {
self.managedObject = [fetchedResultsController.fetchedObjects objectAtIndex:0];
}
}
[self loadDataFromManagedObject];
if (self.canEdit) {
[self.navigationItem setRightBarButtonItem:self.editButtonItem animated:YES];
[self enableEdits:NO];
self.cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];
}
}
- (void)viewDidUnload {
// self.managedObject = nil;
self.entityName = nil;
self.cancelButton = nil;
self.fetchPredicate = nil;
self.fetchedResultsController = nil;
self.sortDescriptors = nil;
[super viewDidUnload];
}
- (void)dealloc {
// [managedObject release];
[cancelButton release];
[fetchedResultsController release];
[fetchPredicate release];
[entityName release];
[sortDescriptors release];
[super dealloc];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void) loadDataFromManagedObject {
}
- (void) copyDataToManagedObject {
}
- (void) enableEdits:(BOOL) enable {
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// if (textField == nameTextField) {
[textField resignFirstResponder];
[self save];
//}
return YES;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self enableEdits:editing];
[self.navigationItem setHidesBackButton:editing animated:YES];
/*
If editing is finished, save the managed object context.
*/
if(!editing) {
if(self.navigationItem.leftBarButtonItem == cancelButton) {
[self save];
}
} else {
[self.navigationItem setLeftBarButtonItem:cancelButton animated:YES];
}
}
- (BOOL)save {
[self copyDataToManagedObject];
NSError *error = nil;
if ([self.managedObjectContext save:&error]) {
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
[self.detailDelegate saved];
return YES;
} else if (error != nil) {
[self processError:error];
[self setEditing:YES animated:YES];
}
return NO;
}
- (void)cancel {
[self loadDataFromManagedObject];
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
[self setEditing:NO animated:YES];
}
- (void)done {
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
-(void) processError:(NSError *) error {
[self.appDelegate processError:error];
}
- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController != nil) {
return fetchedResultsController;
}
/*
Set up the fetched results controller.
*/
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:self.entityName inManagedObjectContext:appDelegate.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setSortDescriptors:self.sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:appDelegate.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
return fetchedResultsController;
}
#pragma mark -
#pragma mark NSFetchedResultsController Delegate Methods
/*
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller is about to start sending change notifications, so prepare the table view for updates.
//NSLog(@"controllerWillChangeContent");
//[self.tableView beginUpdates];
}
*/
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
// this gets called if the song got updated elsewhere...so update the UI with the new data, too.
[self performSelectorOnMainThread:@selector(loadDataFromManagedObject) withObject:nil waitUntilDone:NO];
}
/*
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
//UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeDelete:
//NSLog(@"NSFetchedResultsChangeDelete");
break;
case NSFetchedResultsChangeUpdate:
//NSLog(@"NSFetchedResultsChangeUpdate");
break;
}
}
*/
@end