
What Readers Are Saying About Cocoa Programming: A Quick-Start Guide for Developers Cocoa Programming is powerful because Daniel Steinberg teaches us the brilliant way Cocoa and Objective C are constructed and commonly used—just what you’d expect from a seasoned, native, local resident. Eric Freeman Author, Head First Design Patterns Over the years, as a programmer experienced in many different languages and paradigms, I’ve come to dread the process of learning new programming languages and technologies. It’s really hard to find a teacher who can speak to experienced programmers without boring us to tears with oversimplification or taking too much prior knowledge for granted. In this book, Daniel Steinberg has proven to be such a teacher. Cocoa Programming exposes the beauty of the Cocoa environment with just enough detail and explanation to help you “get it” the first time. Chad Fowler CTO, InfoEther, Inc. If you are writing applications for the Mac, the iPhone, or the exciting new iPad, this book will get you started. The programming model for all three platforms is essentially the same, and this book will teach it to you. Get this book so you have a solid foundation to write the next big hit. Bill Dudney Gala Factory Software This book is perfect for seasoned developers looking to get started with Cocoa development. Daniel gives you a solid foundation that will allow you to build the next great Mac or iPhone application. James Frye Developer, Tasty Cocoa Software LLC If you’re new to Mac programming or switching from iPhone development, start reading this book now! Cocoa Programming covers topics other books don’t and puts it all together through great examples where you actually learn it and don’t just read it. Jake Behrens Software Engineer, Snafl As a recently initiated iPhone developer with several applications under my belt (and seasoned web applications developer), this book was a perfect fit for my desire to use my knowledge to create robust, functional, and lightweight Cocoa applications. Daniel Steinberg captured my attention early on with his brilliance and kept me intrigued from one chapter to the next. I had no choice but to write my first Cocoa application while reading and felt I walked away with more than just a solid foundation upon which to build. This book will be a mainstay in my library for sure. Kevin J. Garriott Developer II—Mobile Applications, Rockfish Interactive One of the best flowing programming books I’ve ever read. The chapters just naturally follow one after another. The book is a whole, in much the same way the Cocoa framework is a whole. Both reflect a single, clear, concise voice. Craig Castelaz Principle Software Engineer Cocoa Programming A Quick-Start Guide for Developers Daniel H Steinberg The Pragmatic Bookshelf Raleigh, North Carolina Dallas, Texas Many of the designations used by manufacturers and sellers to distinguish their prod- ucts are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://www.pragprog.com Copyright © 2010 Daniel H Steinberg. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmit- ted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-10: 1-9343563-0-1 ISBN-13: 978-1-9343563-0-2 Printed on acid-free paper. P1.0 printing, April 2010 Version: 2010-4-22 Contents 1 Introduction 15 1.1 Moving In ........................... 15 1.2 Learning the Language ................... 18 1.3 Installing the Tools ..................... 19 1.4 Exploring the Frameworks . 20 1.5 In This Book ......................... 21 2 Using What’s There 24 2.1 Creating Your Project in Xcode . 25 2.2 Creating the Appearance with Interface Builder . 26 2.3 Testing the Interface with the Cocoa Simulator . 31 2.4 Finishing the Interface ................... 33 2.5 Wiring Up the Components . 35 2.6 Fixing the Build ....................... 39 2.7 Sharing Your Browser ................... 41 2.8 Exercise: Rinse and Repeat . 42 2.9 The Nib File ......................... 43 3 Methods and Parameters 48 3.1 Sending Messages Without Arguments . 48 3.2 Reading the Docs ...................... 50 3.3 Methods with Arguments . 53 3.4 Dynamic Binding ...................... 56 3.5 Problems Sending Messages . 57 3.6 Links Back to Yourself ................... 58 3.7 Exercise: Multiple Connections . 59 4 Classes and Objects 62 4.1 Creating “Hello, World!” . 62 4.2 Logging Output to the Console . 63 4.3 Using an Existing Class . 66 4.4 Refactoring Code ...................... 69 4.5 Creating a New Class .................... 72 CONTENTS 8 4.6 Creating and Using a Class Method . 75 4.7 Creating a New Object ................... 76 4.8 Further Refactoring ..................... 78 4.9 Initializing Your Objects . 79 4.10 Logging Objects ....................... 82 4.11 Exercise: Other Initializations . 83 4.12 Solution: Other Initializations . 84 5 Instance Variables and Properties 86 5.1 Pointers ............................ 87 5.2 Working with Nonobject Types . 88 5.3 Getters and Setters ..................... 89 5.4 Converting the Accessors to Properties . 92 5.5 Dot Notation ......................... 93 5.6 Property Attributes ..................... 96 5.7 Exercise: Adding Properties . 98 5.8 Solution: Adding Properties . 99 5.9 Removing Instance Variables . 100 6 Memory 102 6.1 Reference Counting . 103 6.2 Finding Leaks with the Clang Static Analyzer . 104 6.3 Fixing the Memory Leak on Mac OS X . 106 6.4 Properties and Garbage Collection . 107 6.5 Creating a Flashlight . 109 6.6 Finding Leaks in Instruments . 111 6.7 Fixing the Memory Leak on the iPhone . 112 6.8 Using Zombies . 113 6.9 Cleaning Up in dealloc . 115 6.10 Retain and Release in a Setter . 116 6.11 The Autorelease Pool . 117 6.12 Using Convenience Constructors . 120 6.13 Exercise: Creating and Using a Convenience Constructor 121 6.14 Solution: Creating and Using a Convenience Constructor 122 7 Outlets and Actions 124 7.1 The Big Picture . 125 7.2 Using an Outlet . 125 7.3 Exercise: Creating and Using an Outlet . 128 7.4 Solution: Creating and Using an Outlet . 129 7.5 Declaring an Action . 130 7.6 Connecting and Implementing the Action . 133 Report erratum this copy is (P1.0 printing, April 2010) CONTENTS 9 7.7 Exercise: Hiding the Button . 135 7.8 Solution: Hiding the Button . 135 7.9 Exercise: Toggling the Interface . 136 7.10 Solution: Toggling the Interface . 136 7.11 Introducing Another Outlet . 137 7.12 Creating Selectors from Strings . 139 8 Creating a Controller 141 8.1 How We’ve Created Objects . 141 8.2 Creating Our Controller Class . 143 8.3 Creating an Instance of Our Controller in IB . 144 8.4 Declaring an Outlet and an Action . 146 8.5 Forward Declaration . 149 8.6 Wiring Up the Controller . 150 8.7 Implementing the Loading of the Previous Page . 150 8.8 Exercise: Finishing the Controller . 150 8.9 Solution: Finishing the Controller . 151 8.10 Awake from Nib . 152 8.11 Disabling and Enabling the Buttons . 153 8.12 Still Needs Work . 156 9 Customizing with Delegates 158 9.1 Understanding Delegates . 158 9.2 The Default Window Behavior . 162 9.3 Turning the Background Red . 162 9.4 Exercise: Turning the Background Green . 165 9.5 Solution: Turning the Background Green . 165 9.6 Application Delegate . 165 9.7 Delegates for Your Web View . 166 9.8 Setting the Window Title . 167 9.9 Exercise: Updating the URL and Setting Buttons . 169 9.10 Solution: Updating the URL and Setting Buttons . 170 9.11 Cleaning Up . 170 9.12 Exercise: Adding a Progress Indicator . 173 9.13 Solution: Adding a Progress Indicator . 173 10 Adapting Our Browser to the iPhone 175 10.1 Creating the iPhone Project . 175 10.2 Creating the Look of Our Browser . 177 10.3 The WebView’s Limitations . 178 10.4 Loading a Web Page at Launch . 179 10.5 Tweaking the Text Field in IB . 181 Report erratum this copy is (P1.0 printing, April 2010) CONTENTS 10 10.6 Using the Text Field Delegate . 183 10.7 Using a Third Delegate to Implement the Buttons . 184 10.8 Exercise: Adding an Activity Indicator . 185 10.9 Solution: Adding an Activity Indicator . 186 10.10 Organizing with Pragma Marks . 188 11 Posting and Listening for Notifications 191 11.1 Exercise: Creating a Model . 192 11.2 Solution: Creating a Model . 192 11.3 Registering for Notifications . 194 11.4 Responding to Workspace Activity . 195 11.5 Holding on to the Controller . 197 11.6 Exercise: Registering for Notifications . 198 11.7 Solution: Registering for Notifications . 198 11.8 Posting Notifications . 199 11.9 Exercise: Receiving the Custom Notifications . 201 11.10 Solution: Receiving the Custom Notifications . 201 12 Creating Protocols for Delegation 203 12.1 Exercise: Creating and Setting the Delegate . 204 12.2 Solution: Creating and Setting the Delegate .
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages454 Page
-
File Size-