Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Tài liệu iPhone Programming: The Big Nerd Ranch Guide docx
PREMIUM
Số trang
476
Kích thước
20.5 MB
Định dạng
PDF
Lượt xem
1748

Tài liệu iPhone Programming: The Big Nerd Ranch Guide docx

Nội dung xem thử

Mô tả chi tiết

iPhone Programming

The Big Nerd Ranch Guide

Joe Conway

Aaron Hillegass

iPhone Programming: The Big Nerd Ranch Guide

by Joe Conway and Aaron Hillegass

Copyright © 2010 Big Nerd Ranch, Inc.

All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permis￾sion must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or

transmission in any form or by any means, electronic, mechanical, photocopying, recoring, or likewise. For informa￾tion regarding permissions, contact

Big Nerd Ranch, Inc.

1963 Hosea L. Williams Drive SE

Suite 209

Atlanta, GA 30317

(404) 478-9005

http://www.bignerdranch.com/

[email protected]

The 10-gallon hat with propeller logo is a trademark of Big Nerd Ranch, Inc.

Exclusive worldwide distribution of the English edition of this book by

Pearson Technology Group

800 East 96th Street

Indianapolis, IN 46240 USA

http://www.informit.com

ISBN-13 978-0321706249

ISBN-10 0321706242

First printing May 2010

The authors and publisher have taken care in writing and printing this book but make no expressed or implied war￾ranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or con￾sequential damages in connection with or arising out of the use of the information or programs contained herein.

App Store, Apple, Bonjour, Cocoa, Cocoa Touch, Finder, Instruments, Interface Builder, iPad, iPhone, iPod, iPod

touch, iTunes, iTunes Store,

Keychain, Leopard, Mac, Mac OS, Multi-Touch, Objective-C, Quartz, Snow Leopard, and Xcode are trademarks of

Apple, Inc., registered in the U.S. and other countries.

Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trade￾marks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the

designations have been printed with initial capital letters or in all capitals.

Acknowledgements

While our names appear on the cover, many people helped make this book a reality. We

would like to take this chance to thank them.

• The other instructors who teach the iPhone Bootcamp fed us with a never-ending stream

of suggestions and corrections. They are Scott Ritchie, Brian Hardy, and Alex von Below.

• Our tireless editor, Susan Loper, took our distracted mumblings and made them into read￾able prose.

• Several technical reviewers helped us find and fix flaws. They are Bill Monk, Mark Miller,

Alex Silverman,

Jonathan Saggau, and Mikey Ward.

• Ellie Volckhausen designed the cover. (The photo is of the bottom bracket of a bicycle

frame.)

• The amazing team at Pearson Technology Group patiently guided us through the busi￾ness end of book

publishing.

The final and most important thanks goes to our students whose questions inspired us to write

this book and whose frustrations inspired us to make it clear and comprehensible.

PDF: CrUmp

請勿打印此書,若條件許可,請購買。

iPhone Programming: The Big Nerd Ranch Guide

Chapter 1. A Simple iPhone Application 1

Creating an Xcode Project 2

Using Interface Builder 4

Model-View-Controller 8

Declarations 10

Declaring methods 12

Making Connections 12

Setting pointers 13

Setting targets and actions 14

Summary 16

Implementing Methods 17

Build and Run on the Simulator 19

Event-driven Programming 20

Application Icons 22

Default Images 24

Chapter 2. Objective-C 26

Using Instances 27

Writing the RandomPossessions Tool 30

NSArray and NSMutableArray 34

Subclassing an Objective-C Class 36

Accessors and properties 40

Instance methods 42

Initializers 43

self 45

super 46

Initializer chain 47

Class methods 48

Exceptions and the Console Window 52

Objective-C 2.0 Additions 54

Chapter 3. Memory Management 56

Memory Management Concepts 56

Managing memory in C 57

Managing memory with objects 58

Reference Counting 59

Using retain counts 60

Avoiding memory leaks with autorelease 62

Managing memory in accessors and properties 64

Retain count rules 65

Managing Memory in RandomPossessions 66

Chapter 4. Delegation and Core Location 73

Delegation 73

Beginning the Whereami Application 77

Using frameworks 77

Core Location 78

Receiving updates from CLLocationManager 81

Releasing Controller Instance Variables 83

Challenge: Heading 84

