Tuesday, September 23, 2008

5. After the Custom View is created

View Programming Guide for Cocoa points out some foundamental steps a developer has to take in consideration when creating a custom view class. They are listed in the Creating a Custom View chapter.
  1. Providing methods to allocate and deallocate the view.
  2. Drawing the view content.
  3. Marking portion of the view for updating in response to value changes.
  4. Responding to mouse events produced by user interaction.
  5. Updating mouse pointer when it is over a draggable item.
  6. Responding to keyboard events.
  7. Implementing NSResponder actions methods.
  8. Providing key-value-coding compliant accessors for its settable properties.
An important guide to understand Event Handling in Cocoa is Cocoa Event-Handling Guide. This guide covers tips regarding mouse and keyboard events and also NSResponder class.
Another foundamental document is Key-Value Coding Programming Guide that explains point 8.

4. More detailed informations about subclassing NSView

Either custom Quartz views or OpenGL views, need to subclass abstract class NSView.

In the post 1. Quartz subclassing NSView I explained the two possible methods to achieve this result. My favourite one is the first one, because it is the more reliable and compatible with different versions of Xcode.

Now I just want to note once more the whole procedure visually.

  1. In your project right click on the Classes group and choose Add -> New File....


  2. In the New File chooser windows select Mac OS X.


  3. Then, scroll the upper right scroll view to find Objective-C NSView subclass icon, and select it.


  4. Press Next button.


  5. In the next view, give a name to your class and press Finish button.


  6. Make both Xcode and Interface Builder visible and drag your custom view header from Xcode project window to the NIB window, in Interface Builder. If Interface Builder is not opened yet, double click the MainMenu.xib file, in the Resources group of your Xcode project, to open it.


  7. In Interface Builder, select a custom view instance.


  8. Open Inspector (COMMAND-SHIFT-I), move to the Identity panel and choose your custom view class from the Class drop down menu of Class Identity subsection.



Now the custom view instance, in Interface Builder, is defined as an instance of your custom view class. And you can operate on it editing your custom view class code.