Adds select field with picker as TextField inputView.

This commit is contained in:
2020-09-26 20:36:31 -07:00
parent 57bf1f2e3a
commit 0912ac0fd8
12 changed files with 402 additions and 10 deletions

View File

@@ -34,6 +34,8 @@
E2D3E3B0250827110052A8EC /* EditMonsterViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = E2D3E3AF250827110052A8EC /* EditMonsterViewController.m */; }; E2D3E3B0250827110052A8EC /* EditMonsterViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = E2D3E3AF250827110052A8EC /* EditMonsterViewController.m */; };
E2D3E3B42508C3360052A8EC /* MCShortStringFieldTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = E2D3E3B32508C3360052A8EC /* MCShortStringFieldTableViewCell.m */; }; E2D3E3B42508C3360052A8EC /* MCShortStringFieldTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = E2D3E3B32508C3360052A8EC /* MCShortStringFieldTableViewCell.m */; };
E2E25805250CC3A7002E7308 /* MonsterCards.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = E2F7248225005E89007D87ED /* MonsterCards.xcdatamodeld */; }; E2E25805250CC3A7002E7308 /* MonsterCards.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = E2F7248225005E89007D87ED /* MonsterCards.xcdatamodeld */; };
E2E90AFF252015B3005241C8 /* MCSelectFieldTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = E2E90AFE252015B3005241C8 /* MCSelectFieldTableViewCell.m */; };
E2E90B0525201785005241C8 /* MCChoice.m in Sources */ = {isa = PBXBuildFile; fileRef = E2E90B0425201785005241C8 /* MCChoice.m */; };
E2ECA8F32504AC3300C1FFA5 /* SkillTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E2ECA8F22504AC3300C1FFA5 /* SkillTests.m */; }; E2ECA8F32504AC3300C1FFA5 /* SkillTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E2ECA8F22504AC3300C1FFA5 /* SkillTests.m */; };
E2ECA8F52504BAAD00C1FFA5 /* MonsterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E2ECA8F42504BAAD00C1FFA5 /* MonsterTests.m */; }; E2ECA8F52504BAAD00C1FFA5 /* MonsterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E2ECA8F42504BAAD00C1FFA5 /* MonsterTests.m */; };
E2F7247525005E89007D87ED /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = E2F7247425005E89007D87ED /* AppDelegate.m */; }; E2F7247525005E89007D87ED /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = E2F7247425005E89007D87ED /* AppDelegate.m */; };
@@ -122,6 +124,10 @@
E2D3E3AF250827110052A8EC /* EditMonsterViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EditMonsterViewController.m; sourceTree = "<group>"; }; E2D3E3AF250827110052A8EC /* EditMonsterViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EditMonsterViewController.m; sourceTree = "<group>"; };
E2D3E3B22508C3360052A8EC /* MCShortStringFieldTableViewCell.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MCShortStringFieldTableViewCell.h; sourceTree = "<group>"; }; E2D3E3B22508C3360052A8EC /* MCShortStringFieldTableViewCell.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MCShortStringFieldTableViewCell.h; sourceTree = "<group>"; };
E2D3E3B32508C3360052A8EC /* MCShortStringFieldTableViewCell.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MCShortStringFieldTableViewCell.m; sourceTree = "<group>"; }; E2D3E3B32508C3360052A8EC /* MCShortStringFieldTableViewCell.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MCShortStringFieldTableViewCell.m; sourceTree = "<group>"; };
E2E90AFD252015B3005241C8 /* MCSelectFieldTableViewCell.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MCSelectFieldTableViewCell.h; sourceTree = "<group>"; };
E2E90AFE252015B3005241C8 /* MCSelectFieldTableViewCell.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MCSelectFieldTableViewCell.m; sourceTree = "<group>"; };
E2E90B0325201785005241C8 /* MCChoice.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MCChoice.h; sourceTree = "<group>"; };
E2E90B0425201785005241C8 /* MCChoice.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MCChoice.m; sourceTree = "<group>"; };
E2ECA8F22504AC3300C1FFA5 /* SkillTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SkillTests.m; sourceTree = "<group>"; }; E2ECA8F22504AC3300C1FFA5 /* SkillTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SkillTests.m; sourceTree = "<group>"; };
E2ECA8F42504BAAD00C1FFA5 /* MonsterTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MonsterTests.m; sourceTree = "<group>"; }; E2ECA8F42504BAAD00C1FFA5 /* MonsterTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MonsterTests.m; sourceTree = "<group>"; };
E2F7247025005E89007D87ED /* Monster Cards.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Monster Cards.app"; sourceTree = BUILT_PRODUCTS_DIR; }; E2F7247025005E89007D87ED /* Monster Cards.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Monster Cards.app"; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -276,12 +282,16 @@
E2591EB62509DD4900B396FD /* MCFormFieldDelegate.h */, E2591EB62509DD4900B396FD /* MCFormFieldDelegate.h */,
E288744E25148BAD005CA948 /* MCFormFieldConstants.h */, E288744E25148BAD005CA948 /* MCFormFieldConstants.h */,
E288744925148BA0005CA948 /* MCFormFieldConstants.m */, E288744925148BA0005CA948 /* MCFormFieldConstants.m */,
E20C315225146708003AB1AA /* MCIntegerFieldTableViewCell.h */, E2E90B0325201785005241C8 /* MCChoice.h */,
E20C315325146708003AB1AA /* MCIntegerFieldTableViewCell.m */, E2E90B0425201785005241C8 /* MCChoice.m */,
E2D3E3B22508C3360052A8EC /* MCShortStringFieldTableViewCell.h */,
E2D3E3B32508C3360052A8EC /* MCShortStringFieldTableViewCell.m */,
E2805512251E03BE00C87527 /* MCBooleanFieldTableViewCell.h */, E2805512251E03BE00C87527 /* MCBooleanFieldTableViewCell.h */,
E2805513251E03BE00C87527 /* MCBooleanFieldTableViewCell.m */, E2805513251E03BE00C87527 /* MCBooleanFieldTableViewCell.m */,
E20C315225146708003AB1AA /* MCIntegerFieldTableViewCell.h */,
E20C315325146708003AB1AA /* MCIntegerFieldTableViewCell.m */,
E2E90AFD252015B3005241C8 /* MCSelectFieldTableViewCell.h */,
E2E90AFE252015B3005241C8 /* MCSelectFieldTableViewCell.m */,
E2D3E3B22508C3360052A8EC /* MCShortStringFieldTableViewCell.h */,
E2D3E3B32508C3360052A8EC /* MCShortStringFieldTableViewCell.m */,
); );
path = FormFields; path = FormFields;
sourceTree = "<group>"; sourceTree = "<group>";
@@ -623,10 +633,12 @@
E25BD5F8250368A8007B04EF /* SavingThrow.m in Sources */, E25BD5F8250368A8007B04EF /* SavingThrow.m in Sources */,
E2F7248C25005E8A007D87ED /* main.m in Sources */, E2F7248C25005E8A007D87ED /* main.m in Sources */,
E25BD5FB250369D7007B04EF /* Skill.m in Sources */, E25BD5FB250369D7007B04EF /* Skill.m in Sources */,
E2E90B0525201785005241C8 /* MCChoice.m in Sources */,
E2F7247825005E89007D87ED /* SceneDelegate.m in Sources */, E2F7247825005E89007D87ED /* SceneDelegate.m in Sources */,
E20D032425031B9D00FB6E43 /* SearchViewController.m in Sources */, E20D032425031B9D00FB6E43 /* SearchViewController.m in Sources */,
E22F837C2511D14E0072105C /* JSONHelper.m in Sources */, E22F837C2511D14E0072105C /* JSONHelper.m in Sources */,
E25BD60125036BF8007B04EF /* Language.m in Sources */, E25BD60125036BF8007B04EF /* Language.m in Sources */,
E2E90AFF252015B3005241C8 /* MCSelectFieldTableViewCell.m in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };

View File

@@ -394,6 +394,45 @@
<outlet property="switchView" destination="L1a-6J-Yco" id="tTF-vZ-9gZ"/> <outlet property="switchView" destination="L1a-6J-Yco" id="tTF-vZ-9gZ"/>
</connections> </connections>
</tableViewCell> </tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" reuseIdentifier="MCSelectField" id="PQF-mC-UlI" userLabel="MCSelectField" customClass="MCSelectFieldTableViewCell">
<rect key="frame" x="0.0" y="158.5" width="414" height="37.5"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="PQF-mC-UlI" id="Wcu-Sa-z2O">
<rect key="frame" x="0.0" y="0.0" width="414" height="37.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="lik-sI-94j" userLabel="Label View">
<rect key="frame" x="20" y="8" width="42" height="21.5"/>
<constraints>
<constraint firstAttribute="height" constant="21" id="A1K-Wk-Nuf"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="DFa-Hd-laX" userLabel="Text Field">
<rect key="frame" x="82" y="2" width="312" height="34"/>
<constraints>
<constraint firstAttribute="height" constant="34" id="zHt-9x-mSt"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
</textField>
</subviews>
<constraints>
<constraint firstItem="DFa-Hd-laX" firstAttribute="centerY" secondItem="Wcu-Sa-z2O" secondAttribute="centerY" id="CAr-SD-zJQ"/>
<constraint firstItem="lik-sI-94j" firstAttribute="centerY" secondItem="Wcu-Sa-z2O" secondAttribute="centerY" id="NXK-tA-2fO"/>
<constraint firstItem="lik-sI-94j" firstAttribute="leading" secondItem="Wcu-Sa-z2O" secondAttribute="leading" constant="20" id="fea-zT-1Gp"/>
<constraint firstAttribute="trailing" secondItem="DFa-Hd-laX" secondAttribute="trailing" constant="20" id="k98-fH-Q1Z"/>
<constraint firstItem="DFa-Hd-laX" firstAttribute="leading" secondItem="lik-sI-94j" secondAttribute="trailing" constant="20" id="pF8-P8-BrC"/>
<constraint firstItem="lik-sI-94j" firstAttribute="top" secondItem="Wcu-Sa-z2O" secondAttribute="top" constant="8" id="tXM-6N-Dk1"/>
</constraints>
</tableViewCellContentView>
<connections>
<outlet property="labelView" destination="lik-sI-94j" id="TYU-vE-Agy"/>
<outlet property="textField" destination="DFa-Hd-laX" id="8fm-vK-dar"/>
</connections>
</tableViewCell>
</prototypes> </prototypes>
</tableView> </tableView>
</subviews> </subviews>

View File

@@ -15,7 +15,8 @@ NS_ASSUME_NONNULL_BEGIN
@property NSString* name; @property NSString* name;
@property BOOL speaks; @property BOOL speaks;
-(id)initWithName: (NSString*)name andSpeaks: (BOOL)canSpeak; -(id)initWithName:(NSString*)name
andSpeaks:(BOOL)canSpeak;
@end @end

View File

@@ -19,7 +19,8 @@
return self; return self;
} }
-(id)initWithName: (NSString*)name andSpeaks: (BOOL)canSpeak { -(id)initWithName:(NSString*)name
andSpeaks:(BOOL)canSpeak {
self = [super init]; self = [super init];
self.name = name; self.name = name;

View File

@@ -504,6 +504,10 @@ NSString* const kMonsterSizeGargantuan = @"gargantuan";
self.swimSpeed = monster.swimSpeed; self.swimSpeed = monster.swimSpeed;
self.hasCustomSpeed = monster.hasCustomSpeed; self.hasCustomSpeed = monster.hasCustomSpeed;
self.customSpeed = monster.customSpeed; self.customSpeed = monster.customSpeed;
self.armorType = monster.armorType;
self.naturalArmorBonus = monster.naturalArmorBonus;
self.hasShield = monster.hasShield;
self.customArmor = monster.customArmor;
} }
@end @end

View File

@@ -7,9 +7,10 @@
// //
#import "EditMonsterViewController.h" #import "EditMonsterViewController.h"
#import "MCShortStringFieldTableViewCell.h"
#import "MCIntegerFieldTableViewCell.h"
#import "MCBooleanFieldTableViewCell.h" #import "MCBooleanFieldTableViewCell.h"
#import "MCIntegerFieldTableViewCell.h"
#import "MCSelectFieldTableViewCell.h"
#import "MCShortStringFieldTableViewCell.h"
#import "AppDelegate.h" #import "AppDelegate.h"
@interface EditMonsterViewController () @interface EditMonsterViewController ()
@@ -55,12 +56,48 @@ const int kAbilityScoreSectionRowIndexCharisma = 5;
@implementation EditMonsterViewController { @implementation EditMonsterViewController {
NSManagedObjectContext *_context; NSManagedObjectContext *_context;
NSArray<MCChoice*>* _armorTypes;
} }
- (void)viewDidLoad { - (void)viewDidLoad {
[super viewDidLoad]; [super viewDidLoad];
AppDelegate *appDelegate = (AppDelegate*)UIApplication.sharedApplication.delegate; AppDelegate *appDelegate = (AppDelegate*)UIApplication.sharedApplication.delegate;
_context = appDelegate.persistentContainer.viewContext; _context = appDelegate.persistentContainer.viewContext;
_armorTypes = [NSArray arrayWithObjects:
[MCChoice choiceWithLabel:NSLocalizedString(@"None", @"")
andValue:kArmorNameNone],
[MCChoice choiceWithLabel:NSLocalizedString(@"Natural Armor", @"")
andValue:kArmorNameNaturalArmor],
[MCChoice choiceWithLabel:NSLocalizedString(@"Mage Armor", @"")
andValue:kArmorNameMageArmor],
[MCChoice choiceWithLabel:NSLocalizedString(@"Padded", @"")
andValue:kArmorNamePadded],
[MCChoice choiceWithLabel:NSLocalizedString(@"Leather", @"")
andValue:kArmorNameLeather],
[MCChoice choiceWithLabel:NSLocalizedString(@"Studded", @"")
andValue:kArmorNameStuddedLeather],
[MCChoice choiceWithLabel:NSLocalizedString(@"Hide", @"")
andValue:kArmorNameHide],
[MCChoice choiceWithLabel:NSLocalizedString(@"Chain Shirt", @"")
andValue:kArmorNameChainShirt],
[MCChoice choiceWithLabel:NSLocalizedString(@"Scale Mail", @"")
andValue:kArmorNameScaleMail],
[MCChoice choiceWithLabel:NSLocalizedString(@"Breastplate", @"")
andValue:kArmorNameBreastplate],
[MCChoice choiceWithLabel:NSLocalizedString(@"Half Plate", @"")
andValue:kArmorNameHalfPlate],
[MCChoice choiceWithLabel:NSLocalizedString(@"Ring Mail", @"")
andValue:kArmorNameRingMail],
[MCChoice choiceWithLabel:NSLocalizedString(@"Chain Mail", @"")
andValue:kArmorNameChainMail],
[MCChoice choiceWithLabel:NSLocalizedString(@"Splint", @"")
andValue:kArmorNameSplintMail],
[MCChoice choiceWithLabel:NSLocalizedString(@"Plate", @"")
andValue:kArmorNamePlateMail],
[MCChoice choiceWithLabel:NSLocalizedString(@"Other", @"")
andValue:kArmorNameOther],
nil];
self.monsterTableView.dataSource = self; self.monsterTableView.dataSource = self;
self.monsterTableView.delegate = self; self.monsterTableView.delegate = self;
@@ -131,6 +168,24 @@ const int kAbilityScoreSectionRowIndexCharisma = 5;
return cell; return cell;
} }
- (MCSelectFieldTableViewCell*) makeSelectCellFromTableView:(UITableView*)tableView
withIdentifier:(NSString*)identifier
label:(NSString*)label
initialValue:(NSObject*)initialValue
andChoices:(NSArray*)choices {
MCSelectFieldTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MCSelectField"];
if (!cell || ![cell isKindOfClass:[MCSelectFieldTableViewCell class]]) {
return nil;
}
cell.delegate = self;
cell.identifier = identifier;
cell.label = label;
cell.selectedValue = initialValue;
cell.choices = choices;
return cell;
}
#pragma mark - Navigation #pragma mark - Navigation
@@ -160,7 +215,7 @@ const int kAbilityScoreSectionRowIndexCharisma = 5;
// * Alignment // * Alignment
return 8; return 8;
case kSectionIndexArmor: case kSectionIndexArmor:
return 0; return 4;
case kSectionIndexSpeed: case kSectionIndexSpeed:
return 8; return 8;
case kSectionIndexAbilityScores: case kSectionIndexAbilityScores:
@@ -181,7 +236,7 @@ titleForHeaderInSection:(NSInteger)section {
case kSectionIndexBasicInfo: case kSectionIndexBasicInfo:
return NSLocalizedString(@"Basic Info", @"Section title"); return NSLocalizedString(@"Basic Info", @"Section title");
case kSectionIndexArmor: case kSectionIndexArmor:
return NSLocalizedString(@"Armor and HP", @"Section title"); return NSLocalizedString(@"Armor", @"Section title");
case kSectionIndexSpeed: case kSectionIndexSpeed:
return NSLocalizedString(@"Speed", @"Section title"); return NSLocalizedString(@"Speed", @"Section title");
case kSectionIndexAbilityScores: case kSectionIndexAbilityScores:
@@ -250,6 +305,31 @@ titleForHeaderInSection:(NSInteger)section {
break; break;
case kSectionIndexArmor: case kSectionIndexArmor:
switch (indexPath.row) { switch (indexPath.row) {
case kArmorSectionRowIndexArmorType:
newCell = [self makeSelectCellFromTableView:self.monsterTableView
withIdentifier:@"monster.armorType"
label:NSLocalizedString(@"Type", @"")
initialValue:self.editingMonster.armorType
andChoices:_armorTypes];
break;
case kArmorSectionRowIndexHasShield:
newCell = [self makeBooleanCellFromTableView:self.monsterTableView
withIdentifier:@"monster.hasShield"
label:NSLocalizedString(@"Shield", @"")
andInitialValue:self.editingMonster.hasShield];
break;
case kArmorSectionRowIndexCustomArmor:
newCell = [self makeShortStringCellFromTableView:self.monsterTableView
withIdentifier:@"monster.customArmor"
label:NSLocalizedString(@"Custom Armor", @"")
andInitialValue:self.editingMonster.customArmor];
break;
case kArmorSectionRowIndexNaturalArmorBonus:
newCell = [self makeIntegerCellFromTableView:self.monsterTableView
withIdentifier:@"monster.naturalArmorBonus"
label:NSLocalizedString(@"Natural Armor Bonus", @"")
andInitialValue:self.editingMonster.naturalArmorBonus];
break;
} }
break; break;
case kSectionIndexSpeed: case kSectionIndexSpeed:
@@ -373,6 +453,8 @@ titleForHeaderInSection:(NSInteger)section {
self.editingMonster.hpText = (NSString*)value; self.editingMonster.hpText = (NSString*)value;
} else if ([@"monster.customSpeed" isEqualToString:identifier]) { } else if ([@"monster.customSpeed" isEqualToString:identifier]) {
self.editingMonster.customSpeed = (NSString*)value; self.editingMonster.customSpeed = (NSString*)value;
} else if ([@"monster.customArmor" isEqualToString:identifier]) {
self.editingMonster.customArmor = (NSString*)value;
} }
} }
if ([kMCFieldValueTypeInteger isEqualToString:type]) { if ([kMCFieldValueTypeInteger isEqualToString:type]) {
@@ -400,6 +482,8 @@ titleForHeaderInSection:(NSInteger)section {
self.editingMonster.flySpeed = [(NSNumber*)value intValue]; self.editingMonster.flySpeed = [(NSNumber*)value intValue];
} else if ([@"monster.swimSpeed" isEqualToString:identifier]) { } else if ([@"monster.swimSpeed" isEqualToString:identifier]) {
self.editingMonster.swimSpeed = [(NSNumber*)value intValue]; self.editingMonster.swimSpeed = [(NSNumber*)value intValue];
} else if ([@"monster.naturalArmorBonus" isEqualToString:identifier]) {
self.editingMonster.naturalArmorBonus = [(NSNumber*)value intValue];
} }
} }
if ([kMCFieldValueTypeBoolean isEqualToString:type]) { if ([kMCFieldValueTypeBoolean isEqualToString:type]) {
@@ -409,6 +493,13 @@ titleForHeaderInSection:(NSInteger)section {
self.editingMonster.canHover = [(NSNumber*)value boolValue]; self.editingMonster.canHover = [(NSNumber*)value boolValue];
} else if ([@"monster.hasCustomSpeed" isEqualToString:identifier]) { } else if ([@"monster.hasCustomSpeed" isEqualToString:identifier]) {
self.editingMonster.hasCustomSpeed = [(NSNumber*)value boolValue]; self.editingMonster.hasCustomSpeed = [(NSNumber*)value boolValue];
} else if ([@"monster.hasShield" isEqualToString:identifier]) {
self.editingMonster.hasShield = [(NSNumber*)value boolValue];
}
}
if ([kMCFieldValueTypeChoice isEqualToString:type]) {
if ([@"monster.armorType" isEqualToString:identifier]) {
self.editingMonster.armorType = (NSString*)value;
} }
} }
} }

View File

@@ -0,0 +1,28 @@
//
// MCChoice.h
// MonsterCards
//
// Created by Tom Hicks on 9/26/20.
// Copyright © 2020 Tom Hicks. All rights reserved.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface MCChoice : NSObject
@property NSString* label;
@property NSObject* value;
+(id)choiceWithLabel:(NSString*)label
andValue:(NSObject*)value;
-(id)initWithLabel:(NSString*)label
andValue:(NSObject*)value;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,29 @@
//
// MCChoice.m
// MonsterCards
//
// Created by Tom Hicks on 9/26/20.
// Copyright © 2020 Tom Hicks. All rights reserved.
//
#import "MCChoice.h"
@implementation MCChoice
+(id)choiceWithLabel:(NSString*)label
andValue:(NSObject*)value {
return [[MCChoice alloc] initWithLabel:label
andValue:value];
}
-(id)initWithLabel:(NSString*)label
andValue:(NSObject*)value {
self = [super init];
self.label = label;
self.value = value;
return self;
}
@end

View File

@@ -12,5 +12,6 @@
extern NSString* const kMCFieldValueTypeInteger; extern NSString* const kMCFieldValueTypeInteger;
extern NSString* const kMCFieldValueTypeString; extern NSString* const kMCFieldValueTypeString;
extern NSString* const kMCFieldValueTypeBoolean; extern NSString* const kMCFieldValueTypeBoolean;
extern NSString* const kMCFieldValueTypeChoice;
#endif /* MCFormFieldConstants_h */ #endif /* MCFormFieldConstants_h */

