views:

26

answers:

1

As per the title really. I have an entity which has a property "idNumber". Just as I can bind a text box to the array controller's arrangedObjects with Model Key Path "@count" to provide a count of all the objects in the array, I would like to be able to bind a text field to the array controller's arrangedObjects with a value transformer to return a count of a filtered subset of the array (those objects with an idNumber >5).

I'm assuming this is possible??

My attempt is:

I have bound the text box to the array controller, Controller Key "arrangedObjects" Model Key Path "" Value Transformer "AllToSomeTransformer".

The code for the AllToSomeTransformer is:

-(id)transformedValue:(id)value {
NSArray *arrayOfAllCars;

if (value == nil) return nil;

if ([value respondsToSelector: @selector(count)]) {
    arrayOfAllCars = [NSArray arrayWithArray:value];
} else {
    [NSException raise: NSInternalInconsistencyException
                format: @"Value (%@) does not respond to -count.",
[value class]];
}

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"idNumber > %@", [NSNumber numberWithInt:5]];

NSArray *arrayOfBlueCars = [arrayOfAllCars filteredArrayUsingPredicate:predicate];

return [NSNumber numberWithInt:[arrayOfBlueCars count]];
}

I believe my value transformer is correctly registered etc. By way of trying to figure out what's going on I added some NSLog outputs through to above code. It appears the above method is only called once, on app startup, and not again when new objects are added to the array. Could this be why the text field is not being updated with values??

Thanks, Oli

A: 

Since the transformer is called and does work but only once, that suggest there is something wrong with the bindings such that the transformer is not observing the changes in arrangedObjects. I'm not sure what that would be.

TechZen

related questions