Tải bản đầy đủ (.pdf) (10 trang)

Phát triển ứng dụng cho iPhone và iPad - part 2 pptx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (694.5 KB, 10 trang )

CREDITS
EXECUTIVE EDITOR
Carol Long
PROJECT EDITOR
Brian MacDonald
TECHNICAL EDITOR
Michael Gilbert
PRODUCTION EDITOR
Rebecca Anderson
COPY EDITOR
Nancy Rapoport
EDITORIAL DIRECTOR
Robyn B. Siesky
EDITORIAL MANAGER
Mary Beth Wakefi eld
FREELANCER EDITORIAL MANAGER
Rosemarie Graham
MARKETING MANAGER
Ashley Zurcher
PRODUCTION MANAGER
Tim Tate
VICE PRESIDENT AND
EXECUTIVE GROUP PUBLISHER
Richard Swadley
VICE PRESIDENT AND
EXECUTIVE PUBLISHER
Barry Pruett
ASSOCIATE PUBLISHER
Jim Minatel
PROJECT COORDINATOR, COVER
Lynsey Stanford


PROOFREADER
Carrie Hunter, Word One New York
INDEXER
Johnna VanHoose Dinse
COVER DESIGNER
Michael E. Trent
COVER IMAGE
© naphtalina/istockphoto.com
ffirs.indd ixffirs.indd ix 9/17/10 6:52:57 PM9/17/10 6:52:57 PM
ffirs.indd xffirs.indd x 9/17/10 6:52:57 PM9/17/10 6:52:57 PM
ABOUT THE AUTHOR
PATRICK ALESSI has been fascinated with writing computer programs since he fi rst saw his name
fl ash across a terminal in 1980. Since then, he has written software using every language and hard-
ware platform that he could get his hands on, including a brief and painful foray into Fortran on a
VAX system during his engineering education. Patrick holds a B.S. degree in Civil Engineering from
Rutgers University and an M.S. in Computer Science from Stevens Institute of Technology.
Professionally, Patrick has focused on data-centric applications for clients ranging from small
business databases to large-scale systems for the United States Air Force. Currently, he is focused on
the promise of mobility and developing connected applications for mobile devices such as the iPhone
and iPad.
When he can back away from the computer, Patrick enjoys photography, traveling, and doing just
about anything with his family. You can follow him on Twitter at pwalessi and read his blog at
iphonedevsphere.blogspot.com.
ABOUT THE TECHNICAL EDITOR
MICHAEL GILBERT is a long - time systems programmer for various engineering fi rms. He got
his start developing games for the Atari ST, and he was a frequent contributing editor for STart
magazine. Over the years he has continued to develop gaming software on the PC and Mac for
clients worldwide. He ’ s also an expert Flash ActionScript programmer and has produced a popular
Internet gaming environment called HigherGames. He now enjoys developing games for the iPhone
and iPad, and currently has three games in the AppStore (Wordigo, Jumpin ’ Java, and Set Pro HD).

In his spare time, he enjoys trying to defeat his wife Janeen in a friendly game of Scrabble.
ffirs.indd xiffirs.indd xi 9/17/10 6:52:57 PM9/17/10 6:52:57 PM
ffirs.indd xiiffirs.indd xii 9/17/10 6:52:57 PM9/17/10 6:52:57 PM
AC K N O W LED G M ENT S
I WOULD LIKE TO TAKE THIS OPPORTUNITY to thank everyone who made this book possible. Carol
Long, my executive editor, took a chance with a fi rst - time author and shepherded my idea through
the acquisitions process. My project editor, Brian MacDonald, was always there to answer every
question that I had about writing and the publishing process. Mike Gilbert, my technical editor,
gave up valuable app development time to review my work. I would also like to thank all of the
other editorial and production staff that put many hours into this project to help get it to print.
Finally, I would like to thank Jeff LaMarche for providing me with insight into writing a book from
a software developer ’ s perspective, which gave me the courage to move forward with the project.
I cannot thank my wife, Cheryl, and my stepdaughter, Morgan, enough for putting up with my fi ts,
general crankiness, and lack of time for fun family activities as I worked my way through writing
this book. Your patience with me is astounding. I want to thank my Mom for introducing me to
computers at a very young age and teaching me the basics. And fi nally, I want to thank my Dad for
pushing me to work hard and for showing me how to be a father.
ffirs.indd xiiiffirs.indd xiii 9/17/10 6:52:58 PM9/17/10 6:52:58 PM
ffirs.indd xivffirs.indd xiv 9/17/10 6:52:58 PM9/17/10 6:52:58 PM
CONTENTS
INTRODUCTION xxiii
PART I: MANIPULATING AND DISPLAYING DATA ON THE
IPHONE AND IPAD
CHAPTER 1: INTRODUCING DATA-DRIVEN APPLICATIONS 3
Building a Simple Data-Driven Application 3
Creating the Project 4
Adding a UITableView 6
Model-View-Controller Architecture 6
Adding the TableView Programmatically 7
Retrieving Data 8

