Developing Cocoa Objective-C Applications: a Tutorial
Total Page:16
File Type:pdf, Size:1020Kb
Inside Mac OS X Developing Cocoa Objective-C Applications: A Tutorial July 2002 Apple Computer, Inc. Simultaneously published in the © 1993-1995, 2000-2002 Apple United States and Canada. Computer, Inc. Even though Apple has reviewed this All rights reserved. manual, APPLE MAKES NO No part of this publication may be WARRANTY OR REPRESENTATION, reproduced, stored in a retrieval EITHER EXPRESS OR IMPLIED, WITH system, or transmitted, in any form or RESPECT TO THIS MANUAL, ITS by any means, mechanical, electronic, QUALITY, ACCURACY, photocopying, recording, or MERCHANTABILITY, OR FITNESS otherwise, without prior written FOR A PARTICULAR PURPOSE. AS A permission of Apple Computer, Inc., RESULT, THIS MANUAL IS SOLD “AS with the following exceptions: Any IS,” AND YOU, THE PURCHASER, ARE person is hereby authorized to store ASSUMING THE ENTIRE RISK AS TO documentation on a single computer ITS QUALITY AND ACCURACY. for personal use only and to print IN NO EVENT WILL APPLE BE LIABLE copies of documentation for personal FOR DIRECT, INDIRECT, SPECIAL, use provided that the documentation INCIDENTAL, OR CONSEQUENTIAL contains Apple’s copyright notice. DAMAGES RESULTING FROM ANY The Apple logo is a trademark of DEFECT OR INACCURACY IN THIS Apple Computer, Inc. MANUAL, even if advised of the Use of the “keyboard” Apple logo possibility of such damages. (Option-Shift-K) for commercial THE WARRANTY AND REMEDIES SET purposes without the prior written FORTH ABOVE ARE EXCLUSIVE AND consent of Apple may constitute IN LIEU OF ALL OTHERS, ORAL OR trademark infringement and unfair WRITTEN, EXPRESS OR IMPLIED. No competition in violation of federal Apple dealer, agent, or employee is and state laws. authorized to make any modification, No licenses, express or implied, are extension, or addition to this warranty. granted with respect to any of the technology described in this book. Some states do not allow the exclusion or Apple retains all intellectual property limitation of implied warranties or rights associated with the technology liability for incidental or consequential described in this book. This book is damages, so the above limitation or intended to assist application exclusion may not apply to you. This developers to develop applications warranty gives you specific legal rights, only for Apple-labeled or and you may also have other rights which Apple-licensed computers vary from state to state. Every effort has been made to ensure that the information in this document is accurate. Apple is not responsible for typographical errors. Apple Computer, Inc. 1 Infinite Loop Cupertino, CA 95014 408-996-1010 Apple, the Apple logo, Cocoa, Mac and Macintosh are trademarks of Apple Computer, Inc., registered in the United States and other countries. Objective-C is a trademark of NeXT Software, Inc., registered in the U.S. and other countries. Contents Chapter 1 Introduction 7 What You’ll Learn in This Tutorial 7 Chapter 2 Designing the Currency Converter Application 11 Designing the Currency Converter Application 11 Object Notation 11 The Model-View-Controller (MVC) Paradigm 13 Model Objects 13 View Objects 14 Controller Object 14 Hybrid Models 14 MVC in Currency Converter’s Design 15 Chapter 3 Building the Currency Converter Application 17 Creating the Currency Converter Project 17 Open Project Builder 18 Choose the New Project Command 18 Select Project Type 18 Creating the Currency Converter Interface 22 Nib Files 23 Open the Main Nib File 23 Windows in Cocoa 24 NSWindow and the Window Server 24 Application, Window, View 25 Key and Main Windows 25 Resize the Window 25 Set the Window’s Title and Attributes 27 Place a Text Field, Resize and Initialize It 28 Duplicate an Object 28 3 Apple Computer, Inc. July 2002 CONTENTS Change the Attributes of a Text Field 29 Assign Labels to the Fields 29 Add a Button to the Interface and Initialize It 32 Add a Horizontal Decorative Line 33 Aqua Layout and Object Alignment 34 Finalize the Window Layout 35 Enable Tabbing Between Text Fields 36 Set the initialFirstResponder for the Window 37 Test the Interface 38 For Free with Cocoa 39 Application and Window Behavior 39 Controls and Text 40 Menu Commands 40 Document Management 41 File Management 41 Communicating With Other Applications 41 Custom Drawing and Animation 41 Localization 42 Editing Support 42 Printing 42 Help 42 Plug-in Architecture 43 Defining the Classes of Currency Converter 43 Classes and Objects 43 Specify a Subclass 44 Paths for Object Communication: Outlets, Targets, and Actions 45 Outlets 45 When You Make a Connection in Interface Builder 46 Target/Action in Interface Builder 47 Which Direction to Connect? 48 Define the Outlets of the Class 48 Define the Actions of the Class 50 Connecting ConverterController to the Interface 50 Generate an Instance of the Class 50 Connect the Custom Class to the Interface 51 Connect the Interface Controls to the Class’s Actions 53 Define the Converter Class 54 Implementing the Classes of Currency Converter 54 4 Apple Computer, Inc. July 2002 CONTENTS Generate the Source Files 54 Objective-C Quick Reference 55 Messages and Method Implementations 55 Declarations 56 Examine an Interface (header) File in Project Builder 57 Add a method declaration 58 Implement Currency Converter’s Classes. 59 Implement the awakeFromNib method 60 Turbo Coding With Project Builder 61 Project Find 61 Integrated Documentation Viewing 61 Indentation 62 Delimiter Checking 62 Emacs Bindings 62 Build, Debug, and Run Currency Converter 62 What Happens When You Build An Application 63 Build the Project 63 Look Up Documentation 64 Debug the Project 66 Using the Graphical Debugger 66 Run Currency Converter 68 Chapter 4 Cocoa Resources 69 Cocoa Developer Documentation 69 Developer Tools Help 70 Mailing Lists 71 Books 71 Other Resources 71 5 Apple Computer, Inc. July 2002 CONTENTS 6 Apple Computer, Inc. July 2002 CHAPTER 1 1 Introduction This tutorial introduces the Cocoa application framework of Mac OS X, and teaches you how to use Apple’s development tools and the Objective-C language to build robust, object-oriented applications. Cocoa provides the best way to build modern, multimedia-rich, objected-oriented applications for consumers and enterprise customers alike. This tutorial assumes familiarity with C programming, but does not assume any previous experience with Cocoa, Project Builder, or Interface Builder. What You’ll Learn in This Tutorial In this tutorial you will build a simple application that converts a dollar amount to an amount in another currency, given the rate of that currency relative to the dollar. Here’s what the finished application looks like: Figure 1-1 The completed Currency Converter application What You’ll Learn in This Tutorial 7 Apple Computer, Inc. July 2002 CHAPTER 1 Introduction Currency Converter is a simple application, yet it exemplifies much of what software development with Cocoa is all about. As you’ll discover, Currency Converter is amazingly easy to create, and it’s equally amazing how many features you get “for free”—as with all Cocoa applications. You can click in one of the text fields to enter a value, or use the tab key to move between them. The conversion is invoked by clicking the Convert button or by pressing the Return key. Because of the features inherited from the Cocoa application environment, you can select the converted amount, copy it (with the Edit menu’s Copy command), and paste it in another application that takes text. This tutorial will lead you through all of the basic steps for creating a Cocoa application. You’ll learn how to: I Create a Project Builder project. I Create a graphical user interface using Interface Builder. I Create a custom subclass of a Cocoa framework class. I Connect an instance of your custom subclass to the interface. By following the steps of this tutorial, you will become more familiar with the two most important Cocoa applications used for application development: Interface Builder and Project Builder. You will also learn the typical work flow of Cocoa application development: 1. Designing the application (your brain) 2. Creating the project (Project Builder) 3. Creating the interface (Interface Builder) 4. Defining the classes (Interface Builder) 5. Implementing the classes (Project Builder) 6. Building the project (Project Builder) 7. Running and testing the application Along the way, you’ll also learn how to design an application using a common object-oriented design paradigm. For additional background on object-oriented programming and Objective-C, see Inside Mac OS X: The Objective-C Programming Language. The tutorial consists of the next two chapters, which you should complete in order: 8 What You’ll Learn in This Tutorial Apple Computer, Inc. July 2002 CHAPTER 1 Introduction 1. “Designing the Currency Converter Application” (page 11) 2. “Building the Currency Converter Application” (page 17) The final chapter, “Cocoa Resources” (page 69), describes where you can go to further improve your knowledge of Cocoa and Mac OS X programming in general. What You’ll Learn in This Tutorial 9 Apple Computer, Inc. July 2002 CHAPTER 1 Introduction 10 What You’ll Learn in This Tutorial Apple Computer, Inc. July 2002 CHAPTER 2 2 Designing the Currency Converter Application This part of the tutorial explains the most common object-oriented design paradigm used in Cocoa, Model-View-Controller, and guides you through the design of the Currency Converter application. Designing the Currency Converter Application An object-oriented application should be based on a design that identifies the objects of the application and clearly defines their roles and responsibilities. You normally work on a design before you write a line of code. You don’t need any fancy tools for designing many applications; a pencil and a pad of paper will do. Object Notation When designing an object-oriented application, it is often helpful to graphically depict the relationships between objects.