Makes the library view display a list of monsters.

Makes the library view ad search view share a view controller for their destination.
This commit is contained in:
2020-09-11 23:39:50 -07:00
parent 314906f74d
commit b1fbc169dc
6 changed files with 165 additions and 36 deletions

View File

@@ -10,7 +10,9 @@
NS_ASSUME_NONNULL_BEGIN
@interface LibraryViewController : UIViewController
@interface LibraryViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *monstersTable;
@end

View File

@@ -7,26 +7,68 @@
//
#import "LibraryViewController.h"
#import "Monster.h"
#import "MonsterViewController.h"
@interface LibraryViewController ()
@property NSArray* allMonsters;
@end
@implementation LibraryViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// Temporary setup of allMonsters until we bind to CoreData.
Monster *pixie = [[Monster alloc] initWithJSONString:@"{\"name\":\"Pixie\"}"];
Monster *acolyte = [[Monster alloc] initWithJSONString:@"{\"name\":\"Acolyte\"}"];
self.allMonsters = [NSArray arrayWithObjects:acolyte, pixie, nil];
}
- (void)viewWillAppear:(BOOL)animated {
// TODO: fetch monsters from CoreData
[self.monstersTable reloadData];
}
- (IBAction)addNewMonster:(id)sender {
Monster *monster = [[Monster alloc] init];
monster.name = @"Unnamed Monster";
self.allMonsters = [self.allMonsters arrayByAddingObject:monster];
[self.monstersTable reloadData];
}
/*
#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 {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if ([@"ShowMonsterDetail" isEqualToString:segue.identifier]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
MonsterViewController *vc = (MonsterViewController*)segue.destinationViewController;
vc.monster = [self.allMonsters objectAtIndex:indexPath.row];
}
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
return [self.allMonsters count];
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"MonsterCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
Monster *monster = (Monster*)[self.allMonsters objectAtIndex:indexPath.row];
cell.textLabel.text = monster.name;
return cell;
}
*/
@end

View File

@@ -18,9 +18,32 @@
[super viewDidLoad];
if (self.monsterName != nil) {
self.monsterName.text = self.monster.name;
} else if (self.navigationItem != nil && self.navigationItem.title != nil) {
} else if (self.navigationItem != nil) {
self.navigationItem.title = self.monster.name;
}
}
- (void)viewWillAppear:(BOOL)animated {
// TODO: get the latest version of this monster from CoreData
if (self.monsterName != nil) {
self.monsterName.text = self.monster.name;
} else if (self.navigationItem != nil) {
if (self.monster.name == nil) {
self.navigationItem.title = @"Unnamed Monster";
} else {
self.navigationItem.title = self.monster.name;
}
}
}
- (IBAction)unwindWithSegue:(UIStoryboardSegue *)unwindSegue {
// UIViewController *sourceViewController = unwindSegue.sourceViewController;
// Use data from the view controller which initiated the unwind segue
}
#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
}
@end

View File

@@ -29,14 +29,13 @@
#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 {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if ([@"ShowMonsterDetail" isEqualToString:segue.identifier]) {
NSIndexPath *indexPath = [self.searchResults indexPathForSelectedRow];
MonsterViewController *vc = (MonsterViewController*)segue.destinationViewController;
vc.monster = [self.foundMonsters objectAtIndex:indexPath.row];
if ([segue.destinationViewController isKindOfClass:[MonsterViewController class]]) {
MonsterViewController *vc = (MonsterViewController*)segue.destinationViewController;
vc.monster = [self.foundMonsters objectAtIndex:indexPath.row];
}
}
}