365Cocoa

A snippet of Objective-C code per day, 365 days long.
Written by Pieter Omvlee, developer and owner of Bohemian Coding.

Apr 18

Day 44: nsview programming

View programming is often considered difficult by programmers who have never actually done it. Let me tell you; it’s not. I’m not sure how this misunderstanding arose or why exactly it is perceived as difficult. Maybe just because it’s so unfamiliar and people don’t know where to start.

Just like with any programming project, stick to MVC and just use the view for the view-part of the application. There are I think two routes to take here: give the view it’s information and make the view smart enough to know how to draw whatever it is supposed to draw.
The other route is of course using delegates. Both can work well and it just depends on what your demands. The next two posts will give an example of both ways of working.

For today just remember how event handling fits into this:
1. Your view intercepts events
2. Your code updates the model
3. Your view or model tells the view to redraw (based on the complexity of the drawing you might want to heavily optimise here)
4. Your drawRect gets called and you do your drawing based on the model.


  1. 365cocoa posted this