Monday, February 4, 2008

1. Quartz subclassing NSView

My aim is to create a simple 2D painting application using Cocoa. I have already tried some tutorials on the basics of coding in Objective-C and Cocoa with the Apple IDE Xcode.

Xcode with its counterpart Interface Builder are two extremelly powerfull tools. Most of the work is done using interface, and they prepare automatically a skeleton of the code. Even if, for the lovers of pure coding, this could seem to be not so good, there is the advantage that the code generated this way is quite clean, compliant to Apple standards an less error prone.

I am collecting informations from a lot of different resources on line. There are two tutorial that I found particularly interesting. Those are: the Cocoa Dev Central, Intro To Quartz II, and the Episode 18 - Custom Views of CocoaCast. They basically talk about the same thing: subclassing a NSView class to create a drawing canvas.

All my experience comes from Java so I have no problems to understand Object Oriented languages, I just have some little problem to understand some C sintax but it does not rapresent a great obstacle.

So, watching this two tutorials I discovered that a class can be subclassed in two differnt ways.

  1. Using Xcode: in the Project window left list, it is possible to right click on the Classes subfolder (or any other project subfolder) and choose Add -> New File.... The Assistant will appear and it is possible to choose from a list preconfigured subclasses. Choose Objective-C NSView subclass in the Cocoa section. Then click Next. In the following view give a name to your class and click Finish. After these steps, it is necessary to drag the custom class header to Interface Builder, in the Instances panel of the NIB window to make Interface Builder aware of the new custom class.


  2. Using Interface Builder: in the Classes panel of the NIB window, search for NSView class right click on it an choose Subclass NSView. Giva a name to the subclass and right click over it. Choose Create Files for SubclassName. It will appear a dialog window. Leave it as it is, except for Insert into targets: box. There you have to make sure that your project name check box is selected. To finish click Choose.

No comments: