Renames old form field class and delegate.
Adds new form field for integers.
This commit is contained in:
@@ -8,11 +8,11 @@
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
#import "Monster.h"
|
||||
#import "EditableShortStringTableViewCell.h"
|
||||
#import "MCShortStringFieldTableViewCell.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface EditMonsterViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, EditableShortStringDelegate>
|
||||
@interface EditMonsterViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, MCFormFieldDelegate>
|
||||
|
||||
@property Monster* originalMonster;
|
||||
@property (weak, nonatomic) IBOutlet UITableView *monsterTableView;
|
||||
|
||||
@@ -7,7 +7,8 @@
|
||||
//
|
||||
|
||||
#import "EditMonsterViewController.h"
|
||||
#import "EditableShortStringTableViewCell.h"
|
||||
#import "MCShortStringFieldTableViewCell.h"
|
||||
#import "MCIntegerFieldTableViewCell.h"
|
||||
#import "AppDelegate.h"
|
||||
|
||||
@interface EditMonsterViewController ()
|
||||
@@ -16,6 +17,9 @@
|
||||
|
||||
@end
|
||||
|
||||
const int kSectionIndexBasicInfo = 0;
|
||||
const int kSectionIndexAbilityScores = 1;
|
||||
|
||||
@implementation EditMonsterViewController {
|
||||
NSManagedObjectContext *_context;
|
||||
}
|
||||
@@ -34,6 +38,24 @@
|
||||
self.editingMonster = [[Monster alloc] initWithMonster:self.originalMonster];
|
||||
}
|
||||
|
||||
- (MCShortStringFieldTableViewCell*) makeShortStringCellFromCell:(UITableViewCell*)cell {
|
||||
if (cell == nil || ![cell isKindOfClass:[MCShortStringFieldTableViewCell class]]) {
|
||||
// TODO: Figure out how to make this cell generate child views.
|
||||
return [[MCShortStringFieldTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MCShortStringField"];
|
||||
} else {
|
||||
return (MCShortStringFieldTableViewCell*)cell;
|
||||
}
|
||||
}
|
||||
|
||||
- (MCIntegerFieldTableViewCell*) makeIntegerCellFromCell:(UITableViewCell*)cell {
|
||||
if (cell == nil || ![cell isKindOfClass:[MCIntegerFieldTableViewCell class]]) {
|
||||
// TODO: Figure out how to make this cell generate child views.
|
||||
return [[MCIntegerFieldTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MCIntegerField"];
|
||||
} else {
|
||||
return (MCIntegerFieldTableViewCell*)cell;
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - Navigation
|
||||
|
||||
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
|
||||
@@ -52,65 +74,80 @@
|
||||
#pragma mark - UITableViewDataSource
|
||||
|
||||
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
|
||||
// Section 0 is basic info
|
||||
// * Name
|
||||
// * Size
|
||||
// * Type
|
||||
// * Subtype
|
||||
// * Alignment
|
||||
|
||||
return 5;
|
||||
switch(section) {
|
||||
case kSectionIndexBasicInfo:
|
||||
// Section 0 is basic info
|
||||
// * Name
|
||||
// * Size
|
||||
// * Type
|
||||
// * Subtype
|
||||
// * Alignment
|
||||
return 5;
|
||||
case kSectionIndexAbilityScores:
|
||||
return 0;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
- (EditableShortStringTableViewCell*) makeShortStringCellFromCell:(UITableViewCell*)cell {
|
||||
if (cell == nil || ![cell isKindOfClass:[EditableShortStringTableViewCell class]]) {
|
||||
// TODO: Figure out why this doesn't create a cell with a text field.
|
||||
return [[EditableShortStringTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"EditableShortString"];
|
||||
} else {
|
||||
return (EditableShortStringTableViewCell*)cell;
|
||||
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
|
||||
- (NSString *)tableView:(UITableView *)tableView
|
||||
titleForHeaderInSection:(NSInteger)section {
|
||||
switch(section) {
|
||||
case kSectionIndexBasicInfo:
|
||||
return NSLocalizedString(@"Basic Info", @"Section title");
|
||||
case kSectionIndexAbilityScores:
|
||||
return NSLocalizedString(@"Ability Scores", @"Section title");
|
||||
default:
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
|
||||
|
||||
EditableShortStringTableViewCell *shortStringCell = nil;
|
||||
MCShortStringFieldTableViewCell *shortStringCell = nil;
|
||||
MCIntegerFieldTableViewCell *integerCell = nil;
|
||||
|
||||
switch (indexPath.section) {
|
||||
case 0:
|
||||
case kSectionIndexBasicInfo:
|
||||
switch (indexPath.row) {
|
||||
case 0:
|
||||
shortStringCell = [self makeShortStringCellFromCell:[self.monsterTableView dequeueReusableCellWithIdentifier:@"EditableShortString"]];
|
||||
shortStringCell = [self makeShortStringCellFromCell:[self.monsterTableView dequeueReusableCellWithIdentifier:@"MCShortStringField"]];
|
||||
shortStringCell.delegate = self;
|
||||
shortStringCell.identifier = @"monster.name";
|
||||
// TODO: make these use setters on EditableShortStringTableViewCell
|
||||
// TODO: make these use setters on MCShortStringFieldTableViewCell
|
||||
shortStringCell.textField.text = self.editingMonster.name;
|
||||
shortStringCell.textField.placeholder = NSLocalizedString(@"Name", @"Placeholder text for the name of a monster or NPC.");
|
||||
return shortStringCell;
|
||||
case 1:
|
||||
shortStringCell = [self makeShortStringCellFromCell:[self.monsterTableView dequeueReusableCellWithIdentifier:@"EditableShortString"]];
|
||||
shortStringCell = [self makeShortStringCellFromCell:[self.monsterTableView dequeueReusableCellWithIdentifier:@"MCShortStringField"]];
|
||||
shortStringCell.delegate = self;
|
||||
shortStringCell.identifier = @"monster.size";
|
||||
// TODO: make these use setters on EditableShortStringTableViewCell
|
||||
// TODO: make these use setters on MCShortStringFieldTableViewCell
|
||||
shortStringCell.textField.text = self.editingMonster.size;
|
||||
shortStringCell.textField.placeholder = NSLocalizedString(@"Size", @"Placehodler text for the size of a monster or NPC.");
|
||||
return shortStringCell;
|
||||
case 2:
|
||||
shortStringCell = [self makeShortStringCellFromCell:[self.monsterTableView dequeueReusableCellWithIdentifier:@"EditableShortString"]];
|
||||
shortStringCell = [self makeShortStringCellFromCell:[self.monsterTableView dequeueReusableCellWithIdentifier:@"MCShortStringField"]];
|
||||
shortStringCell.delegate = self;
|
||||
shortStringCell.identifier = @"monster.type";
|
||||
// TODO: make these use setters on EditableShortStringTableViewCell
|
||||
// TODO: make these use setters on MCShortStringFieldTableViewCell
|
||||
shortStringCell.textField.text = self.editingMonster.type;
|
||||
shortStringCell.textField.placeholder = NSLocalizedString(@"Type", @"Placehodler text for the type of a monster or NPC.");
|
||||
return shortStringCell;
|
||||
case 3:
|
||||
shortStringCell = [self makeShortStringCellFromCell:[self.monsterTableView dequeueReusableCellWithIdentifier:@"EditableShortString"]];
|
||||
shortStringCell = [self makeShortStringCellFromCell:[self.monsterTableView dequeueReusableCellWithIdentifier:@"MCShortStringField"]];
|
||||
shortStringCell.delegate = self;
|
||||
shortStringCell.identifier = @"monster.subtype";
|
||||
shortStringCell.textField.text = self.editingMonster.subtype;
|
||||
shortStringCell.textField.placeholder = NSLocalizedString(@"Subtype", @"Placeholder text for the subtype of a monster or NPC.");
|
||||
return shortStringCell;
|
||||
case 4:
|
||||
shortStringCell = [self makeShortStringCellFromCell: [self.monsterTableView dequeueReusableCellWithIdentifier:@"EditableShortString"]];
|
||||
shortStringCell = [self makeShortStringCellFromCell: [self.monsterTableView dequeueReusableCellWithIdentifier:@"MCShortStringField"]];
|
||||
shortStringCell.delegate = self;
|
||||
shortStringCell.identifier = @"monster.alignment";
|
||||
shortStringCell.textField.text = self.editingMonster.alignment;
|
||||
@@ -120,13 +157,18 @@
|
||||
break;
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
NSLog(@"ERROR: Unable to build a cell for %@", indexPath);
|
||||
return nil;
|
||||
#else
|
||||
return [[UITableViewCell alloc] init];
|
||||
#endif
|
||||
}
|
||||
|
||||
#pragma mark - EditableShortStringDelegate
|
||||
#pragma mark - MCShortStringFieldDelegate
|
||||
|
||||
- (void)editableValueDidChange:(NSObject*)value forIdentifier:(NSString*)identifier andType:(NSString*)type {
|
||||
if ([@"String" isEqualToString:type]) {
|
||||
if ([kMCFieldValueTypeString isEqualToString:type]) {
|
||||
if ([@"monster.name" isEqualToString:identifier]) {
|
||||
self.editingMonster.name = (NSString*)value;
|
||||
} else if ([@"monster.size" isEqualToString:identifier]) {
|
||||
@@ -139,6 +181,11 @@
|
||||
self.editingMonster.alignment = (NSString*)value;
|
||||
}
|
||||
}
|
||||
if ([kMCFieldValueTypeInteger isEqualToString:type]) {
|
||||
if ([@"monster.strengthScore" isEqualToString:identifier]) {
|
||||
self.editingMonster.strengthScore = [(NSNumber*)value intValue];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
//
|
||||
// EditableShortStringTableViewCell.m
|
||||
// MonsterCards
|
||||
//
|
||||
// Created by Tom Hicks on 9/9/20.
|
||||
// Copyright © 2020 Tom Hicks. All rights reserved.
|
||||
//
|
||||
|
||||
#import "EditableShortStringTableViewCell.h"
|
||||
|
||||
@implementation EditableShortStringTableViewCell
|
||||
|
||||
- (void)awakeFromNib {
|
||||
[super awakeFromNib];
|
||||
// Initialization code
|
||||
self.textField.delegate = self;
|
||||
}
|
||||
|
||||
#pragma mark - UITextFieldDelegate
|
||||
|
||||
- (BOOL)textField:(UITextField *)textField
|
||||
shouldChangeCharactersInRange:(NSRange)range
|
||||
replacementString:(NSString *)string {
|
||||
// TODO: See this link for a potentially better way to get this text https://stackoverflow.com/questions/19110617/uitextfieldtextdidchangenotification-ios7-not-fired
|
||||
NSString *finalString = [textField.text stringByReplacingCharactersInRange:range withString:string];
|
||||
if (self.delegate != nil) {
|
||||
[self.delegate editableValueDidChange:finalString forIdentifier:self.identifier andType:@"String"];
|
||||
}
|
||||
return YES;
|
||||
}
|
||||
|
||||
@end
|
||||
15
iOS/MonsterCards/Views/FormFields/MCFormFieldConstants.h
Normal file
15
iOS/MonsterCards/Views/FormFields/MCFormFieldConstants.h
Normal file
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// MCFormFieldConstants.h
|
||||
// MonsterCards
|
||||
//
|
||||
// Created by Tom Hicks on 9/17/20.
|
||||
// Copyright © 2020 Tom Hicks. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef MCFormFieldConstants_h
|
||||
#define MCFormFieldConstants_h
|
||||
|
||||
extern NSString* const kMCFieldValueTypeInteger;
|
||||
extern NSString* const kMCFieldValueTypeString;
|
||||
|
||||
#endif /* MCFormFieldConstants_h */
|
||||
13
iOS/MonsterCards/Views/FormFields/MCFormFieldConstants.m
Normal file
13
iOS/MonsterCards/Views/FormFields/MCFormFieldConstants.m
Normal file
@@ -0,0 +1,13 @@
|
||||
//
|
||||
// MCFormFieldConstants.m
|
||||
// MonsterCards
|
||||
//
|
||||
// Created by Tom Hicks on 9/17/20.
|
||||
// Copyright © 2020 Tom Hicks. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "MCFormFieldConstants.h"
|
||||
|
||||
NSString* const kMCFieldValueTypeInteger = @"Integer";
|
||||
NSString* const kMCFieldValueTypeString = @"String";
|
||||
@@ -1,15 +1,17 @@
|
||||
//
|
||||
// EditableFormFieldDelegate.h
|
||||
// MCFormFieldDelegate.h
|
||||
// MonsterCards
|
||||
//
|
||||
// Created by Tom Hicks on 9/9/20.
|
||||
// Copyright © 2020 Tom Hicks. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef EditableFormFieldDelegate_h
|
||||
#define EditableFormFieldDelegate_h
|
||||
#ifndef MCFormFieldDelegate_h
|
||||
#define MCFormFieldDelegate_h
|
||||
|
||||
@protocol EditableFormFieldDelegate <NSObject>
|
||||
#import "MCFormFieldConstants.h"
|
||||
|
||||
@protocol MCFormFieldDelegate <NSObject>
|
||||
|
||||
@optional
|
||||
-(void)editableValueDidChange:(NSObject*)value forIdentifier:(NSString*)identifier andType:(NSString*)type;
|
||||
@@ -17,4 +19,4 @@
|
||||
@end
|
||||
|
||||
|
||||
#endif /* EditableFormFieldDelegate_h */
|
||||
#endif /* MCFormFieldDelegate_h */
|
||||
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// MCIntegerFieldTableViewCell.h
|
||||
// MonsterCards
|
||||
//
|
||||
// Created by Tom Hicks on 9/17/20.
|
||||
// Copyright © 2020 Tom Hicks. All rights reserved.
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
#import "MCFormFieldDelegate.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface MCIntegerFieldTableViewCell : UITableViewCell <UITextFieldDelegate>
|
||||
|
||||
@property NSString* identifier;
|
||||
@property NSString* label;
|
||||
@property int value;
|
||||
|
||||
@property (weak, nonatomic) id<MCFormFieldDelegate> delegate;
|
||||
@property (weak, nonatomic) IBOutlet UITextField *textField;
|
||||
@property (weak, nonatomic) IBOutlet UIStepper *stepper;
|
||||
- (IBAction)stepperValueChanged:(id)sender;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// MCIntegerFieldTableViewCell.m
|
||||
// MonsterCards
|
||||
//
|
||||
// Created by Tom Hicks on 9/17/20.
|
||||
// Copyright © 2020 Tom Hicks. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MCIntegerFieldTableViewCell.h"
|
||||
|
||||
@implementation MCIntegerFieldTableViewCell
|
||||
|
||||
@synthesize value = _value;
|
||||
|
||||
-(void) setValue:(int)number {
|
||||
if (_value != number) {
|
||||
NSNumber *newValue = [NSNumber numberWithInt:number];
|
||||
_value = number;
|
||||
if (self.textField) {
|
||||
self.textField.text = [newValue stringValue];
|
||||
}
|
||||
if (self.stepper) {
|
||||
self.stepper.value = number;
|
||||
}
|
||||
if (self.delegate) {
|
||||
[self.delegate editableValueDidChange:newValue
|
||||
forIdentifier:self.identifier
|
||||
andType:kMCFieldValueTypeInteger];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (int) value {
|
||||
return _value;
|
||||
}
|
||||
|
||||
- (void)awakeFromNib {
|
||||
[super awakeFromNib];
|
||||
[self.textField addTarget:self
|
||||
action:@selector(textFieldValueChanged:)
|
||||
forControlEvents:UIControlEventEditingChanged];
|
||||
}
|
||||
|
||||
- (void)textFieldValueChanged:(UITextField*)textField {
|
||||
self.value = [textField.text intValue];
|
||||
}
|
||||
|
||||
- (IBAction)stepperValueChanged:(id)sender {
|
||||
self.value = self.stepper.value;
|
||||
}
|
||||
@end
|
||||
@@ -7,22 +7,17 @@
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
#import "MCFormFieldDelegate.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@protocol EditableShortStringDelegate <NSObject>
|
||||
|
||||
@optional
|
||||
-(void)editableValueDidChange:(NSString*)value forIdentifier:(NSString*)identifier andType:(NSString*)type;
|
||||
|
||||
@end
|
||||
|
||||
@interface EditableShortStringTableViewCell : UITableViewCell <UITextFieldDelegate>
|
||||
@interface MCShortStringFieldTableViewCell : UITableViewCell
|
||||
|
||||
@property NSString* identifier;
|
||||
@property NSString* label;
|
||||
@property NSString* value;
|
||||
@property (nonatomic, weak) id<EditableShortStringDelegate> delegate;
|
||||
|
||||
@property (weak, nonatomic) id<MCFormFieldDelegate> delegate;
|
||||
@property (weak, nonatomic) IBOutlet UITextField *textField;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// EditableShortStringTableViewCell.m
|
||||
// MonsterCards
|
||||
//
|
||||
// Created by Tom Hicks on 9/9/20.
|
||||
// Copyright © 2020 Tom Hicks. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MCShortStringFieldTableViewCell.h"
|
||||
|
||||
@implementation MCShortStringFieldTableViewCell
|
||||
|
||||
- (void)awakeFromNib {
|
||||
[super awakeFromNib];
|
||||
[self.textField addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventEditingChanged];
|
||||
}
|
||||
|
||||
- (void)valueChanged:(UITextField*)textField {
|
||||
NSString *newValue = textField.text;
|
||||
if (self.delegate != nil) {
|
||||
[self.delegate editableValueDidChange:newValue
|
||||
forIdentifier:self.identifier
|
||||
andType:kMCFieldValueTypeString];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user