Skip navigation.
 
mlRe: Correct use of NSViewController
FROM : Cathy Shive
DATE : Wed Mar 19 13:19:06 2008

Hi,

My strategy is similar to yours, but it doesn't have to result in 
spaghetti code.  I have a view controller class that contains a list 
of sub-viewcontrollers.  What I end up with is a structure that does 
in fact resemble the actual view hierarchy.

The tricky part is breaking down your window controller into logical 
units that are as self-contained as possible.  In your design, you 
don't want the subcontrollers to have dependencies on each other, or 
you will end up with spaghetti code.  This is where bindings, KVO and 
sometimes notifications come in real handy.

So in the case of Mail.app, I could see a window controller with a 
singe view controller that returns the main split view as its view. 
That controller creates two sub-controllers, the right side and left 
side of the main split view. The left side view controller returns 
the source list as its view and the right side controller returns the 
other split view as its view.

As far as data is concerned, the left side controller's source list 
is displaying a list of mail boxes that may be stored in an array 
controller somewhere else.  Users select a mail box by clicking on 
the source list.  The right side controller (with the detail/compare 
split view) is bound to the "selectedObjects" of that array 
controller.  When users change the selected objects, the right side 
controller is alerted to the change through bindings.  It then gets 
the selected Mail box and displays what it needs to display.  There's 
no reason for the right side and left side controllers to communicate 
with each other, the bindings mechanism takes care of all of that.

Of course, your controllers don't necessarily need to have the 
hierarchical structure of controllers built in to the class.  One 
advantage of having that relationship in place is that when dealloc 
is called on a view controller, it will automatically release all of 
the subcontrollers in its list so that you don't have to worry about 
memory management for these objects.

I know people that keep dictionaries with the view controllers and 
their associated nibs instead of storing them as a tree structure 
like I do.  I don't know which is the best, but I think that your 
instinct that some structure should be there is right.


Hope that helps,
Cathy




On Mar 19, 2008, at 12:37 PM, Jonathan Dann wrote:

> Hi guys,
>
> I've ended up with a bloated window controller in my document based 
> app and want to refactor my code using done view controllers.  My 
> question is really about design.
>
> If I have a split view with which contains a split view (like mail) 
> then should I have a controller for the large split view which will 
> then contain an ivar that points to a controller for the smaller 
> split view?  As a subview of one of the split views I would then 
> have a text view that itself needs a view controller, so it seems 
> like I'll he ending up with a heirarchy of view controllers that 
> reflects the view heirarchy itself. This seems like I'm gong about 
> this the wrong way ad would end up with a spaghetti code.
>
> Can anyone give mr a few hints as to how this should he fine properly?
>
> Thanks,
>
> Jon
> _______________________________________________
>
> Cocoa-dev mailing list (<email_removed>)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
> http://lists.apple.com/mailman/options/cocoa-dev/<email_removed>
>
> This email sent to <email_removed>

Related mailsAuthorDate
mlCorrect use of NSViewController Jonathan Dann Mar 19, 12:37
mlRe: Correct use of NSViewController Cathy Shive Mar 19, 13:19
mlRe: Correct use of NSViewController Jonathan Dann Mar 20, 02:19
mlRe: Correct use of NSViewController Cathy Shive Mar 20, 08:40
mlRe: Correct use of NSViewController Jonathan Dann Mar 20, 12:35
mlRe: Correct use of NSViewController Cathy Shive Mar 20, 13:29
mlRe: Correct use of NSViewController Paul Szego Mar 20, 23:27
mlRe: Correct use of NSViewController Jonathan Dann Mar 21, 21:49
mlRe: Correct use of NSViewController Cathy Shive Mar 21, 22:31
mlRe: Correct use of NSViewController Steve Weller Mar 22, 04:25
mlRe: Correct use of NSViewController Steve Weller Mar 22, 04:31
mlRe: Correct use of NSViewController Cathy Shive Mar 22, 05:18