views:

44

answers:

2

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 [email protected]

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 arrangedObjects] 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

A: 

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.

Joshua Nozzi
I figured it would be possible to do with multiple array controllers (the colors are fixed - only red and blue :) ) but just hoped there would be a more elegant solution.
Charlie
On the note of using a couple more array controllers I can't seem to get them to use filtered data form the carArrayController in IB. Any ideas? (ie where do i add the predicate? Presuming i need to use a predicate.
Charlie
You can actually type the predicate into the filter predicate field in IB (with the array controller selected) or call -setFilterPredicate: in code. Beyond that, I think just the "@sum.mileage" model keypath (in your text field binding) should be sufficient, but don't quote me. As to elegant, it's best to manage your expectations regarding what bindings will automatically do for you ... less-than-perfectly-simple needs require less-than-perfectly-simple solutions. Bindings only goes so far toward alleviating that. Sorry. :-)
Joshua Nozzi
What I cam trying to achieve is outlined better here:http://stackoverflow.com/questions/3728308/is-it-possible-to-bind-to-a-property-of-an-nsarraycontroller. If you think that is possible with the filter predicate field please let me know how to implement it. Cheers, Oli
Charlie
A: 

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"

Vojto
Any ideas on how to do that? I cant figure it out :(
Charlie
How is that better than simply instantiating two array controllers and setting their respective filter predicates?
Joshua Nozzi
Having many controllers per one entity feels wrong. Just my opinion.
Vojto
It's perfectly normal and legit. Try managing popup buttons in a table column with a another table to manage the available choices in the popup with just one controller per entity.
Joshua Nozzi