如何设置XLForm行值的格式



我在项目中使用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;
}

根据文档,我应该捕捉到用户与字段进行了交互,然后更改字段的值并更新行。

这就是我如何捕捉到用户在字段中输入了一些东西:

-(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];
            }    
}

通过重置formRowDescriptorValueHasChangedrowDescriptor的值创建了一个无限循环。

我通过改变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];
        }            
    }    
}

相关内容

  • 没有找到相关文章

最新更新