For the More Curious: Compiler and Linker Errors 84

For the More Curious: Protocols 86

Chapter 5. MapKit and Text Input 89

Object Diagrams 89

Interface Properties 92

Being a MapView Delegate 94

Your own MKAnnotation 97

Tagging locations 100

Text Input and the First Responder 101

Putting the Pieces Together 103

Challenge: Annotation Extras 105

Challenge: Reverse Geocoding 105

Challenge: Changing the Map Type 105

For the More Curious: Renaming an Application 105

Chapter 6. Subclassing UIView 108

Creating a Custom View 108

The drawRect: method 110

Instantiating

a UIView 112

Drawing Text and Shadows 114

Using UIScrollView 115

Zooming 117

Hiding the Status Bar 119

Challenge: Colors 119

For the More Curious: Retain Cycles 119

For the More Curious: Redrawing Views 120

Chapter 7. View Controllers 122

View Controllers and XIB Files 122

Using View Controllers 125

Creating the UITabBarController 126

Creating views for the view controllers 132

The Lifecycle of a View Controller 139

Challenge: Map Tab 141

For the More Curious: Paging 141

Chapter 8. The Accelerometer 143

Setting Up the Accelerometer 144

Getting Accelerometer Data 145

Orientation and Scale of Acceleration 146

Using Accelerometer Data 146

Smoothing Accelerometer Data 148

Detecting Shakes 148

Challenge: Changing Colors 152

For the More Curious: Filtering and Frequency 152

Chapter 9. Notification and Rotation 155

Notification Center 155

UIDevice Notifications 156

Autorotation 158

For the More Curious: Forcing Landscape Mode 162

Challenge: Proximity Notifications 164

For the More Curious: Overriding Autorotation 164

Chapter 10. UITableView and UITableViewController 166

Beginning the Homepwner Application 167

UITableViewController 167

Subclassing UITableViewController 168

UITableView’s Data Source 173

UITableViewDataSource protocol 175

UITableViewCells 178

Reusing UITableViewCells 181

Challenge: Sections 183

Chapter 11. Editing UITableViews 184

Editing Mode 184

Deleting Rows 189

Moving Rows 190

Chapter 12. UINavigationController 198

UINavigationController 199

UINavigationBar 203

An Additional UIViewController 206

The XIB File and File’s Owner 208

Setting up ItemDetailViewController 209

Navigating with UINavigationController 211

Appearing and disappearing views 217

Challenge: Number Pad 218

Chapter 13. Camera and UIImagePickerController 219

ImageCache: a Singleton 219

NSDictionary 220

Taking pictures and UIImagePickerController 227

Creating and using keys 232

Dismissing the Keyboard 237

Challenge: Removing an Image 238

For the More Curious: Recording Video 238

Chapter 14. Saving and Loading 242

Application Sandbox 242

Archiving 245

Writing to Disk with NSData 253

Challenge: Archiving Wherewasi 256

For the More Curious: The Application Bundle 258

Chapter 15. Low-Memory Warnings 262

Handling Low-Memory Warnings 262

Simulating Low-Memory Warnings 266

Chapter 16. Subclassing UITableViewCell 268

Creating HomepwnerItemCell 269

Create subviews 270

Layout subviews 272

Using the custom cell 273

Image Manipulation 275

Challenge: Accessory Views 281

Challenge: Make it Pretty 281

Chapter 17. Multi-Touch, UIResponder, and Using Instruments 282

Touch Events 282

Creating the TouchTracker Application 283

Turning Touches Into Lines 288

The Responder Chain 290

The ObjectAlloc Instrument 292

The Sampler Instrument 296

Challenge: Saving and Loading 298

Challenge: Circles 298

For the More Curious: UIControl 298

Chapter 18. Core Animation Layer 301

Creating a CALayer 303

For the More Curious: Programmatically Generating Content 312

For the More Curious: Layers and Views 313

Challenge: Dynamic Layer Content 316

Chapter 19. Controlling Animation with CAAnimation 317

Animation Objects 317

Spinning the Time with CABasicAnimation 321

Bouncing the Time with a CAKeyframeAnimation 327

Challenge: More Animation 330

For the More Curious: Presentation and Model Layers 330

Chapter 20. Playing Audio and Video 332

Creating the MediaPlayer Application 332

Playing System Sounds 335

Playing Audio Files 339

