Stuck on challenge 1


I’ve really enjoyed working through the book so far but now I’m stuck on chapter 17 challenge 1 and seem to be going round in circles.

The good news is that I do understand how the TouchTracker code works but in adding saving and loading of the line data, it seems bad practice that the data is being stored in the view. So I thought that linesInProcess and completeLines should be either in the TouchTrackerAppDelegate or in a view controller. I managed to add a view controller with xib (TouchDrawController) and that works fine but I don’t really know where to go from here. Some questions:

  1. I’ve moved linesInProcess and completeLines from TouchDrawView into TouchDrawController and added both of these as properties.
  2. How can TouchDrawView now access these variables as it did with a line such as

I couldn’t see any obvious way for a view to get its view controller.
3. Where do you draw the line regarding a method like clearAll? Should that remain in the TouchDrawView, or (as it seems to be more about the data than the view) should it also be moved to TouchDrawController? Or doesn’t it really matter?

many thanks for any enlightenment!

  1. Yup, sounds good.

  2. Now, go back and add linesInProcess and completeLines back to TouchDrawView - also make them properties. TouchDrawController will be responsible for loading them from disk and then setting them in TouchDrawView. TouchDrawController will also be responsible for getting them back from TouchDrawView and saving them to disk.

  3. You can keep it in the view because when TouchDrawController goes to save to disk, it will get the current completeLines from TouchDrawView.

(This is similar to what you did in the AppDelegate->Controller relationship in the Archiving chapter, only now, its a Controller->View relationship.)


Thanks for the useful reply Joe. Through the wonders of NSLog, I’ve finally managed to get things working properly using 2 different approaches.

The AppDelegate is unarchiving the data and then setting it to completeLines properties in both the view controller and the view. I ended up simplifying things by removing the linesInProcess property from the view controller since it’s only needed while actually drawing in the view.

Then I got brave and reworked things all over again: Removed completeLines and linesInProcess from TouchDrawView and added a parentController ivar referring to the view’s view controller. Moved all the touch event methods into the TouchDrawController. So TouchDrawView only contains initWithCoder, dealloc and DrawRect accesses the Line data by calling [parentController completeLines] and [parentController linesInProcess]. All seems to work fine. I’m not sure whether this architecture is good practice or not but it seems more streamlined.

Coming from 7 years of being very comfortable with REALbasic, delving into the world of Obj-C is quite a challenge! Slowly the fog is clearing though and things are becoming a bit clearer. Thanks for a great book.


This way works. It’s not quite as “Cocoa-y”, but I’ve certainly taken the same approach in applications before when this approach felt “right.”


In this solution, which class is conforming to the protocol? the controller, the TouchDrawView? or both?


The Line class must conform to NSCoding to save the array of lines to disk.