In this chapter, we’ll play with a number of simple, trivial NSView subclasses to learn more about how drawRect: and Interface Builder work. The first view is BlackView, an NSView whose drawRect: method fills the NSView with black.
Launch Project Builder and choose PB’s File → New Project menu command.
Choose Application → Cocoa Application in the New Project Assistant and give your new project the name “ViewDemo”.
Double-click the MainMenu.nib
file in the
Resources group in the Groups & Files pane to launch IB and
display the MainMenu.nib
window.
Select the NSView class under the Classes tab of IB’s Nib File window and subclass NSView by choosing the Classes → Subclass NSView menu command.
Change the name of the new subclass from “MyView” to “BlackView”.
Choose Classes → Create Files for BlackView and insert the class files into the ViewDemo project.
Back in PB, insert the drawRect:
method shown here in bold into BlackView.m
:
#import "BlackView.h" @implementation BlackView - (void)drawRect:(NSRect)aRect { [ [NSColor blackColor] set]; [NSBezierPath fillRect:aRect]; } @end
Back in IB, resize the empty window titled “Window” so it’s about three inches wide and one inch tall. The exact size isn’t important here.
Drag a CustomView icon from IB’s Cocoa-Containers palette and drop it in the empty window.
Type Command-1 and then change the class of the custom NSView to BlackView in the NSView (Custom) Info window. See Figure 15-1.
Resize the BlackView instance so it’s about the size shown in the window in Figure 15-1.
Back in IB, choose File → Test Interface to test the ViewDemo interface. The window titled “Window” is empty — it contains no BlackView.
Now let’s take a short diversion to find out a
little bit about IB using the system console application Console,
which resides in the /Applications/Utilities
folder. The Console application displays technical messages from Mac
OS X, including errors and logged output from NSLog( )
calls in applications (the same ones that show up in
PB’s Run pane when an application is run from within
PB).
Launch the Console application. In the Console window, you’ll see a message like the following one, which you can also see in Figure 15-2:
2002-03-27 21:34:17.628 Interface Builder[451] Unknown class 'BlackView' in nib file, using 'NSView' instead.
Quit Test Interface mode by typing Command-Q.
This message appears because IB doesn’t know about BlackView’s implementation — it isn’t compiled into the version of IB that you’re using. Note that you can create your own custom palettes (e.g., a palette including a compiled BlackView similar to CustomView) in IB if you wish; consult the IB documentation to learn how to do this.
To see BlackView work, we first need to build the application in PB.
Back in PB, build and run ViewDemo with BlackView, saving all files when prompted.
This time you’ll see the BlackView instance in all its glory, as shown in the window in Figure 15-3.
Quit ViewDemo.
That’s all there is to it! BlackView’s drawRect: method is invoked automatically when the window is first drawn on the screen; there’s no need to explicitly invoke the drawRect: method yourself. (In fact, you’re never supposed to invoke drawRect: directly.) Likewise, if we tried to print this window, BlackView’s drawRect: method would automatically be invoked to generate the commands necessary to send the image to the printer.
To stress this point, let’s modify the window by adding a few more BlackViews, as shown in the window on the left in Figure 15-4. You can do this with the help of IB’s Edit → Copy to and Edit → Paste menu commands.
Back in IB, resize the BlackView so that it’s only about one-fourth of its original size.
With the BlackView selected, choose IB’s Edit → Copy to and then Edit → Paste menu commands.
Choose Edit → Paste twice more so you have a total of four BlackViews, then move and resize them in the window (see the window on the left in Figure 15-4).
Back in PB, build and run ViewDemo again. Quit after admiring your creation.
When we run ViewDemo this time, we get a window that looks like the one on the right in Figure 15-4. Cocoa automatically calls the drawRect: method for each of the four BlackView instances when the window is displayed. Note that although there are four instances and four sets of instance variables (inherited from NSView), there is only one copy of drawRect: in memory.
Note how quickly ViewDemo was built in PB this time. The reason is
that no code had to be compiled. The only changes made were in the
MainMenu.nib
file — PB only had to swap out
the old nib for this new one to create the new
ViewDemo.app
application bundle. This works
because nib files are not bundled into Mac OS executables — they
are stored as separate resources (files) in the application
bundle
18.216.200.106