-
Notifications
You must be signed in to change notification settings - Fork 1
/
BPManagedObjectInspectorViewController.m
223 lines (172 loc) · 4.54 KB
/
BPManagedObjectInspectorViewController.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
//
// BPManagedObjectInspectorViewController.m
//
// Created by Jon Olson on 2/9/10.
// Copyright 2010 Source14 Platforms. All rights reserved.
//
#import "BPManagedObjectInspectorViewController.h"
@interface BPManagedObjectInspectorViewController (Private)
@end
@implementation BPManagedObjectInspectorViewController
#pragma mark -
#pragma mark Construction and deallocation
- (id)init {
if (self = [super initWithNibName:nil bundle:nil]) {
}
return self;
}
- (void)dealloc {
[super dealloc];
}
#pragma mark -
#pragma mark Accessors
@synthesize managedObjectContext;
- (NSManagedObject *)managedObject {
NSAssert(NO, @"Subclasses of BPManagedObjectInspectorViewController must implement managedObject");
return nil;
}
- (void)setManagedObject:(NSManagedObject *)aManagedObject {
NSAssert(NO, @"Subclasses of BPManagedObjectInspectorViewController must implement setManagedObject:");
}
#pragma mark -
#pragma mark View loading and unloading
// Subclasses should override this
- (void)loadView {
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
self.view = view;
[view release];
}
- (void)viewDidLoad {
[super viewDidLoad];
if (!self.navigationItem.rightBarButtonItem)
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
#pragma mark -
#pragma mark View hiding and showing
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self updateView];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if (self.managedObject && ![self.managedObject isDeleted])
[self updateModel];
}
#pragma mark -
#pragma mark Rotation is cool with ushort
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
#pragma mark -
#pragma mark Toggling editing
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
UITableView *tableView = nil;
if ([self respondsToSelector:@selector(tableView)])
tableView = [(id)self tableView];
[tableView beginUpdates];
if (!self.editing && editing) {
[self beginEditing];
if (!self.navigationItem.leftBarButtonItem) {
[self.navigationItem setHidesBackButton:YES animated:YES];
}
[super setEditing:YES animated:animated];
}
else if (self.editing && !editing) {
[self update:self];
if (![self.managedObjectContext hasChanges]) {
// Update worked, no changes
[self.navigationItem setHidesBackButton:NO animated:YES];
[super setEditing:NO animated:animated];
[self endEditing];
}
}
[tableView endUpdates];
}
#pragma mark -
#pragma mark Model-View synchronization
- (void)updateModel {
// Stub
}
- (void)updateView {
// Stub
}
#pragma mark -
#pragma mark Editing hooks
- (void)beginEditing {
// Stub
}
- (void)endEditing {
// Stub
}
#pragma mark -
#pragma mark CRUD hooks
- (BOOL)beforeSave {
return YES;
}
- (void)afterSave {
}
- (BOOL)beforeCreate {
return YES;
}
- (void)afterCreate {
}
- (BOOL)beforeUpdate {
return YES;
}
- (void)afterUpdate {
}
- (BOOL)beforeDestroy {
return YES;
}
- (void)afterDestroy {
}
#pragma mark -
#pragma mark Default UI actions
- (IBAction)create:(id)sender {
[self updateModel];
if (![self beforeCreate])
return;
if (![self beforeSave])
return;
NSError *error = nil;
if (![self.managedObjectContext save:&error])
[[[[UIAlertView alloc] initWithTitle:@"Error Creating Object" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil] autorelease] show];
else {
[self afterSave];
[self afterCreate];
}
}
- (IBAction)update:(id)sender {
[self updateModel];
if (![self beforeUpdate])
return;
if (![self beforeSave])
return;
NSError *error = nil;
if (![self.managedObjectContext save:&error])
[[[[UIAlertView alloc] initWithTitle:@"Error Saving Changes" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil] autorelease] show];
else {
[self afterSave];
[self afterUpdate];
}
}
- (IBAction)rollback:(id)sender {
[self.managedObjectContext rollback];
[self updateView];
[self setEditing:NO animated:YES];
}
- (IBAction)destroy:(id)sender {
if (![self beforeDestroy])
return;
[self.managedObjectContext deleteObject:self.managedObject];
NSError *error = nil;
if (![self.managedObjectContext save:&error])
[[[[UIAlertView alloc] initWithTitle:@"Error Canceling" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil] autorelease] show];
else
[self afterDestroy];
}
@end