I'm using XLForm in my project and want to display the value of one of the rows in a specific format.
The row is "Annual turnover" and I want to display the value entered by the user as a currency - for example, if the user enters 1000000 into the form field, the field should be updated to display this: $ 1,000,000.00
How do I do that?
This is how the form field is initialised in initializeForm():
-(void)initializeForm
{
// Other fields in the form...
// Annual turnover Section
self.section = [XLFormSectionDescriptor formSectionWithTitle:formLabel8AnnualTurnover];
//section.footerTitle = @"Describe new products";
[self.formDescriptor addFormSection:self.section];
// Annual turnover
self.row = [XLFormRowDescriptor formRowDescriptorWithTag:formField8AnnualTurnover rowType:XLFormRowDescriptorTypeText title:nil];
self.row.required = NO;
rowDescriptor8AnnualTurnover = self.row;
[self.section addFormRow:self.row];
// Other fields in the form...
self.form = self.formDescriptor;
}
According to the documentation, I should catch that the user has interacted with the field and then change the value of the field and update the row.
This is how I catch that the user has entered something into the field:
-(void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
{
[super formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
// Get the current form values
formValues = [self.form formValues];
int i = 0;
if ([rowDescriptor.tag hasPrefix:formField8AnnualTurnover]){
NSString *annualTurnoverValue = [(XLFormOptionsObject*)formValues[formField8AnnualTurnover] displayText];
rowDescriptor8AnnualTurnover.value = [NSString stringWithFormat:@"R %@", annualTurnoverValue]; //This breaks.
[self reloadFormRow:rowDescriptor8AnnualTurnover];
}
}
An endless loop was being created by resetting the value of the rowDescriptor
in formRowDescriptorValueHasChanged
.
I resolved this issue by changing formRowDescriptorValueHasChanged
to first check whether or not the rowDescriptor
's value already starts with the currency symbol, and only add it if it's missing:
-(void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
{
[super formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
// Get the current form values
formValues = [self.form formValues];
if ([rowDescriptor.tag hasPrefix:formField8AnnualTurnover]){
NSString *annualTurnoverValue = [(XLFormOptionsObject*)formValues[formField8AnnualTurnover] displayText];
if([annualTurnoverValue hasPrefix:@"R "]){
// Do nothing - the value will simply be increased.
}else{
rowDescriptor8AnnualTurnover.value = [NSString stringWithFormat:@"R %@", annualTurnoverValue];
[self reloadFormRow:rowDescriptor8AnnualTurnover];
}
}
}