Makes views use monsters from CoreData instead of hard coded ones.
This commit is contained in:
@@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
#import "EditMonsterViewController.h"
|
#import "EditMonsterViewController.h"
|
||||||
#import "EditableShortStringTableViewCell.h"
|
#import "EditableShortStringTableViewCell.h"
|
||||||
|
#import "AppDelegate.h"
|
||||||
|
|
||||||
@interface EditMonsterViewController ()
|
@interface EditMonsterViewController ()
|
||||||
|
|
||||||
@@ -15,10 +16,15 @@
|
|||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation EditMonsterViewController
|
@implementation EditMonsterViewController {
|
||||||
|
NSManagedObjectContext *_context;
|
||||||
|
}
|
||||||
|
|
||||||
- (void)viewDidLoad {
|
- (void)viewDidLoad {
|
||||||
[super viewDidLoad];
|
[super viewDidLoad];
|
||||||
|
AppDelegate *appDelegate = (AppDelegate*)UIApplication.sharedApplication.delegate;
|
||||||
|
_context = appDelegate.persistentContainer.viewContext;
|
||||||
|
|
||||||
self.monsterTableView.dataSource = self;
|
self.monsterTableView.dataSource = self;
|
||||||
self.monsterTableView.delegate = self;
|
self.monsterTableView.delegate = self;
|
||||||
}
|
}
|
||||||
@@ -32,9 +38,12 @@
|
|||||||
|
|
||||||
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
|
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
|
||||||
if ([@"DiscardChanges" isEqualToString:segue.identifier]) {
|
if ([@"DiscardChanges" isEqualToString:segue.identifier]) {
|
||||||
|
[_context rollback];
|
||||||
} else if ([@"SaveChanges" isEqualToString:segue.identifier]) {
|
} else if ([@"SaveChanges" isEqualToString:segue.identifier]) {
|
||||||
// TODO: this should use a method on originalMonster to copy values from editingMonster or pass the new monster back some way. Core Data would save and probably trigger a refresh in the receiving view.
|
// TODO: this should use a method on originalMonster to copy values from editingMonster or pass the new monster back some way. Core Data would save and probably trigger a refresh in the receiving view.
|
||||||
self.originalMonster.name = self.editingMonster.name;
|
self.originalMonster.name = self.editingMonster.name;
|
||||||
|
[_context refreshObject:self.editingMonster mergeChanges:NO];
|
||||||
|
[_context save:nil];
|
||||||
} else {
|
} else {
|
||||||
NSLog(@"Unknown Segue %@", segue.identifier);
|
NSLog(@"Unknown Segue %@", segue.identifier);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,36 +17,32 @@
|
|||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation LibraryViewController
|
@implementation LibraryViewController {
|
||||||
|
NSManagedObjectContext *_context;
|
||||||
|
}
|
||||||
|
|
||||||
- (void)viewDidLoad {
|
- (void)viewDidLoad {
|
||||||
[super viewDidLoad];
|
[super viewDidLoad];
|
||||||
AppDelegate *appDelegate = (AppDelegate*)UIApplication.sharedApplication.delegate;
|
AppDelegate *appDelegate = (AppDelegate*)UIApplication.sharedApplication.delegate;
|
||||||
NSManagedObjectContext *context = appDelegate.persistentContainer.viewContext;
|
_context = appDelegate.persistentContainer.viewContext;
|
||||||
|
|
||||||
// Temporary setup of allMonsters until we bind to CoreData.
|
|
||||||
Monster *pixie = [[Monster alloc] initWithJSONString:@"{\"name\":\"Pixie\"}" andContext:context];
|
|
||||||
Monster *acolyte = [[Monster alloc] initWithJSONString:@"{\"name\":\"Acolyte\"}" andContext:context];
|
|
||||||
self.allMonsters = [NSArray arrayWithObjects:acolyte, pixie, nil];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)viewWillAppear:(BOOL)animated {
|
- (void)viewWillAppear:(BOOL)animated {
|
||||||
// TODO: fetch monsters from CoreData
|
self.allMonsters = [_context executeFetchRequest:[Monster fetchRequest] error:nil];
|
||||||
[self.monstersTable reloadData];
|
[self.monstersTable reloadData];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (IBAction)addNewMonster:(id)sender {
|
- (IBAction)addNewMonster:(id)sender {
|
||||||
AppDelegate *appDelegate = (AppDelegate*)UIApplication.sharedApplication.delegate;
|
Monster *monster = [[Monster alloc] initWithContext:_context];
|
||||||
NSManagedObjectContext *context = appDelegate.persistentContainer.viewContext;
|
|
||||||
Monster *monster = [[Monster alloc] initWithContext:context];
|
|
||||||
monster.name = @"Unnamed Monster";
|
monster.name = @"Unnamed Monster";
|
||||||
self.allMonsters = [self.allMonsters arrayByAddingObject:monster];
|
self.allMonsters = [self.allMonsters arrayByAddingObject:monster];
|
||||||
|
//DispatchQueue.main.async{"code here"}
|
||||||
|
[_context save:nil];
|
||||||
[self.monstersTable reloadData];
|
[self.monstersTable reloadData];
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - Navigation
|
#pragma mark - Navigation
|
||||||
|
|
||||||
// In a storyboard-based application, you will often want to do a little preparation before navigation
|
|
||||||
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
|
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
|
||||||
if ([@"ShowMonsterDetail" isEqualToString:segue.identifier]) {
|
if ([@"ShowMonsterDetail" isEqualToString:segue.identifier]) {
|
||||||
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
|
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
|
||||||
|
|||||||
@@ -18,17 +18,18 @@
|
|||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation SearchViewController
|
@implementation SearchViewController {
|
||||||
|
NSManagedObjectContext *_context;
|
||||||
|
}
|
||||||
|
|
||||||
- (void)viewDidLoad {
|
- (void)viewDidLoad {
|
||||||
[super viewDidLoad];
|
[super viewDidLoad];
|
||||||
AppDelegate *appDelegate = (AppDelegate*)UIApplication.sharedApplication.delegate;
|
AppDelegate *appDelegate = (AppDelegate*)UIApplication.sharedApplication.delegate;
|
||||||
NSManagedObjectContext *context = appDelegate.persistentContainer.viewContext;
|
_context = appDelegate.persistentContainer.viewContext;
|
||||||
Monster *pixie = [[Monster alloc] initWithContext:context];
|
}
|
||||||
pixie.name = @"Pixie";
|
|
||||||
Monster *acolyte = [[Monster alloc] initWithEntity:[NSEntityDescription entityForName:@"Monster" inManagedObjectContext:context] insertIntoManagedObjectContext:nil];
|
- (void)viewWillAppear:(BOOL)animated {
|
||||||
acolyte.name = @"Acolyte";
|
self.allMonsters = [_context executeFetchRequest:[Monster fetchRequest] error:nil];
|
||||||
self.allMonsters = [NSArray arrayWithObjects:acolyte, pixie, nil];
|
|
||||||
self.foundMonsters= self.allMonsters;
|
self.foundMonsters= self.allMonsters;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user