Playing Movie Files 341

Low-level APIs 344

Challenge: Audio Recording 344

Chapter 21. Web Services 345

Creating the TopSongs Application 345

Setting up the interface 347

Fetching Data From a URL 349

Working with NSURLConnection 350

Parsing XML 353

For the More Curious: The Request Body 358

Challenge: More Data 358

For the More Curious: Credentials 358

Chapter 22. Address Book 361

The People Picker 361

Additions to Possession Class 366

Address Book Functions 368

For the More Curious: That Other Delegate Method 372

Chapter 23. Localization 374

Internationalization using NSLocale 375

Localizing Resources 376

NSLocalizedString and Strings Tables 381

Challenge: Another Localization 384

For the More Curious: NSBundle’s Role in Internationalization 384

Chapter 24. Bonjour 386

Publishing a Service 386

Browsing for Services 388

TXT Record 392

Socket Connections 395

Chapter 25. Settings 398

Settings Bundle 398

NSUserDefaults 402

Registering defaults 402

Using the defaults 403

Chapter 26. SQLite 405

Creating the Nayshunz Application 405

Creating the Database 409

Fetching Data 410

Making and Using the Tree 414

Challenge: Fetching More Data 418

Challenge: Custom Objects 419

Chapter 27. Core Data 420

Creating the Inventory Application 422

Editing the model file 424

AppController 428

LabelSettingViewController 432

LocationListViewController 438

AssetListViewController 442

CountViewController 448

How It All Works 455

Trade-offs of Persistence Mechanisms 457

Challenge 1: Deleting 458

Challenge 2: Custom NSManagedObject Subclasses 458

Chapter 28. Preparing for the iPad 459

Universal Applications 459

Porting existing projects to the iPad 459

Re-designing Wherewasi’s interface 461

More considerations: universal view controllers 463

New Stuff 464

Chapter 1. A Simple iPhone Application

Page 1

Chapter 1. A Simple iPhone Application

In this chapter, you are going to write your first iPhone application. You probably

won’t understand everything that you are doing, and you may feel stupid just

going through the motions. But going through the motions is enough for now.

Mimicry is a powerful form of learning; it is how you learned to speak, and it is how

you will start to do iPhone programming. As you become more capable, you can

experiment and challenge yourself to do creative things on the platform. For now,

just do what we show you. The details will be explained in later chapters.

When you are writing an iPhone application, you must answer two basic

questions:

• How do I get my objects created and configured properly? (Example: “I

want a button here entitled Show Estimate.”)

• How do I deal with user interaction? (Example: “When the user presses

the button, I want this piece of code to be executed.”)

Most of this book is dedicated to answering these questions.

When an iPhone application starts, it puts a window on the screen. You can think

of the window as the canvas on which everything else appears: buttons, labels,

etc. Anything that can appear on the window is a view.

The iPhone SDK is an object-oriented library, and the window and views are

represented by objects. The window is an instance of the class UIWindow. Each

view is an instance of one of several subclasses of UIView. For example, a button

is an instance of UIButton, which inherits from UIView.

Views can be placed on a window in two different ways:

• create views and controls programmatically and add them to the UIWindow

• use Interface Builder to visually lay out views

In this chapter, you will use Interface Builder to visually lay out the views and build

the user interface for your first iPhone application, Quiz (Figure 1.1).

Chapter 1. A Simple iPhone Application

Page 2

Figure 1.1. Your first application

Creating an Xcode Project

Open Xcode and select New Project... from the File menu. A window will appear

giving you several application templates to choose from. Create a barebones

Cocoa Touch application by selecting the Window-Based Application icon (Figure

1.2). Click theChoose... button. A sheet will drop down and ask you to name this

new project. Save it as “Quiz.”

Chapter 1. A Simple iPhone Application

Page 3

Figure 1.2. Creating a new project

Once the project is created, the project window will appear on your screen (Figure

1.3). Take a look at the contents of the Groups and Files table on the left hand

side of the project window. Overall, there are two kinds of files used to create

an application: code and resources. Code is written in Objective-C, C, or C++.

The code files are listed in the Classes and Other Sources groups. Resources

are things like images and sounds that are used by the application at runtime.

The groups in the project window are purely for the organization of files. You can

rename them whatever you want.

Tải ngay đi em, còn do dự, trời tối mất!