The Barnes & Noble Review
Mac developers worldwide are oohing and aahing at Mac OS X and Aqua. Wouldn't it be so cool to be building OS X applications right now? To do it right, though, you've gotta bite the bullet. You need to master Apple's Cocoa framework, and (realistically) you should be working with Apple's language of choice, Objective-C.
While Objective-C isn't a big stretch for experienced C/C++ programmers, the Cocoa framework can be pretty challenging. That's where Aaron Hillegass comes in. His company, Big Nerd Ranch, is the world's leading independent Cocoa training firm. This guy goes back a long way with Steve Jobs: at NeXT, he wrote the first course on OpenStep, the development environment that led to Cocoa. He knows his stuff. His new book, Cocoa Programming for Mac OS X, is utterly authoritative.
It's full of real code, and answers to the questions Mac developers have been asking in Hillegass's seminars. Questions about Aqua interface development. Custom views. Pasteboards. Compilation. Debugging. Most of all, this book reflects a deep understanding of the powerful design patterns that underlie Cocoa. You've got to "think different" to program today's Macs. Nobody's more qualified to show you how.
Bill Camarda is a consultant, writer, and web/multimedia content developer with nearly 20 years' experience in helping technology companies deploy and market advanced software, computing, and networking products and services. He served for nearly ten years as vice president of a New Jerseybased marketing company, where he supervised a wide range of graphics and web design projects. His 15 books include Special Edition Using Word 2000 and Upgrading & Fixing Networks For Dummies®, Second Edition.
Five new chapters were added in this 2nd edition, which discuss creating AppleScriptable applications, integrating OpenGL, adding Undo abilities, creating reusable frameworks, and tinkering with GNUStep, the raw open-source tools for those curious about making Cocoa apps under Linux.
If you're a UNIX or Windows developer who picked up a Mac OS X machine recently in hopes of developing new apps or porting your apps to Mac users, this book should be strongly considered as one of your essential reference and training tomes.
Read an Excerpt
Chapter 12: Custom Views
All the visible objects in an application are either windows or views. In this chapter, you will create a subclass of NSView . From time to time, you will create a
custom view to do custom drawing or event handling. Even if you do not plan to do
custom drawing or event handling, by learning how to create a new view class, you
will learn a lot about how Cocoa works.
Windows are instances of the class NSWindow . Each window has a collection of views.
Each view is responsible for a rectangle of the window. The view draws inside that
rectangle and handles mouse events that occur there. A view may also handle keyboard events. You have worked with several subclasses of NSView already: NSButton, NSTextField, NSTableView, and NSColorWell are all views. (Note that a window is
not a subclass of NSView .)
The View Hierarchy
Views are arranged in a hierarchy (Figure 12.1). The window has a content view that
completely fills its interior. The content view usually has several subviews. Each subview may have subviews of its own. Every view knows its superview, its subviews, and
the window it lives on.
Here are the relevant methods:
Any view can have subviews, but most don't. Here are five views that commonly have
- (NSView *)superview ;
- (NSArray *)subviews ;
- (NSWindow *)window ;
- The content view of a window.
NSBox . The contents of a box are its subviews.
NSScrollView . If a view appears in a scroll view, it is a subview of the scroll
view. The scroll bars are also subviews of the scroll view.
NSSplitView . Each view in a split view is a subview (Figure 12.2).
NSTabView . As the user chooses different tabs, different subviews are
swapped in and out (Figure 12.3).
Get a View to Draw Itself
In this section, you are going to create a very simple view. It will simply appear and
paint itself green. It will look like Figure 12.4.
Create a new project of type Cocoa Application (Figure 12.5).
Name it ImageFun
After the new project is created, open MainMenu. nib, and select NSView in the classes
browser (Figure 12.6).
Press return to create a subclass, and name it StretchView (Figure 12.7).
Create the files for StretchView (Figure 12.8).
Save the files in the project directory.
Create an Instance of a View Subclass
Now create an instance of your class by dragging out a CustomView placeholder and
dropping it on the window (Figure 12.9).
Resize the view to fill most of the window. Open the info panel and set the class of
the view to be StretchView (Figure 12.10).
Notice that creating an instance of a view is different from creating an instance of a
controller object like AppController . To create an instance of AppController in
Chapter 7, you used the Instantiate menu item. When creating a view, it is important
that you attach it to a window and give it a size and location in that window.
Your StretchView object is a subview of the window's content view. An interesting
question is: What happens to the view when the superview resizes? There is a page in
the info panel that allows you to set that behavior. Open the size info panel, and set it
as shown in Figure 12.11. This means that it will grow and shrink as necessary to
keep the distance from its edges to the edges of its superview constant.
If you wanted the view to stay the same size, you could let the distance between the
edges of the view and the edges of the superview grow and shrink. In this exercise,
you do not want this behavior. But in a parallel universe where you did, the inspector
would look like this (Figure 12.12).
Save and close the nib file.
When a view needs to draw itself, it is sent the message drawRect: with the rectangle
that needs to be drawn or redrawn. This method is called automatically, and you will never need to call it directly. Instead, if you know that a view needs redrawing, you
will send the view the message setNeedsDisplay...
What People are saying about this
“Aaron’s book is the gold standard for Mac OS X programming books—beautifully written, and thoughtfully sculpted. The best book on Leopard development.”
—Scott Stevenson, www.theocacao.com
“This is the first book I’d recommend for anyone wanting to learn Cocoa from scratch. Aaron’s one of the few (perhaps only) full-time professional Cocoa instructors, and his teaching experience shows in the book.”
—Tim Burks, software developer and creator of the Nu programming language
“If you’re a UNIX or Windows developer who picked up a Mac OS X machine recently in hopes of developing new apps or porting your apps to Mac users, this book should be strongly considered as one of your essential reference and training tomes.”
—Kevin H. Spencer, Apple Certified Technical Coordinator