views:

259

answers:

1

I have an NSTableView that is done using the typical IB and data bindings. It works well.

However I need to put in a Segmented Control to indicate date range. So for example, if user clicks on the "Last Year" segmented control button, the table should only display records for last year.

I looked at the data binding options in IB for the NSArrayController, but I have no idea how to do this.

Can somebody point me in the right direction please?

*Update:

The app I'm working on has 3 SegmentedControl buttons -- "This Year", "Last Year", "Total", and a tableview that displays the records associated with the year selection the user selected. So selecting the "This Year" will reload the tableview with records for the current year, and selecting "Last Year" will reload the table to show last year records.

+1  A: 

You want to set a filter predicate. Your "last year" button would have to set the array controller's "filter predicate" to one that describes the appropriate date range filter.

Predicates Programming Guide http://tinyurl.com/ykcyhxy

-[NSArrayController setFilterPredicate:] http://tinyurl.com/yz4txn9

Joshua Nozzi
Thanks, but it's not clear to me how I can dynamically change the predicate filter from the IB. Maybe I can't and that's the point.
Seymour Cakes
It's the most direct way. As is often said about Bindings on the cocoa-dev list: "Sometimes you just have to write some code." :-)It really depends on what exactly you're trying to do. If you describe more about how this segmented control is supposed to work with your app (and how things are bound, where the model is controlled), I can get a lot more specific. :-)
Joshua Nozzi
Thanks for helping me out, I just added more details on the app I'm working on the problem with the filtering. I'm not afraid of code but just now sure how to go about the whole thing. :-)
Seymour Cakes
Okay, so the most direct way is via the target/action mechanism. Whatever controller you're using (app delegate, a document, a window controller, whichever - we'll call it your app controller for now) will need an outlet to the array controller. The segmented button will target the app controller. When pressed, you'll figure out (via "sender" what segment was hit. You'll then construct an appropriate NSPredicate, then set the array controller's -filterPredicate: ... nice and straightforward.
Joshua Nozzi
The last part of the puzzle is figuring out how to construct the NSPredicate, but this is also straightforward if you read the Predicate Programming Guide and google some examples.
Joshua Nozzi
Thank you Joshua Nozzi!
Seymour Cakes