Created
November 6, 2012 04:35
-
-
Save ChrisRisner/4022579 to your computer and use it in GitHub Desktop.
iOS Day 12
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface AppDelegate : UIResponder <UIApplicationDelegate> | |
@property (strong, nonatomic) UIWindow *window; | |
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext; | |
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel; | |
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator; | |
- (void)saveContext; | |
- (NSURL *)applicationDocumentsDirectory; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma mark - Core Data stack | |
// Returns the managed object context for the application. | |
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application. | |
- (NSManagedObjectContext *)managedObjectContext | |
{ | |
if (_managedObjectContext != nil) { | |
return _managedObjectContext; | |
} | |
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; | |
if (coordinator != nil) { | |
_managedObjectContext = [[NSManagedObjectContext alloc] init]; | |
[_managedObjectContext setPersistentStoreCoordinator:coordinator]; | |
} | |
return _managedObjectContext; | |
} | |
// Returns the managed object model for the application. | |
// If the model doesn't already exist, it is created from the application's model. | |
- (NSManagedObjectModel *)managedObjectModel | |
{ | |
if (_managedObjectModel != nil) { | |
return _managedObjectModel; | |
} | |
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"DayTwelveData" withExtension:@"momd"]; | |
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; | |
return _managedObjectModel; | |
} | |
// Returns the persistent store coordinator for the application. | |
// If the coordinator doesn't already exist, it is created and the application's store added to it. | |
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator | |
{ | |
if (_persistentStoreCoordinator != nil) { | |
return _persistentStoreCoordinator; | |
} | |
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"DayTwelveData.sqlite"]; | |
NSError *error = nil; | |
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; | |
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) { | |
NSLog(@"Unresolved error %@, %@", error, [error userInfo]); | |
abort(); | |
} | |
return _persistentStoreCoordinator; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)saveContext | |
{ | |
NSError *error = nil; | |
NSManagedObjectContext *managedObjectContext = self.managedObjectContext; | |
if (managedObjectContext != nil) { | |
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) { | |
// Replace this implementation with code to handle the error appropriately. | |
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. | |
NSLog(@"Unresolved error %@, %@", error, [error userInfo]); | |
abort(); | |
} | |
} | |
} | |
#pragma mark - Application's Documents directory | |
// Returns the URL to the application's Documents directory. | |
- (NSURL *)applicationDocumentsDirectory | |
{ | |
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@synthesize managedObjectContext = _managedObjectContext; | |
@synthesize managedObjectModel = _managedObjectModel; | |
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (IBAction)tappedLoadData:(id)sender { | |
AppDelegate *delegate = [UIApplication sharedApplication].delegate; | |
NSManagedObjectContext *context = [delegate managedObjectContext]; | |
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; | |
NSEntityDescription *entity = [NSEntityDescription | |
entityForName:@"DataRecord" inManagedObjectContext:context]; | |
[fetchRequest setEntity:entity]; | |
NSError *error; | |
fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; | |
for (NSManagedObject *obj in fetchedObjects) { | |
NSLog(@"Name: %@", [obj valueForKey:@"name"]); | |
NSLog(@"Info: %@", [obj valueForKey:@"info"]); | |
NSLog(@"Number: %@", [obj valueForKey:@"number"]); | |
NSLog(@"Create Date: %@", [obj valueForKey:@"createDate"]); | |
NSLog(@"Last Update: %@", [obj valueForKey:@"updateDate"]); | |
} | |
NSManagedObject *obj = [fetchedObjects objectAtIndex:0]; | |
[self displayManagedObject:obj]; | |
selectedObject = obj; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface ViewController : UIViewController | |
@property (nonatomic,strong) NSManagedObjectContext* managedObjectContext; | |
@property (weak, nonatomic) IBOutlet UITextField *txtName; | |
@property (weak, nonatomic) IBOutlet UITextField *txtInfo; | |
@property (weak, nonatomic) IBOutlet UITextField *txtNumber; | |
@property (weak, nonatomic) IBOutlet UILabel *lblCreateDate; | |
@property (weak, nonatomic) IBOutlet UILabel *lblUpdateDate; | |
- (IBAction)tappedSaveAsNew:(id)sender; | |
- (IBAction)tappedLoadData:(id)sender; | |
- (IBAction)tappedNext:(id)sender; | |
- (IBAction)tappedPrevious:(id)sender; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (IBAction)tappedSaveAsNew:(id)sender { | |
AppDelegate *delegate = [UIApplication sharedApplication].delegate; | |
NSManagedObjectContext *context = [delegate managedObjectContext]; | |
NSManagedObject *dataRecord = [NSEntityDescription | |
insertNewObjectForEntityForName:@"DataRecord" | |
inManagedObjectContext:context]; | |
[dataRecord setValue:self.txtName.text forKey:@"name"]; | |
[dataRecord setValue:self.txtInfo.text forKey:@"info"]; | |
[dataRecord setValue:[NSNumber numberWithInteger:[self.txtNumber.text integerValue]] forKey:@"number"]; | |
[dataRecord setValue:[NSDate date] forKey:@"createDate"]; | |
[dataRecord setValue:[NSDate date] forKey:@"updateDate"]; | |
NSError *error; | |
if (![context save:&error]) { | |
NSLog(@"Error:%@", error); | |
} | |
NSLog(@"Data saved"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
self.txtInfo.delegate = self; | |
self.txtName.delegate = self; | |
self.txtNumber.delegate = self; | |
} | |
- (BOOL)textFieldShouldReturn:(UITextField *)textField { | |
[textField resignFirstResponder]; | |
return YES; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface ViewController : UIViewController <UITextFieldDelegate> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface ViewController : UIViewController | |
@property (weak, nonatomic) IBOutlet UITextField *txtName; | |
@property (weak, nonatomic) IBOutlet UITextField *txtInfo; | |
@property (weak, nonatomic) IBOutlet UITextField *txtNumber; | |
@property (weak, nonatomic) IBOutlet UILabel *lblCreateDate; | |
@property (weak, nonatomic) IBOutlet UILabel *lblUpdateDate; | |
- (IBAction)tappedSaveAsNew:(id)sender; | |
- (IBAction)tappedLoadData:(id)sender; | |
- (IBAction)tappedNext:(id)sender; | |
- (IBAction)tappedPrevious:(id)sender; | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment