Chapter 21: Prototype

Quoting the book, “A Prototype is an object that is copied to implement application features.” In some circumstances, it’s either faster or more flexible than creating new instances from scratch. Creating an NSMatrix is one of these situations, since the NSMatrix will need to create objects in its grid, often identical objects, which are often custom objects that aren’t known in advance.

There’s a fairly elaborate discussion that I don’t quite understand, but I will probably have a better idea after I’ve done the rest of the book. Based on that, I decided not to finish the chapter, instead just doing the section that created a matrix of MyLabeledBarCells in a scroll view.

After getting the cells to show up, I decided it would be good to be able to change them, so I went ahead and added a slider to set a float value between 0 and 1, and two steppers to determine the row and column of the cell that would have its value changed. It turns out there was a bug in MyLabeledBarCell: the length of the bar wouldn’t update.

After a half hour or so of debugging, I realized that the drawBarInRect: method never got called after the cells were first drawn. It took another half hour or so to figure out how to get it to redraw. Along the way I used bindings to keep track of the row and column to be set.

Good things I got out of this exercise:

  1. Practice in setting up bindings.
  2. Reminders of how drawing works & how you must call a view’s -setNeedsDisplay: method to have the screen update.
  3. I noticed that this version of Xcode (3.2.1) automatically provides an AppDelegate class, so I didn’t have to create a controller class for handling changing the bar lengths.

Interestingly, the next chapter, Template Method, illustrates its topic using drawing, and more specifically, the -drawRect: method. I do remember the Template Method from Head First Design Patterns, but it’s been a while, and I haven’t studied the concept in Cocoa.

This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s