How to wire up a custom view using NSViewController to a model?

63 Views Asked by At

I came across a problem where I see multiple approaches but I'm unsure which one to implement. Please consider this simplified example of a classical Cocoa MVC design:

  • My own custom graph view
  • NSViewController subclass for this view
  • Model (distributed, but relevant part here is user defaults)

Let's assume I have a property in my custom view, say foreground color of the graph line and a color well in the same window where the user can choose that color. The color is stored permanently as a user default. How should I wire this up? I see a few possibilities:

  1. Bind the color well directly to the color property in the custom view and let the view controller load the color from user defaults and set it in the view when the view is loaded (and store the value from the view to user defaults when the view is closed).

  2. Duplicate the color property in the view controller, bind the color well to this property and have custom accessors in the view controller set and get the value from the view?

  3. Bind the color well directly to user defaults and manually bind the view's color property to user defaults in the view controller when the view is loaded.

I tend to go for 3. but really dislike manual bindings in code. Which one do you recommend as cleanest solution? Or do you see better approaches?

This is for a 1:1 value (color), so here goes the bonus question: Imagine that instead of the the color well I have a NSPopup with a list of predefined colors. Instead of the color value directly I only have an index into a list of these predefined colors. How should I wire this up?

Thanks a lot, Chris

0

There are 0 best solutions below