· 7 years ago · Dec 14, 2018, 11:48 PM
1//
2// RootViewController.m
3// Random Dates
4//
5// Created by Imaginative on 9/10/11.
6// Copyright 2011 __MyCompanyName__. All rights reserved.
7//
8
9#import "RootViewController.h"
10#import "MLog.h"
11
12@implementation RootViewController
13
14@synthesize managedObjectContext, datesArray;
15
16#pragma mark -
17#pragma mark View lifecycle
18
19- (void)viewDidLoad {
20
21 [super viewDidLoad];
22
23 // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
24 self.navigationItem.leftBarButtonItem = self.editButtonItem;
25
26 // addButton will be applied to the Right Button Control
27 UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
28 initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
29 target: self
30 action: @selector(addNewRandomDate)];
31
32 self.navigationItem.rightBarButtonItem = addButton;
33
34 [self fetchRandomDates];
35}
36
37- (void) addNewRandomDate {
38
39 NSManagedObject *newRandomDate = [NSEntityDescription
40 insertNewObjectForEntityForName: @"RandomDate"
41 inManagedObjectContext: managedObjectContext];
42
43 NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
44
45 [dateFormatter setTimeStyle: NSDateFormatterNoStyle];
46 [dateFormatter setDateFormat:@"EEEE"];
47
48 NSDate *date = [NSDate dateWithTimeIntervalSince1970: arc4random()];
49 NSString *dayName = [dateFormatter stringFromDate: date];
50
51 MLogString(@"date: %@ - dayName: %@", date, dayName);
52
53 // NSLog(@"date: %@", date);
54
55 // Convert Date to String //////////////////////////////////////////////
56 // NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
57 // [formatter setDateFormat:@"yyyy"];
58
59 //Optionally for time zone conversions
60 // [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]];
61
62 // NSString *stringFromDate = [formatter stringFromDate: myNSDateInstance];
63 ///////////////////////////////////////////////////////////////////////
64
65 NSString *sDate = [dateFormatter stringFromDate: date];
66 NSString *sDayName = [dateFormatter stringFromDate: dayName];
67
68 [newRandomDate setValue:date forKey: @"date"];
69 [newRandomDate setValue:dayName forKey: @"dayName"];
70
71 NSError *anyError = nil;
72
73 BOOL success = [managedObjectContext save:&anyError];
74
75 if (!success) { NSLog(@"Error = %@", anyError); }
76
77 [self fetchRandomDates];
78
79 // Alert
80 UIAlertView *message = [[UIAlertView alloc] initWithTitle: sDate
81 message: sDayName
82 delegate: self
83 cancelButtonTitle: @"OK"
84 otherButtonTitles: nil];
85 [message show];
86 [message release];
87}
88
89// Fetching the Random Date Objects
90- (void) fetchRandomDates {
91
92 NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
93
94 [request setEntity: [NSEntityDescription entityForName: @"RandomDate" inManagedObjectContext: managedObjectContext]];
95 MLogString(@"request: %@", request);
96
97 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
98
99 [request setSortDescriptors: [NSArray arrayWithObject: sortDescriptor]];
100
101 [sortDescriptor release];
102
103 // Release the datesArray, if it already exists
104 if (datesArray) {
105
106 self.datesArray = nil;
107 }
108
109 NSError *anyError = nil;
110 NSArray *results = [managedObjectContext executeFetchRequest: request error: &anyError];
111
112 if (!results) {
113
114 NSLog(@"Error = %@", anyError);
115
116 } else {
117
118 self.datesArray = results;
119 }
120
121 [self.tableView reloadData];
122}
123
124/*
125- (void)viewWillAppear:(BOOL)animated {
126 [super viewWillAppear:animated];
127}
128*/
129/*
130- (void)viewDidAppear:(BOOL)animated {
131 [super viewDidAppear:animated];
132}
133*/
134/*
135- (void)viewWillDisappear:(BOOL)animated {
136 [super viewWillDisappear:animated];
137}
138*/
139/*
140- (void)viewDidDisappear:(BOOL)animated {
141 [super viewDidDisappear:animated];
142}
143*/
144/*
145// Override to allow orientations other than the default portrait orientation.
146- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
147 // Return YES for supported orientations.
148 return (interfaceOrientation == UIInterfaceOrientationPortrait);
149}
150*/
151
152#pragma mark -
153#pragma mark Table view data source
154
155- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
156 // Return the number of sections.
157 return 1;
158}
159
160
161- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
162
163 // Return the number of rows in the section.
164 return [datesArray count];
165}
166
167// Customize the appearance of table view cells.
168- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
169
170 static NSString *CellIdentifier = @"Cell";
171
172 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
173
174 if (cell == nil) {
175
176 cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
177 }
178
179 // Configure the cell...
180
181 NSManagedObject *object = [datesArray objectAtIndex:[indexPath row]];
182
183 cell.textLabel.text = [[object valueForKey:@"date"] description];
184 cell.detailTextLabel.text = [object valueForKey:@"dayName"];
185
186 return cell;
187}
188
189/*
190// Override to support conditional editing of the table view.
191- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
192 // Return NO if you do not want the specified item to be editable.
193 return YES;
194}
195*/
196
197#pragma mark -
198#pragma mark Current Development
199// Deleting items
200
201// Override to support editing the table view.
202- (void)tableView:(UITableView *)tableView
203 commitEditingStyle: (UITableViewCellEditingStyle) editingStyle
204 forRowAtIndexPath: (NSIndexPath *) indexPath {
205
206 if (editingStyle == UITableViewCellEditingStyleDelete) {
207
208 // Delete the row from the data source.
209 NSManagedObject *objectToDelete = [datesArray objectAtIndex: [indexPath row]];
210
211 [managedObjectContext deleteObject: objectToDelete];
212
213 [self fetchRandomDates];
214
215 // [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
216 }
217
218 else if (editingStyle == UITableViewCellEditingStyleInsert) {
219 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
220 }
221}
222
223/*
224// Override to support rearranging the table view.
225- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
226}
227*/
228
229
230/*
231// Override to support conditional rearranging of the table view.
232- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
233 // Return NO if you do not want the item to be re-orderable.
234 return YES;
235}
236*/
237
238
239#pragma mark -
240#pragma mark Table view delegate
241
242- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
243
244 // Navigation logic may go here. Create and push another view controller.
245 /*
246 <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
247 // ...
248 // Pass the selected object to the new view controller.
249 [self.navigationController pushViewController:detailViewController animated:YES];
250 [detailViewController release];
251 */
252}
253
254#pragma mark -
255#pragma mark Memory management
256
257- (void) didReceiveMemoryWarning {
258
259 // Releases the view if it doesn't have a superview.
260 [super didReceiveMemoryWarning];
261
262 // Relinquish ownership any cached data, images, etc. that aren't in use.
263}
264
265- (void) viewDidUnload {
266
267 [datesArray release];
268
269 // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
270 // For example: self.myOutlet = nil;
271}
272
273
274- (void)dealloc {
275 [super dealloc];
276}
277
278
279@end