View File

@@ -12,3 +12,4 @@
NSString* const kMCFieldValueTypeInteger = @"Integer"; NSString* const kMCFieldValueTypeInteger = @"Integer";
NSString* const kMCFieldValueTypeString = @"String"; NSString* const kMCFieldValueTypeString = @"String";
NSString* const kMCFieldValueTypeBoolean = @"Boolean"; NSString* const kMCFieldValueTypeBoolean = @"Boolean";
NSString* const kMCFieldValueTypeChoice = @"Choice";

View File

@@ -0,0 +1,28 @@
//
// MCSelectFieldTableViewCell.h
// MonsterCards
//
// Created by Tom Hicks on 9/26/20.
// Copyright © 2020 Tom Hicks. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "MCFormFieldDelegate.h"
#import "MCChoice.h"
NS_ASSUME_NONNULL_BEGIN
@interface MCSelectFieldTableViewCell : UITableViewCell<UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate>
@property NSString* identifier;
@property NSString* label;
@property NSObject* selectedValue;
@property NSArray<MCChoice*>* choices;
@property (weak, nonatomic) id<MCFormFieldDelegate> delegate;
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UILabel *labelView;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,157 @@
//
// MCSelectFieldTableViewCell.m
// MonsterCards
//
// Created by Tom Hicks on 9/26/20.
// Copyright © 2020 Tom Hicks. All rights reserved.
//
#import "MCSelectFieldTableViewCell.h"
@implementation MCSelectFieldTableViewCell {
MCChoice* _selectedChoice;
}
@synthesize choices = _choices;
-(void)setChoices:(NSArray<MCChoice*>*)choices {
// TODO: only do this if choices is different
// TODO: update selectedValue and selectedIndex
_choices = choices;
if (![_choices isEqualToArray:choices]) {
self.selectedValue = _selectedValue;
} else if (choices) {
self.selectedValue = [choices firstObject].value;
}
}
-(NSArray<MCChoice*>*)choices {
return _choices;
}
@synthesize identifier = _identifier;
-(void)setIdentifier:(NSString*)identifier {
if (![_identifier isEqualToString:identifier]) {
_identifier = identifier;
}
}
-(NSString*)identifier {
return _identifier;
}
@synthesize label = _label;
-(void)setLabel:(NSString*)label {
if (![_label isEqualToString:label]) {
_label = label;
}
if (_labelView && ![_labelView.text isEqualToString:label]) {
_labelView.text = label;
}
}
-(NSString*)label {
return _label;
}
-(MCChoice*)findChoiceWithValue:(NSObject*)value
inArray:(NSArray*)array {
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id element, NSDictionary *bindings) {
if (![element isKindOfClass:[MCChoice class]]) {
return NO;
}
MCChoice *choice = (MCChoice*)element;
return [choice.value isEqual:value];
}];
NSArray<MCChoice*> *matchingChoices = [_choices filteredArrayUsingPredicate:predicate];
MCChoice *foundChoice = matchingChoices.count > 0 ? matchingChoices.firstObject : nil;
return foundChoice;
}
@synthesize selectedValue = _selectedValue;
-(void)setSelectedValue:(NSObject*)value {
if (!_choices) {
// choices hasn't been initialized yet so just set our selected value until choices is set
_selectedValue = value;
}
MCChoice *foundChoice = [self findChoiceWithValue:value
inArray:_choices];
if (![_selectedChoice isEqual:foundChoice] || ![_selectedValue isEqual:foundChoice.value]) {
_selectedChoice = foundChoice;
_selectedValue = foundChoice.value;
if (_delegate) {
[_delegate editableValueDidChange:_selectedValue
forIdentifier:_identifier
andType:kMCFieldValueTypeChoice];
}
}
self.textField.text = _selectedChoice != nil
? _selectedChoice.label != nil
? _selectedChoice.label
: @""
: @"";
}
-(NSObject*)selectedValue {
return _selectedValue;
}
- (void)awakeFromNib {
[super awakeFromNib];
UIPickerView *childPicker = [[UIPickerView alloc] init];
childPicker.delegate = self;
childPicker.dataSource = self;
self.textField.inputView = childPicker;
UIToolbar *toolbar = [[UIToolbar alloc] init];
[toolbar sizeToFit];
UIBarButtonItem *button =
[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done", @"Button label")
style:UIBarButtonItemStylePlain
target:self
action:@selector(choiceSelected)];
[toolbar setItems:@[button]
animated:true];
[toolbar setUserInteractionEnabled:YES];
self.textField.inputAccessoryView = toolbar;
self.textField.hidden = NO;
self.textField.text = _selectedChoice.label;
self.textField.delegate = self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)choiceSelected {
[self endEditing:true];
}
#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component {
return [_choices count];
}
#pragma mark - UIPickerViewDelegate
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
return [_choices objectAtIndex:row].label;
}
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
_selectedChoice = [_choices objectAtIndex:row];
self.textField.text = _selectedChoice.label;
self.selectedValue = _selectedChoice.value;
}
@end