Skip to content

Commit

Permalink
Added ability to register cell and controller classes based on field …
Browse files Browse the repository at this point in the history
…class
  • Loading branch information
nicklockwood committed May 26, 2014
1 parent 47bad5a commit c81d62b
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 29 deletions.
4 changes: 0 additions & 4 deletions Examples/BasicExample/Classes/RootForm.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@
#import "RegistrationForm.h"


@interface BMRange : NSObject
@end

@interface RootForm : NSObject <FXForm>

@property (nonatomic) BMRange* value;
@property (nonatomic, strong) LoginForm *login;
@property (nonatomic, strong) RegistrationForm *registration;

Expand Down
3 changes: 0 additions & 3 deletions Examples/BasicExample/Classes/RootForm.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

#import "RootForm.h"

@implementation BMRange

@end

@implementation RootForm

Expand Down
1 change: 0 additions & 1 deletion Examples/BasicExample/Classes/RootFormViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
//set up form
self.formController.form = [[RootForm alloc] init];
[self.formController registerCellClass:[FXFormSliderCell class] forClassName:NSStringFromClass([BMRange class])];
}
return self;
}
Expand Down
8 changes: 5 additions & 3 deletions FXForms/FXForms.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,16 @@ static NSString *const FXFormFieldTypeImage = @"image";
- (FXFormField *)fieldForIndexPath:(NSIndexPath *)indexPath;
- (void)enumerateFieldsWithBlock:(void (^)(FXFormField *field, NSIndexPath *indexPath))block;

- (Class)cellClassForField:(FXFormField *)fieldType;
- (Class)cellClassForField:(FXFormField *)field;
- (void)registerDefaultFieldCellClass:(Class)cellClass;
- (void)registerCellClass:(Class)cellClass forFieldType:(NSString *)fieldType;
- (void)registerCellClass:(Class)cellClass forClassName:(NSString*)fieldClassName;
- (void)registerCellClass:(Class)cellClass forFieldClass:(Class)fieldClass;

- (Class)viewControllerClassForFieldType:(NSString *)fieldType;
- (Class)viewControllerClassForField:(FXFormField *)field;
- (void)registerDefaultViewControllerClass:(Class)controllerClass;
- (void)registerViewControllerClass:(Class)controllerClass forFieldType:(NSString *)fieldType;
- (void)registerViewControllerClass:(Class)controllerClass forFieldClass:(Class)fieldClass;


@end

Expand Down
70 changes: 53 additions & 17 deletions FXForms/FXForms.m
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ @interface FXFormController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSMutableDictionary *cellClassesForFieldTypes;
@property (nonatomic, strong) NSMutableDictionary *cellClassesForFieldClasses;
@property (nonatomic, strong) NSMutableDictionary *controllerClassesForFieldTypes;
@property (nonatomic, strong) NSMutableDictionary *controllerClassesForFieldClasses;

- (void)performAction:(SEL)selector withSender:(id)sender;

Expand Down Expand Up @@ -1142,10 +1143,12 @@ - (instancetype)init
FXFormFieldTypeDateTime: [FXFormDatePickerCell class],
FXFormFieldTypeImage: [FXFormImagePickerCell class]} mutableCopy];

_cellClassesForFieldClasses = [@{FXFormFieldTypeDefault: [FXFormBaseCell class]} mutableCopy];
_cellClassesForFieldClasses = [@{@"NSObject": [FXFormBaseCell class]} mutableCopy];

_controllerClassesForFieldTypes = [@{FXFormFieldTypeDefault: [FXFormViewController class]} mutableCopy];


