Button actions
jeffleeismyhero opened this issue · comments
Whenever a button gets pressed you'll received a call to this block, then you can do whatever you want by using the button's fieldID
I've attempted something like this. Where am I going wrong? Both the decrease and increase steps fire.
- (FORMDataSource *)dataSource {
if (_dataSource) return _dataSource;
_dataSource = [[FORMDataSource alloc] initWithJSON:self.JSON
collectionView:self.collectionView
layout:self.layout
values:self.initialValues
disabled:NO];
__weak typeof(self)weakSelf = self;
_dataSource.fieldUpdatedBlock = ^(id cell, FORMField *field) {
HYPParsedRelationship *parsedRelationship = [field.fieldID hyp_parseRelationship];
NSString *attribute = parsedRelationship.attribute;
if ([attribute isEqualToString:@"decrease_start_date"]) {
[weakSelf adjustStartDate:-1];
} else if ([attribute isEqualToString:@"increase_start_date"]) {
[weakSelf adjustStartDate:1];
}
};
return _dataSource;
}
The issue here was with my date comparison to prevent dates earlier than today:
- (void)adjustStartDate:(NSInteger)value {
__weak typeof(self)weakSelf = self;
[self.dataSource fieldWithID:@"start_date" includingHiddenFields:YES completion:^(FORMField *field, NSIndexPath *indexPath) {
if (field) {
NSDate *adjustedDate = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay
value:value
toDate:field.value
options:0];
if (adjustedDate < [NSDate date]) {
adjustedDate = [NSDate date];
}
field.value = adjustedDate;
[weakSelf.dataSource updateValuesWithDictionary:@{ field.fieldID : adjustedDate }];
[weakSelf.dataSource reloadFieldsAtIndexPaths:@[indexPath]];
}
}];
}
The working version of that method is:
- (void)adjustStartDate:(NSInteger)value {
__weak typeof(self)weakSelf = self;
[self.dataSource fieldWithID:@"start_date" includingHiddenFields:YES completion:^(FORMField *field, NSIndexPath *indexPath) {
if (field) {
NSDate *adjustedDate = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay
value:value
toDate:field.value
options:0];
if ([adjustedDate compare:[NSDate date]] == NSOrderedAscending) {
adjustedDate = [NSDate date];
}
field.value = adjustedDate;
[weakSelf.dataSource updateValuesWithDictionary:@{ field.fieldID : adjustedDate }];
[weakSelf.dataSource reloadFieldsAtIndexPaths:@[indexPath]];
}
}];
}
While learning Objective-C has been fun, this sort of stuff makes me miss the conveniences of Ruby-land.
Of course! I hate when I have to do that. Hopefully it will get better with Swift.