Cocoa; Binding a Text Field to the sum of a filtered array (from an NSArrayController)
I have (for example) a table with cars in. The cars each have a name, a color and a mileage.
I can have a text field which displays the total mileage of all the cars using bindings in interface builder:
Value - Bind To: Car Array Controller.arrangedObjects.@sum.mielage
However, I would like separate boxes for the total mileage done by red cars and blues cars. I know i can code this using a predicate to filter the array eg:
NSPredicate *bluePredicate = [NSPredicate predicateWithFormat:@"color like blue"];
NSArray *blueCars = [[carArrayController arrangedObject开发者_如何学编程s] filteredArrayUsingPredicate:bluePredicate];
NSNumber *blueMileage = [blueFilteredArray valueForKeyPath:@"@sum.mileage"];
But I don't want to have to do this and have to code IBOutlets for the text boxes. I want to do it purely in IB if possible.
Any ideas??
Cheers, Oli
Why not create a separate array controller for each car color, then? Or is that also variable?
If the car colors are variable, the best approach will probably still be to do this programmatically.
Don't do many controllers. I think the most elegant solution would be subclassing nsarraycontroller and somehow adding support for "arranged objects with a predicate"
精彩评论