_controllerClassesForFieldClasses = [@{@"NSObject": [FXFormViewController class]} mutableCopy];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
Expand All @@ -1167,19 +1170,28 @@ - (void)dealloc
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (Class)cellClassForField:(FXFormField*)field
- (Class)cellClassForField:(FXFormField *)field
{
if (field.type != FXFormFieldTypeDefault) {

if (field.type != FXFormFieldTypeDefault)
{
return self.cellClassesForFieldTypes[field.type] ?:
self.parentFormController.cellClassesForFieldTypes[field.type] ?:
self.cellClassesForFieldTypes[FXFormFieldTypeDefault];

} else {

return self.cellClassesForFieldClasses[NSStringFromClass(field.valueClass)] ?:
self.parentFormController.cellClassesForFieldClasses[NSStringFromClass(field.valueClass)] ?:
self.cellClassesForFieldClasses[FXFormFieldTypeDefault];
}
else
{
Class valueClass = field.valueClass;
while (valueClass != [NSObject class])
{
Class cellClass = self.cellClassesForFieldClasses[NSStringFromClass(valueClass)] ?:
self.parentFormController.cellClassesForFieldClasses[NSStringFromClass(valueClass)];
if (cellClass)
{
return cellClass;
}
valueClass = [valueClass superclass];
}
return self.cellClassesForFieldTypes[FXFormFieldTypeDefault];
}
}

Expand All @@ -1195,17 +1207,35 @@ - (void)registerCellClass:(Class)cellClass forFieldType:(NSString *)fieldType
self.cellClassesForFieldTypes[fieldType] = cellClass;
}

- (void)registerCellClass:(Class)cellClass forClassName:(NSString*)fieldClassName
- (void)registerCellClass:(Class)cellClass forFieldClass:(__unsafe_unretained Class)fieldClass
{
NSParameterAssert([cellClass conformsToProtocol:@protocol(FXFormFieldCell)]);
self.cellClassesForFieldClasses[fieldClassName] = cellClass;
self.cellClassesForFieldClasses[NSStringFromClass(fieldClass)] = cellClass;
}

- (Class)viewControllerClassForFieldType:(NSString *)fieldType
- (Class)viewControllerClassForField:(FXFormField *)field
{
return self.controllerClassesForFieldTypes[fieldType] ?:
self.parentFormController.controllerClassesForFieldTypes[fieldType] ?:
self.controllerClassesForFieldTypes[FXFormFieldTypeDefault];
if (field.type != FXFormFieldTypeDefault)
{
return self.controllerClassesForFieldTypes[field.type] ?:
self.parentFormController.controllerClassesForFieldTypes[field.type] ?:
self.controllerClassesForFieldTypes[FXFormFieldTypeDefault];
}
else
{
Class valueClass = field.valueClass;
while (valueClass != [NSObject class])
{
Class controllerClass = self.controllerClassesForFieldClasses[NSStringFromClass(valueClass)] ?:
self.parentFormController.controllerClassesForFieldClasses[NSStringFromClass(valueClass)];
if (controllerClass)
{
return controllerClass;
}
valueClass = [valueClass superclass];
}
return self.controllerClassesForFieldTypes[FXFormFieldTypeDefault];
}
}

- (void)registerDefaultViewControllerClass:(Class)controllerClass
Expand All @@ -1220,6 +1250,12 @@ - (void)registerViewControllerClass:(Class)controllerClass forFieldType:(NSStrin
self.controllerClassesForFieldTypes[fieldType] = controllerClass;
}

- (void)registerViewControllerClass:(Class)controllerClass forFieldClass:(__unsafe_unretained Class)fieldClass
{
NSParameterAssert([controllerClass conformsToProtocol:@protocol(FXFormFieldViewController)]);
self.controllerClassesForFieldClasses[NSStringFromClass(fieldClass)] = fieldClass;
}

- (void)setDelegate:(id<FXFormControllerDelegate>)delegate
{
_delegate = delegate;
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FXForms

version 1.1.6, April 18th, 2014
version 1.2, May 26th, 2014

Copyright (C) 2014 Charcoal Design

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ Once you have created your custom cell, you can use it as follows:

* If your cell is used only for a few specific fields, you can use the `FXFormFieldCell` property to use it for a particular form field
* If your cell is designed to handle a particular field type (or types), you can tell the formController to use your custom cell class for a particular type using the `-registerCellClass:forFieldType:` method of FXFormController.
* If your cell is designed to handle a particular field value class (or subclass), you can tell the formController to use your custom cell class for a particular value class using the `-registerCellClass:forFieldClass:` method of FXFormController.
* If you want to completely replace all cells with your own classes, use the `-registerDefaultFieldCellClass:` method of `FXFormController`. This replaces all default cell associations for all field types with your new cell class. You can then use `-registerCellClass:forFieldType:` to add additional cell classes for specific types.


Expand All @@ -483,6 +484,7 @@ Release notes
Version 1.2 beta

- Added phone field type
- Added ability to register cell and controller classes based on field value class as well as type

Version 1.1.6

Expand Down

0 comments on commit c81d62b

Please sign in to comment.