3lvis / Form

The most flexible and powerful way to build a form on iOS
http://hyper.no
Other
1.64k stars 145 forks source link

Evaluate why pressing clear on a select field makes the `valueID` of select nil #558

Closed 3lvis closed 7 years ago

3lvis commented 7 years ago

More info: https://github.com/hyperoslo/Form/pull/553

richardtop commented 7 years ago

It's pretty straighforward: tapping "clear" button triggers this method in FORMFieldValuesTableViewController

- (void)clearButtonDidTap {
    FORMFieldValue *fieldValue = [FORMFieldValue new];
    fieldValue.value = @NO;

    if ([self.delegate respondsToSelector:@selector(fieldValuesTableViewController:didSelectedValue:)]) {
        [self.delegate fieldValuesTableViewController:self
                                     didSelectedValue:fieldValue];
    }
}

Compare it with the one triggered when user selects one of the rows:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    FORMFieldValue *fieldValue = self.values[indexPath.row];

    if ([self.delegate respondsToSelector:@selector(fieldValuesTableViewController:didSelectedValue:)]) {
        [self.delegate fieldValuesTableViewController:self
                                     didSelectedValue:fieldValue];
    }
}

So, in this case, the value is being picked among some valid values.

In the former case, a new value is created and propagated further through the chain. In case of select-style table (not DatePicker), the delegate is FORMSelectFieldCell, so here is the method triggered:

- (void)fieldValuesTableViewController:(FORMFieldValuesTableViewController *)fieldValuesTableViewController
                      didSelectedValue:(FORMFieldValue *)selectedValue {
    self.field.value = selectedValue;

    [self updateWithField:self.field];

    [self validate];

    [fieldValuesTableViewController dismissViewControllerAnimated:YES completion:nil];

    if ([self.delegate respondsToSelector:@selector(fieldCell:updatedWithField:)]) {
        [self.delegate fieldCell:self updatedWithField:self.field];
    }
}

So, now the newly created field has been set. Comparison with that field, that has only value, but doesn't have an id triggers crash.

553 is one of the options to avoid the crash. The other one is to use predefined fields for empty/clear states.