ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  The Cocoa Controller Layer
Subject:   Slightly different views on the same data?
Date:   2004-04-11 11:38:21
From:   look_to_windward

Hi,


I've switched over to using NSArrayController for an application I'm developing, and it's a very neat fit for a class I already had in there. Gets rid of lots of the glue code, as you mention. However, I've run into a problem which isn't easy to solve given all the simpler examples that apple and yourself have provided.


I have some controllers which are in charge of an array that I want to present to the user in several ways, but customised each time. For example, one of the controllers has an array which is presented in a table, but also used to populate a popup menu with the array plus a few other items.


The problem comes when I want to customise the views of the data - for example in the popup I'd like to say


none
---
array item1
array item...


Before, I had the controller watching for changes in its array, and then manually rebuilding the popup and the menubar menu (again slightly different) when things changed (and of course updating the table as it was the data source).


Ideally I'd like to be able to bind the various objects to different accessors in the controller that returned a slightly modified array depending on where it was being presented. I did try to experiment with manually setting up bindings (since you can't set them up in IB, which is a shame), and I can't seem to get that to work.


The alternative would be to set up a controller for each 'view' on the array, but that doesn't seem right to me (it's tying the controllers very tightly to the different views) - I don't see why you should have multiple controller objects for one array of objects - I'd like to keep all the logic for manipulating that array in one place.


Any help on this would be greatly appreciated. Perhaps I just haven't understood how bindings should work. Closest I've found to a solution is this
filtering example - however that's using a controller for each 'view'. A possible subject for a future article anyway : )



1 to 2 of 2
1 to 2 of 2