Implementing Your Data Model Class 9
Displaying the Data 12
Protocols 12
Implementing the UITableViewDataSource Protocol 13
Delegates 15
Finishing Up 15
Further Exploration 17
Design Patterns 17
Reading a Text File 18
Moving Forward 18
CHAPTER 2: THE IPHONE AND IPAD DATABASE: SQLITE 19
What Is SQLite? 20
The SQLite Library 20
SQLite and Core Data 20
Building a Simple Database 21
Designing the Database 22
Creating the Database 24
Populating the Database 26
Creating Records with the INSERT Command 26
Reading Your Rows with the SELECT Command 28
Tools to Visualize the SQLite Database 31
TOC.indd xvTOC.indd xv 9/17/10 6:54:24 PM9/17/10 6:54:24 PM
CONTENTS
xvi
Connecting to Your Database 33
Starting the Project 34
The UINavigationController 34
The UITableViewController 35
The Model Class 36
The DBAccess Class 37

Parameterized Queries 47
Writing to the Database 49
Displaying the Catalog 50
Viewing Product Details 52
Moving Forward 55
CHAPTER 3: DISPLAYING YOUR DATA: THE UITABLEVIEW 57
Customizing the TableView 57
TableViewCell Styles 58
Adding Subviews to the contentView 61
Subclassing UITableViewCell 65
Getting Started 66
Implementing drawRect: 69
Finishing Up 71
Implementing Sections and an Index 72
Implementing Search 78
Optimizing TableView Performance 85
Reusing Existing Cells 85
Opaque Subviews 86
Custom Drawn Cells with drawRect 88
UI Conventions for Accessory Views 88
Moving Forward 88
CHAPTER 4: IPAD INTERFACE ELEMENTS 89
Displaying Master/Detail Data with the UISplitViewController 89
Introducing the UISplitViewController 90
The UISplitViewControllerDelegate Protocol 92
Starting the Split View Sample Application 93
Building the Detail Interface 95
Implementing Save and Master/Detail View 97
Setting Up the DetailViewController 97
Changes to the RootViewController 99

Modify the TableView Methods 100
Adding Surveys 101
TOC.indd xviTOC.indd xvi 9/17/10 6:54:25 PM9/17/10 6:54:25 PM
CONTENTS
xvii
Displaying Data in a Popover 103
Building the InfoViewController 104
Displaying the UIPopoverController 107
Gesture Recognizers 110
The UIGestureRecognizer Class 110
Using Gesture Recognizers 111
File Sharing Support 115
Enable File Sharing in the Sample Application 115
Serializing the Survey Data Array 115
Deserializing and Loading the Survey Data Array 117
Sharing the Data 118
Moving Forward 119
PART II: MANAGING YOUR DATA WITH CORE DATA
CHAPTER 5: INTRODUCING CORE DATA 123
The Basics of Core Data 124
The Core Data Architecture 124
The Core Data Stack 124
The Data Store 125
The Persistent Store Coordinator 125
The Managed Object Model 126
The Managed Object Context 126
SQLite and Core Data 127
Using Core Data: A Simple Task Manager 127
Creating the Project 127
Examining the Template Code 128

TasksAppDelegate 128
The Data Model 131
RootViewController 132
Modifying the Template Code 137
Moving Forward 143
CHAPTER 6: MODELING DATA IN XCODE 145
Modeling Your Data 145
Defi ning Entities and Their Attributes 146
Entity Details 147
Adding Attributes 149
Adding Relationships Between Entities 150
TOC.indd xviiTOC.indd xvii 9/17/10 6:54:25 PM9/17/10 6:54:25 PM
CONTENTS
xviii
Creating Fetched Properties and Fetch Request Templates 153
Fetched Properties 153
Fetch Request Templates 155
Creating Custom NSManagedObject Subclasses 155
Implementing Validation Rules 158
Implementing Default Values 159
Creating the Tasks Model 159
Moving Forward 161
CHAPTER 7: BUILDING A CORE DATA APPLICATION 163
The Tasks Application Architecture 163
The Data Model 164
The Class Model 164
The User Interface 165
Coding the Application 167
RootViewController and the Basic UI 167
Generating the Managed Object Subclasses 171

Adding and Viewing Tasks 172
Building the ViewTaskController 173
Changes to the RootViewController 178
Building the Editing Controllers 180
Editing Text with the EditTextController 181
Setting Priorities with the EditPriorityController 185
Adding and Editing Locations with the EditLocationController 189
Modifying Dates with the EditDateController 195
Finishing Up the Editing Controllers 199
Displaying Results in the RootViewController 203
Sorting Results with NSSortDescriptor 203
Filtering Results with NSPredicate 204
Generating Grouped Tables Using the NSFetchedResultsController 205
Implementing Custom Managed Objects 211
Coding a Dynamic Property 211
Defaulting Data at Runtime 212
Validating a Single Field 213
Multi-Field Validation 215
Moving Forward 217
CHAPTER 8: CORE DATA–RELATED COCOA FEATURES 219
Key-Value Coding 220
Keys and Keypaths 220
Setting Values Using Keys 221
TOC.indd xviiiTOC.indd xviii 9/17/10 6:54:26 PM9/17/10 6:54:26 PM

×