我在我的项目中使用XLForm,并希望以特定格式显示行之一的值。
该行是“年营业额”,我想显示用户输入的货币值-例如,如果用户在表单字段中输入 1000000 ,则该字段应更新为显示: $ 1,000,000.00
我怎么做?
这是在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;
}
根据the documentation,我应该发现用户已经与该字段进行了交互,然后更改了该字段的值并更新了该行。
这就是我发现用户在该字段中输入了一些内容的方法:
-(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];
}
}
最佳答案
通过在rowDescriptor
中重置formRowDescriptorValueHasChanged
的值来创建无限循环。
我通过更改formRowDescriptorValueHasChanged
来解决此问题,首先检查rowDescriptor
的值是否已经以货币符号开头,并且仅在缺少时才添加它:
-(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];
}
}
}
关于ios - 如何格式化XLForm行值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33125909/