Cocoa Programming for Mac OS X

Cocoa Programming for Mac OS X

4.3 3
by Aaron Hillegass
     
 

View All Available Formats & Editions

The best-selling introduction to Cocoa, once again updated to cover the latest Mac programming technologies, and still enthusiastically recommended by experienced Mac OS X developers. 

“Cocoa® Programming for Mac® OS X is considered by most to be the de-facto intro-to-OS X programming text.”

Overview

The best-selling introduction to Cocoa, once again updated to cover the latest Mac programming technologies, and still enthusiastically recommended by experienced Mac OS X developers. 

“Cocoa® Programming for Mac® OS X is considered by most to be the de-facto intro-to-OS X programming text.”

—Bob Rudis, the Apple Blog

 

“I would highly recommend this title to anyone interested in Mac development. Even if you own the previous edition, I think you’ll find the new and revised content well worth the price.”

—Bob McCune, bobmccune.com 

If you’re developing applications for Mac OS X, Cocoa® Programming for Mac® OS X, Fourth Edition, is the book you’ve been waiting to get your hands on. If you’re new to the Mac environment, it’s probably the book you’ve been told to read first.

 

Covering the bulk of what you need to know to develop full-featured applications for OS X, written in an engaging tutorial style, and thoroughly class-tested to assure clarity and accuracy, it is an invaluable resource for any Mac programmer. Specifically, Aaron Hillegass and Adam Preble introduce the two most commonly used Mac developer tools: Xcode and Instruments. They also cover the Objective-C language and the major design patterns of Cocoa. Aaron and Adam illustrate their explanations with exemplary code, written in the idioms of the Cocoa community, to show you how Mac programs should be written. After reading this book, you will know enough to understand and utilize Apple’s online documentation for your own unique needs. And you will know enough to write your own stylish code.

 

Updated for Mac OS X 10.6 and 10.7, this fourth edition includes coverage of Xcode 4, blocks, view-based table views, Apple’s new approach to memory management (Automatic Reference Counting), and the Mac App Store. This edition adds a new chapter on concurrency and expands coverage of Core Animation. The book now devotes a full chapter to the basics of iOS development.

Editorial Reviews

bn.com
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)

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 Jersey–based 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.

Slashdot.org
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.

Product Details

ISBN-13:
9780132902205
Publisher:
Pearson Education
Publication date:
11/09/2011
Sold by:
Barnes & Noble
Format:
NOOK Book
Pages:
528
File size:
69 MB
Note:
This product may take a few minutes to download.

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:

  • (NSView *)superview ;
  • (NSArray *)subviews ;
  • (NSWindow *)window ;
Any view can have subviews, but most don't. Here are five views that commonly have subviews.
  • 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.

Size Info

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.

drawRect:

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

Meet the Author

Aaron Hillegass, who worked at NeXT and Apple, now teaches popular Cocoa programming classes at Big Nerd Ranch. At NeXT, he wrote the first course on OpenStep, the predecessor to today’s Cocoa tools. This book is based on the big Nerd Ranch course and is influenced by more than a decade of work with OpenStep and Cocoa.

 

Adam Preble learned Cocoa programming from the first edition of this book. After too many years of professional C/C++ development, today Adam writes Mac and iOS applications at Big Nerd Ranch, where he is also a Cocoa instructor. He is frequently filling in the gaps between work and family time with pinball machine software development and countless other projects.

Customer Reviews

Average Review:

Write a Review

and post it to your social network

     

Most Helpful Customer Reviews

See all customer reviews >

Cocoa Programming for Mac OS X 4.3 out of 5 based on 0 ratings. 3 reviews.
Anonymous More than 1 year ago
FusionLS More than 1 year ago
This book is very well written. The diagrams and examples shown in this book make the concepts understandable. Many features and topics are covered in this book in regards to Cocoa frameworks.
Anonymous More than 1 year ago