Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 174907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:36:22+00:00 2026-05-11T13:36:22+00:00

This is a Cocoa n00b question – I’ve been programming GUI applications for years

  • 0

This is a Cocoa n00b question – I’ve been programming GUI applications for years in other environments, but now I would like to understand what is ‘idiomatic Cocoa’ for the following trivialized situation:

I have a simple custom NSView that allows the user to draw simple shapes within it. Its drawRect implementation is like this:

- (void)drawRect:(NSRect)rect {     // Draw a white background.     [[NSColor whiteColor] set];     NSRect bounds = [self bounds];     [NSBezierPath fillRect:bounds];      [[NSColor blackColor] set];      // 'shapes' is a NSMutableArray instance variable     // whose elements are NSValues, each wrapping an NSRect.     for (NSValue *value in shapes)     {         NSRect someRect;         [value getValue:&someRect];         [self drawShapeForRect:someRect];     }      // In addition to drawing the shapes in the 'shapes'     // array, we draw the shape based on the user's     // current drag interaction.     [self drawShapeForRect:[self dragRect]]; } 

You can see how simple this code is: the shapes array instance variable acts as the model that the drawRect method uses to draw the shapes. New NSRects are added to shapes every time the user performs a mouse-down/drag/mouse-up sequence, which I’ve also implemented in this custom view. Here’s my question:

If this were a ‘real’ Cocoa application, what would be the idiomatic way for my custom view to update its model?

In other words, how should the custom view notify the controller that another shape needs to be added to the list of shapes? Right now, the view tracks shapes in its own NSMutableArray, which is fine as an implementation detail, but I do not want to expose this array as part of my custom view’s public API. Furthermore, I would want to put error-checking, save/load, and undo code in a centralized place like the controller rather than have it littered all over my custom views. In my past experience with other GUI programming environments, models are managed by an object in my controller layer, and the view doesn’t generally update them directly – rather, the view communicates when something happens, by dispatching an event, or by calling a method on a controller it has a reference to, or using some similarly-decoupled approach.

My gut feeling is that idiomatic Cocoa code would expose a delegate property on my custom view, and then wire the MyDocument controller object (or another controller-layer object hanging off of the document controller) to the view, as its delegate, in the xib file. Then the view can call some methods like shapeAdded:(NSRect)shape on the delegate. But it seems like there are any number of other ways to do this, such as having the controller pass a reference to a model object (the list of shapes) directly to the custom view (feels wrong), or having the view dispatch a notification that the controller would listen to (feels unwieldy), and then the controller updates the model.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T13:36:22+00:00Added an answer on May 11, 2026 at 1:36 pm

    Having a delegate is a cromulent way to do this. The other way would be to expose an NSArray binding on the view, and bind it to an array controller’s arrangedObjects binding, then bind the array controller’s content binding to whatever owns the real array holding the model objects. You can then add other views on the same array controller, such as a list of objects in the active layer.

    This being a custom view, you’ll need to either create an IBPlugin to expose the binding in IB, or bind it programmatically by sending the view a bind:toObject:withKeyPath:options: message.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 209k
  • Answers 209k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Rotor is the shared source implementation of the runtime itself.… May 12, 2026 at 9:40 pm
  • Editorial Team
    Editorial Team added an answer Mono is what you are looking for. It contains the… May 12, 2026 at 9:40 pm
  • Editorial Team
    Editorial Team added an answer The problem with the URL you would like to have… May 12, 2026 at 9:40 pm

Related Questions

This is a Cocoa n00b question - I've been programming GUI applications for years
I'm implementing a Photoshop plugin on the Mac, and I'm doing it using Cocoa.
I'm developing a Cocoa application, and I'm using constant NSString s as ways to
I am about to start my A-Level Computing project (High School Level) which will
I write applications for various platforms, but chiefly OS X and Windows. On both,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.