2011-Head First iPhone & iPad Development (2nd Edition)

Advance Praise for Head First iPhone and iPad Development “Rather than textbook-style learning, Head First iPhone and iPad Development brings a humoro...

16 downloads 1821 Views 32MB Size

Advance Praise for Head First iPhone and iPad Development “Rather than textbook-style learning, Head First iPhone and iPad Development brings a humorous, engaging, and even enjoyable approach to learning iOS development. With coverage of key technologies including Core Data and even crucial aspects such as interface design, the content is aptly chosen and top-notch. Where else could you witness a fireside chat between a UIWebView and UITextField?” — Sean Murphy, iOS designer and developer “Head First iPhone and iPad Development explains iOS application development from the ground up. Major enhancements to the first edition cover the changes associated with iOS 4, Xcode 4, and (re-)writing apps for use on the iPad. The step-by-step approach with an emphasis on the visual makes this a great way to learn iPhone and iPad app development, from the basics to advanced features.” — Rich Rosen, software developer and co-author of Mac OS X for Unix Geeks “The great thing about this book is its simple, step-by-step approach. It doesn’t try to teach everything—it just launches you right into building iOS applications in a friendly, conversational way. It’s a fantastic book for people who already know how to write code and just want to get straight into the meat of building iOS applications.” — Eric Shephard, owner of Syndicomm

“Head First iPhone and iPad Development was clearly crafted to get you easily creating, using, and learning iOS technologies without needing a lot of background with Macintosh development tools.” — Joe Heck, Seattle Xcoders founder

“This book is infuriating! Some of us had to suffer and learn iOS development ‘the hard way,’ and we’re bitter that the jig is up.” — Mike Morrison, Stalefish Labs founder

“Head First iPhone and iPad Development continues the growing tradition of taking complex technical subjects and increasing their accessibility without reducing the depth and scope of the content. iOS development is a steep learning curve to climb by any measure, but with Head First iPhone and iPad Development, that curve is accompanied with pre-rigged ropes, a harness, and an experienced guide! I recommend this book for anyone who needs to rapidly improve their understanding of developing for this challenging and exciting platform.” — Chris Pelsor, snogboggin.com

Praise for other Head First books “Head First Object-Oriented Analysis and Design is a refreshing look at subject of OOAD. What sets this book apart is its focus on learning. The authors have made the content of OOAD accessible, usable for the practitioner.” — Ivar Jacobson, Ivar Jacobson Consulting “I just finished reading HF OOA&D and I loved it! The thing I liked most about this book was its focus on why we do OOA&D—to write great software!” — Kyle Brown, Distinguished Engineer, IBM “Hidden behind the funny pictures and crazy fonts is a serious, intelligent, extremely well-crafted presentation of OO Analysis and Design. As I read the book, I felt like I was looking over the shoulder of an expert designer who was explaining to me what issues were important at each step, and why.” — Edward Sciore, Associate Professor, Computer Science Department, Boston College “All in all, Head First Software Development is a great resource for anyone wanting to formalise their programming skills in a way that constantly engages the reader on many different levels.” — Andy Hudson, Linux Format “If you’re a new software developer, Head First Software Development will get you started off on the right foot. And if you’re an experienced (read: long-time) developer, don’t be so quick to dismiss this...” — Thomas Duff, Duffbert’s Random Musings “There’s something in Head First Java for everyone. Visual learners, kinesthetic learners, everyone can learn from this book. Visual aids make things easier to remember, and the book is written in a very accessible style—very different from most Java manuals…Head First Java is a valuable book. I can see the Head First books used in the classroom, whether in high schools or adult ed classes. And I will definitely be referring back to this book, and referring others to it as well.” — Warren Kelly, Blogcritics.org, March 2006

Praise for other Head First books “Another nice thing about Head First Java, 2nd Edition is that it whets the appetite for more. With later coverage of more advanced topics such as Swing and RMI, you just can’t wait to dive into those APIs and code that flawless, 100000-line program on java.net that will bring you fame and venture-capital fortune. There’s also a great deal of material, and even some best practices, on networking and threads— my own weak spot. In this case, I couldn’t help but crack up a little when the authors use a 1950s telephone operator—yeah, you got it, that lady with a beehive hairdo that manually hooks in patch lines—as an analogy for TCP/IP ports...you really should go to the bookstore and thumb through Head First Java, 2nd Edition. Even if you already know Java, you may pick up a thing or two. And if not, just thumbing through the pages is a great deal of fun.” — Robert Eckstein, Java.sun.com, April 2005 “Of course it’s not the range of material that makes Head First Java stand out, it’s the style and approach. This book is about as far removed from a computer science textbook or technical manual as you can get. The use of cartoons, quizzes, fridge magnets (yep, fridge magnets…). And, in place of the usual kind of reader exercises, you are asked to pretend to be the compiler and compile the code, or perhaps to piece some code together by filling in the blanks or…you get the picture. The first edition of this book was one of our recommended titles for those new to Java and objects. This new edition doesn’t disappoint and rightfully steps into the shoes of its predecessor. If you are one of those people who falls asleep with a traditional computer book then this one is likely to keep you awake and learning.” — TechBookReport.com, June 2005 “Head First Web Design is your ticket to mastering all of these complex topics, and understanding what’s really going on in the world of web design...If you have not been baptized by fire in using something as involved as Dreamweaver, then this book will be a great way to learn good web design. ” — Robert Pritchett, MacCompanion, April 2009 Issue “Is it possible to learn real web design from a book format? Head First Web Design is the key to designing user-friendly sites, from customer requirements to hand-drawn storyboards to online sites that work well. What sets this apart from other ‘how to build a web site’ books is that it uses the latest research in cognitive science and learning to provide a visual learning experience rich in images and designed for how the brain works and learns best. The result is a powerful tribute to web design basics that any general-interest computer library will find an important key to success.” — Diane C. Donovan, California Bookwatch: The Computer Shelf “I definitely recommend Head First Web Design to all of my fellow programmers who want to get a grip on the more artistic side of the business. ” — Claron Twitchell, UJUG

Other related books from O’Reilly iOS 4 Programming Cookbook Programming iOS 4 Augmented Reality in iOS Graphics and Animation in iOS iOS 4 Sensor Programming Writing Game Center Apps in iOS App Savvy

Other books in O’Reilly’s Head First series Head First C# Head First Java Head First Object-Oriented Analysis and Design (OOA&D) Head First HTML with CSS and XHTML Head First Design Patterns Head First Servlets and JSP Head First EJB Head First SQL Head First Software Development Head First JavaScript Head First Physics Head First Statistics Head First Ajax Head First Rails Head First Algebra Head First PHP & MySQL Head First PMP Head First Web Design Head First Networking

Head First iPhone and iPad Development Second Edition Wouldn’t it be dreamy if there was a book to help me learn how to develop iOS apps that was more fun than going to the dentist? It’s probably nothing but a fantasy…

Dan Pilone Tracey Pilone

Beijing • Cambridge • Farnham • Kln • Sebastopol • Tokyo

Head First iPhone and iPad Development by Dan Pilone and Tracey Pilone Copyright © 2011 Dan Pilone and Tracey Pilone. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly Media books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/ institutional sales department: (800) 998-9938 or [email protected]

Vinny

Series Creators:

Kathy Sierra, Bert Bates

Editor:

Courtney Nash

Cover Designer:

Karen Montgomery

Production Editor:

Holly Bauer

Indexer:

Julie Hawks

Proofreader:

Nancy Reinhardt

Page Viewers:

Vinny and Nick



Printing History:

Nick

October 2009: First Edition. June 2011: Second Edition.

The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. The Head First series designations, Head First iPhone and iPad Development, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and the authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. No PCs were harmed in the making of this book. ISBN: 978-1-449-38782-2 [M]

To Dan, my best friend, husband, and now business partner; and Vinny and Nick, the best boys a mother could ask for. —Tracey This book is dedicated to my family: my parents who made all of this possible, my brothers who keep challenging me, and my wife and sons, who don’t just put up with it—they help make it happen. —Dan

the authors

Dan

Tracey Dan Pilone is the founder and Managing

Partner of Element 84, a consulting and mobile software development company. He has designed and implemented systems for NASA, Hughes, ARINC, UPS, and the Naval Research Laboratory. Dan has taught a number of iPhone and iPad development courses for O’Reilly, iPhone Bootcamp, and private development teams. He has taught project management, software design, and software engineering at The Catholic University in Washington, DC. Dan’s previous Head First books are Head First Software Development and Head First Algebra, so he’s used to them being a little out of the ordinary, but this is the first book to involve bounty hunters. Dan’s degree is in computer science with a minor in mathematics from Virginia Tech and he is one of the instructors for the O’Reilly iPhone Development Workshop.

viii

Tracey Pilone is a project manager with

Element 84, a startup in the DC area that offers mobile, Web, and backend development services. Recent projects she has worked on include writing for Naval Research Labs and Academic Business Consultants, as well as contributing to other Head First titles and keeping involved with the software development projects at Element 84. Before working as a writer, she spent several years working in and around the Washington, DC, area for two of ENR’s (Engineering News Record) top 20 contractors as a construction manager in commercial construction. She is also the coauthor of Head First Algebra.

Tracey has a civil engineering degree from Virginia Tech and a masters of education from the University of Virginia, and holds a Professional Engineer’s License in Virginia.

table of contents

Table of Contents (Summary) Intro

xxiii

1

Getting Started: Going Mobile with iOS

1

2

iOS App Patterns: Hello, Renee!

41

3

Objective-C for iOS: Email Needs Variety

99

4

Multiple Views: A Table with a View

141

5

Plists and Modal Views: Refining Your App

197

6

Saving, Editing, and Sorting Data: Everyone’s an Editor...

251

7

Migrating to iPad: We Need More Room

315

8

Tab Bars and Core Data: Enterprise Apps

361

9

Migrating and Optimizing with Core Data: Things Are Changing

445

10

Camera, Map Kit, and Core Location: Proof in the Real World

501

11

iPad UI: Natural Interfaces

563

i

Leftovers: The Top 4 Things (We Didn’t Cover)

601

ii

Preparing an App for Distribution: Get Ready for the App Store

611

Table of Contents (the real thing) Intro Your brain on iOS Development. Here you are trying to learn something, while here your brain is doing you a favor by making sure the learning doesn’t stick. Your brain’s thinking, “Better leave room for more important things, like which wild animals to avoid and whether naked snowboarding is a bad idea.” So how do you trick your brain into thinking that your life depends on knowing enough to develop your own iPhone and iPad apps? Who is this book for?

xxiv

Metacognition: thinking about thinking

xxvii

Here’s what YOU can do to bend your brain into submission

xxix

Read me

xxx

The technical review team

xxxii

Acknowledgments

xxxiii

ix

table of contents

getting started

1

Going Mobile with iOS The iPhone changed everything.  The iPhone 4 “changed everything, again.” And now you’ve got the iPad to contend with, too. iOS devices are now capable word processors, e-readers, and video cameras. They are being used in business and medicine as enterprise devices and the App Store is a platform for every developer to use, from one-man shows to big name companies. Apple provides the software and we’ll help you with the knowledge; we’re sure you’ve got the enthusiasm covered.

What should I do?

x

So, you want to build an iOS app...

2

...’cause everyone wants one!

3

Apps live in an iTunes universe

4

Time to make a decision

5

It all starts with the iPhone SDK

6

Take a look around

7

Xcode includes app templates to help you get started

8

Xcode is a full-featured IDE

9

Xcode is the hub of your iOS project

10

Build your interface within Xcode

14

Add the button to your view

15

The iOS simulator lets you test your app on your Mac

17

iDecide’s logic

18

Changing the button text

19

You’re using the Model View Controller pattern

23

iDecide is actually a little simpler

24

What happened?

26

Use the GUI editor to connect UI controls to code

27

A component can trigger certain events

28

Connect your events to methods

29

You’ve built your first iPhone app!

31

Your iOS Toolbox

39

table of contents

2

iOS app patterns Hello, Renee! Apps have a lot of moving parts. OK, actually, they don’t have any real moving parts, but they do have lots of UI controls. A typical iPhone app has more going on than just a button, and now it’s time to build one. Working with some of the more complicated widgets means you’ll need to pay more attention than ever to how you design your app as well. In this chapter, you’ll learn how to put together a bigger application and some of the fundamental design patterns used in the iOS SDK.

First we need to figure out what Mike (really) wants

43

App design rules—the iOS HIG

48

HIG guidelines for pickers and buttons

51

Create a new View-based project for InstaEmail

52

The life of a root view

54

We need data

60

Use pickers when you want controlled input

61

Pickers get their data from a datasource...

62

That pattern is back

63

First, declare that the controller conforms to both protocols

68

The datasource protocol has two required methods

70

Connect the datasource just like actions and outlets

71

There’s just one method for the delegate protocol

72

Actions, outlets, and events

77

Connect the event to the action

81

Next, synthesize the property...

85

Connect the picker to our outlet

86

Use your picker reference to pull the selected values

87

Your iOS Toolbox

96

xi

table of contents

3

objective-c for iOS Email needs variety We did a lot in Chapter 2, but what language was that? Parts of the code you’ve been writing might look familiar, but it’s time you got a sense of what’s really going on under the hood. The iOS SDK comes with great tools that mean you don’t need to write code for everything, but you can’t really write apps without learning something about the underlying language, including properties, message passing, and memory management. Unless you work that out, all your apps will be just default widgets! And you want more than just widgets, right?

Messages going here between textField and the controller.

xii

Renee is catching on...

100

Make room for custom input

101

Header files describe the interface to your class

103

Auto-generated accessors also handle memory management

109

To keep your memory straight, you need to remember just two things

111

But when Mike’s finished typing...

121

Customize your UITextField

123

Components that use the keyboard ask it to appear...

124

Ask the UITextField to give up focus

125

Messages in Objective-C use named arguments

127

Use message passing to tell our View Controller when the Done button is pressed

128

Where’s the custom note?

132

Your Objective-C Toolbox

139

table of contents

4

multiple views A table with a view Most iOS apps have more than one View. We’ve written a cool app with one view, but anyone who’s used a smartphone knows that most apps aren’t like that. Some of the more impressive iOS apps out there do a great job of working with complex information by using multiple views. We’re going to start with navigation controllers and table views, like the kind you see in your Mail and Contact apps. Only we’re going to do it with a twist...

So, how do these views fit together?

145

The navigation template pulls multiple views together

146

The table view is built in

147

A table is a collection of cells

152

Just a few more drinks...

160

Plists are an easy way to save and load data

162

Arrays (and more) have built-in support for plists

165

Use a detail view to drill down into data

168

A closer look at the detail view

169

Use the Navigation Controller to switch between views

179

Navigation Controllers maintain a stack of View Controllers

180

Dictionaries store information as key-value pairs

184

Debugging—the dark side of iOS development

187

First stop on your debugging adventure: the console

188

Interact with your application while it’s running

189

Xcode supports you after your app breaks, too

190

The Xcode debugger shows you the state of your application

191

Your iOS Toolbox

195

xiii

table of contents

5

plists and modal views Refining your app So you have this almost-working app... That’s the story of every app! You get some functionality working, decide to add something else, need to do some refactoring, and respond to some feedback from the App Store. Developing an app isn’t always ever a linear process, but there’s a lot to be learned along the way.

Anatomy of a crash

Dictionary name = Cupid’s Cocktail ingredients = Cherry liqueur, peach ... directions = Shake ingredients and strain into...

It all started with Sam...

198

Use the debugger to investigate the crash

200

Update your code to handle a plist of dictionaries

203

The Detail View needs data

206

The other keys are key

207

We have a usability problem

213

Use a disclosure button to show there are more details available

215

Sales were going strong

218

Use Navigation Controller buttons to add drinks

223

The button should create a new view

227

We need a view...but not necessarily a new view

228

The View Controller defines the behavior for the view

229

A nib file contains the UI components and connections...

230

You can subclass and extend view controllers like any other class 231

xiv

Modal views focus the user on the task at hand...

236

Any view can present a modal view

237

Our modal view doesn’t have a navigation bar

242

Create the Save and Cancel buttons

244

Write the Save and Cancel actions

245

Your iOS Toolbox

249

table of contents

6

saving, editing, and sorting data Everyone’s an editor... Displaying data is nice, but adding and editing information is what makes an app really hum. DrinkMixer is great—it uses some cell customization, and works with plist dictionaries to display data. It’s a handy reference application, and you’ve got a good start on adding new drinks. Now, it’s time to give the user the ability to modify the data—saving, editing, and sorting—to make it more useful for everyone. In this chapter, we’ll take a look at editing patterns in iOS apps and how to guide users with the Nav Controller.

NSNotification object

Sam is ready to add a Red-Headed School Girl...

252

...but the keyboard is in the way

253

Wrap your content in a scroll view

255

The scroll view is the same size as the screen

257

The keyboard changes the visible area

260

iOS notifies you about the keyboard

262

Register with the default notification center for events

263

Keyboard events tell you the keyboard state and size

269

The table view doesn’t know its data has changed

288

The array is out of order, too

292

Table views have built-in support for editing and deleting

300

Your iOS Development Toolbox

313

Sam has another project in mind...

314

xv

table of contents

7

migrating to iPad We need more room iPhones are great, but a bigger screen can be better.  When the iPad first launched, some panned it by saying that it was “just a big iPhone” (but uh, without the phone). In many ways it is, but that screen opens up many opportunities for better user interaction. More screen real estate means that reading is comfortable, web pages are easily viewed, and the device can act more like a book. Or a calendar. Or many other things that you already know how to use, like a menu...

Tonight’s talk: Universal App Distribution or not?

xvi

DrinkMixer on the iPad

316

The iPad simulator

318

The HIG covers iPads, too

319

Use Xcode to build your Universal app

326

Check your devices

334

Rotation is key with iPad

337

A persistent view problem

345

Don’t forget the tableview

346

Your iOS Development Toolbox

359

table of contents

8

tab bars and core data Enterprise apps Enterprise apps mean managing more data in different ways. Companies large and small are a significant market for iPhone and iPad apps. A small handheld device with a custom app can be huge for companies that have staff on the go. Most of these apps are going to manage lots of data, and since iOS 3.0, there has been built-in Core Data support. Working with that and another new controller, the tab bar controller, we’re going to build an app for justice!

HF bounty hunting

362

A new iPhone control

368

Choose a template to start iBountyHunter

372

There’s a different structure for universal apps

374

Drawing how iBountyHunter iPhone works...

376

...and how it fits with the universal app

377

Build the fugitive list view

382

Next up: the Captured view

384

A view’s contents are actually subviews

392

After a quick meeting with Bob...

394

Core Data lets you focus on your app

396

Core Data needs to know what to load

397

Core Data describes entities with a Managed Object Model

398

Build your Fugitive entity

399

Use an NSFetchRequest to describe your search

410

Bob’s database is a resource

417

Back to the Core Data stack

418

The template sets things up for a SQLite DB

419

iOS Apps are read-only

421

The iPhone’s application structure defines where you can read and write

422

Copy the database to the Documents directory

423

Your Core Data Toolbox

444

Fugitive

xvii

table of contents

9

migrating and optimizing with core data Things are changing We have a great app in the works. iBountyHunter successfully loads the data Bob needs and lets him view the fugitives easily. But what about when the data has to change? Bob wants some new functionality, and what does that do to the data model? In this chapter, you’ll learn how to handle changes to your data model and how to take advantage of more Core Data features.

captured - Boolean

- NOT Optional - NO by default

xviii

Bob needs documentation

446

Everything stems from our object model

449

The data hasn’t been updated

452

Data migration is a common problem

453

Migrate the old data into the new model

454

Xcode makes it easy to version your data model

455

Core Data can “lightly” migrate data

457

Here’s what you’ve done so far...

461

Bob has some design input

462

Your app has a lifecycle all its own...

472

Multitasking rules of engagement

473

A quick demo with Bob

476

Use predicates for filtering data

478

We need to set a predicate on our NSFetchRequest

479

Core Data controller classes provide efficient results handling

486

Time for some high-efficiency streamlining

487

Create the new FetchedResultsController getter method

488

We need to refresh the data

493

Your Data Toolbox

499

table of contents

10

camera, map kit, and core location Proof in the real world iOS devices know where they are and what they see. As any iPhone, iPod Touch, or iPad user knows, these devices go way beyond just managing data: they can also take pictures, figure out your location, and put that information together for use in your app. The beauty about incorporating these features is that just by tapping into the tools that iOS gives you, suddenly you can import pictures, locations, and maps without much coding at all.

For Bob, payment requires proof

502

The way to the camera...

511

There’s a method for checking

521

Prompt the user with action sheets

522

Bob needs the where, in addition to the when

528

Core Location can find you in a few ways

534

Add a new framework

536

Just latitude and longitude won’t work for Bob

544

Map Kit comes with iOS

545

A little custom setup for the map

546

Annotations require a little more work

553

Fully implement the annotation protocol

554

Your Location Toolbox

561

xix

table of contents

11

iPad UI Natural interfaces The iPad is all about existing in the real world.  We’ve built a basic iPad port of an existing app for DrinkMixer a few chapters back, but now it’s time to build an interface that works with some real-world knowledge. By mimicking things that people use in the real world, users know what to do with an interface just by opening the app. We’re going to use some real-world elements to help Bob catch the bad guys…

[NSString stringWith Format: @”...

.css

Fugitive.css

xx

Bob needs that iPad app, too...

564

iOS HIG user experience guidelines

567

Iterate your interface, too

568

BountyHunterHD is based on a split-view controller

570

Unifying the custom stuff

580

It seems we have a problem...

585

UIWebview has lots of options

586

HTML, CSS and Objective-C

588

Using UIWebView

588

Your NUI Toolbox

599

table of contents

i

appendix i, leftovers The top 4 things (we didn’t cover) Ever feel like something’s missing? We know what you mean... Just when you thought you were done, there’s more. We couldn’t leave you without a few extra details, things we just couldn’t fit into the rest of the book. At least, not if you want to be able to carry this book around without a metallic case with castor wheels on the bottom. So take a peek and see what you (still) might be missing out on.

#1. Internationalization and Localization

602

Localizing string resources

604

#2. View animations

606

#3. Accelerometer

607

Understanding device acceleration

608

#4. A word or two about gaming...

609

Quartz and OpenGL

610

xxi

table of contents

ii

appendix ii, preparing an app for distribution Get ready for the App Store You want to get your app in the App Store, right? So far, we’ve basically worked with apps in the simulator, which is fine. But to get things to the next level, you’ll need to install an app on an actual iPhone, iPad, or iPod Touch before applying to get it in the App Store. And the only way to do that is to register with Apple as a developer. Even then, it’s not just a matter of clicking a button in Xcode to get an app you wrote on your personal device. To do that, it’s time to talk with Apple.

xxii

Apple has rules

612

The Provisioning Profile pulls it all together

613

Keep track in the Organizer

614

how to use this book

Intro I can’t believe they put that in an iOS development book!

: er the burning question In this section, we antswthat in an iOS development book?” “So why DID they pu

  xxiii

how to use this book

Who is this book for? If you can answer “yes” to all of these: 1

Do you have previous development experience? Do you want to learn, understand, remember, and

2 apply important iOS design and development concepts so that you can write your own iPhone and iPad apps and start selling them in the App Store?

3

It definitely helps if you’ve already got some object-oriented chops, too. Experience with Mac development is helpful, but definitely not required.

Do you prefer stimulating dinner party conversation to dry, dull, academic lectures?

this book is for you.

Who should probably back away from this book? If you can answer “yes” to any of these: 1

Are you completely new to software development?

2

Are you already developing iOS apps and looking for a reference book on Objective-C?

3

Are you afraid to try something different? Would you rather have a root canal than mix stripes with plaid? Do you believe that a technical book can’t be serious if there’s a bounty hunter in it?

this book is not for you.

[Note from marketing: this book is for anyone with a credit card. Or cash. Cash is nice, too. - Ed]

xxiv   intro

Check out Head First Java for excellent introduction to objectan oriented development, and then come back and join us in iPhoneville.

the intro

We know what you’re thinking. “How can this be a serious iOS development book?” “What’s with all the graphics?” “Can I actually learn it this way?”

And we know what your brain is thinking.

Your bra THIS is imin thinks portant.

Your brain craves novelty. It’s always searching, scanning, waiting for something unusual. It was built that way, and it helps you stay alive. So what does your brain do with all the routine, ordinary, normal things you encounter? Everything it can to stop them from interfering with the brain’s real job—recording things that matter. It doesn’t bother saving the boring things; they never make it past the “this is obviously not important” filter. How does your brain know what’s important? Suppose you’re out for a day hike and a tiger jumps in front of you. What happens inside your head and body? Neurons fire. Emotions crank up. Chemicals surge. And that’s how your brain knows... This must be important! Don’t forget it! But imagine you’re at home, or in a library. It’s a safe, warm, tiger‑free zone. You’re studying. Getting ready for an exam. Or trying to learn some tough technical topic your boss thinks will take a week, 10 days at the most.

in thinks Your bran’t worth THIinS gis. sav

Great. Only 640 more dull, dry, boring pages.

Just one problem. Your brain’s trying to do you a big favor. It’s trying to make sure that this obviously non-important content doesn’t clutter up scarce resources. Resources that are better spent storing the really big things. Like tigers. Like the danger of fire. Like how you should never again snowboard in shorts. And there’s no simple way to tell your brain, “Hey brain, thank you very much, but no matter how dull this book is, and how little I’m registering on the emotional Richter scale right now, I really do want you to keep this stuff around.”

you are here 4   xxv

how to use this book

er as a learner.

t” read We think of a “Head Firs

then make st, you have to get it, Fir ? ng thi me so rn e to lea . Based on the So what does it tak facts into your head ing sh pu t ou ab t no t it. It’s al psychology, sure you don’t forge ogy, and education iol ob ur ne , ce ien sc gnitive on. at turns your brain latest research in co a page. We know wh on t tex n tha re mo learning takes a lot

ciples: First lear ning prin Some of the Head

ne and morable than words alo ages are far more me all and rec in Make it visual. Im ent 89% improvem more effective (up to make learning much e. understandabl o makes things more transfer studies). It als This sucks. aphics gr e th thin or near n’t we just Put the words wi Ca on another than on the bottom or import the they relate to, rather ly to solve l be up to twice as like page, and learners wil list from Sam content. somehow? problems related to the le. zed sty nal and personali Use a conver satio better on post40% to up ed nts perform In recent studies, stude the reader, using a tent spoke directly to learning tests if the con tak ing a formal tone. ional style rather than first-person, conversat language. Don’t take lec turing. Use casual Tell stories instead of more attention to: a . Which would you pay yourself too seriously ture? ty companion, or a lec stimulating dinner par ively words, unless you act re deeply. In other mo ink th to ated, tiv er Get the learn reader has to be mo pens in your head. A hap ch mu ng new thi no ate s, flex your neuron conclusions, and gener solve problems, draw to ed pir estions, ins qu and ing s, thought-provok engaged, curiou llenges, exercises, and cha d nee you t, tha for ses. knowledge. And brain and multiple sen olve both sides of the and activities that inv “I ion. We’ve all had the he reader’s attent erience. exp e” Get—and keep—t on e pag t I can’t stay awake pas t bu s thi rn lea to nt really wa out of the ordinary, ion to things that are Your brain pays attent rning a new, tough, tching, unexpected. Lea -ca eye e, ang str g, interestin r brain will learn much ’t have to be boring. You technical topic doesn t. more quick ly if it’s no lity to remember now know that your abi We s. ion ot em eir Touch th ember onal content. You rem pendent on its emoti not talking ’re something is largely de we , No you feel something. You remember when ut. abo surprise, e car like s you on at oti wh We’re talking em ut a boy and his dog. abo s rie a puzzle, sto ve ng sol chi you ren en heart-w Rule!” that comes wh “I of ling fee the and ...?” , ing that “I’m more curiosity, fun, “what the lize you know someth e thinks is hard, or rea els y od ryb eve ing learn someth sn’t. b from engineering doe technical than thou” Bo

xxvi   intro

the intro

Metacognition: thinking about thinking If you really want to learn, and you want to learn more quickly and more deeply, pay attention to how you pay attention. Think about how you think. Learn how you learn. Most of us did not take courses on metacognition or learning theory when we were growing up. We were expected to learn, but rarely taught to learn.

I wonder how I can trick my brain into remembering this stuff...

But we assume that if you’re holding this book, you really want to learn about iOS development. And you probably don’t want to spend a lot of time. And since you’re going to build more apps in the future, you need to remember what you read. And for that, you’ve got to understand it. To get the most from this book, or any book or learning experience, take responsibility for your brain. Your brain on this content. The trick is to get your brain to see the new material you’re learning as Really Important. Crucial to your well-being. As important as a tiger. Otherwise, you’re in for a constant battle, with your brain doing its best to keep the new content from sticking.

So just how DO you get your brain to think that iOS development is a hungry tiger? There’s the slow, tedious way, or the faster, more effective way. The slow way is about sheer repetition. You obviously know that you are able to learn and remember even the dullest of topics if you keep pounding the same thing into your brain. With enough repetition, your brain says, “This doesn’t feel important to him, but he keeps looking at the same thing over and over and over, so I suppose it must be.” The faster way is to do anything that increases brain activity, especially different types of brain activity. The things on the previous page are a big part of the solution, and they’re all things that have been proven to help your brain work in your favor. For example, studies show that putting words within the pictures they describe (as opposed to somewhere else on the page, like a caption or in the body text) causes your brain to try to makes sense of how the words and picture relate, and this causes more neurons to fire. More neurons firing = more chances for your brain to get that this is something worth paying attention to, and possibly recording. A conversational style helps because people tend to pay more attention when they perceive that they’re in a conversation, since they’re expected to follow along and hold up their end. The amazing thing is, your brain doesn’t necessarily care that the “conversation” is between you and a book! On the other hand, if the writing style is formal and dry, your brain perceives it the same way you experience being lectured to while sitting in a roomful of passive attendees. No need to stay awake. But pictures and conversational style are just the beginning.

you are here 4   xxvii

how to use this book

Here’s what WE did: We used pictures, because your brain is tuned for visuals, not text. As far as your brain’s concerned, a picture really is worth a thousand words. And where text and pictures work together, we embedded the text in the pictures because your brain works more effectively when the text is within the thing the text refers to, as opposed to in a caption or buried in the text somewhere. We used redundancy, saying the same thing in different ways and with different media types, and multiple senses, to increase the chance that the content gets coded into more than one area of your brain. We used concepts and pictures in unexpected ways because your brain is tuned for novelty, and we used pictures and ideas with at least some emotional content, because your brain is tuned to pay attention to the biochemistry of emotions. That which causes you to feel something is more likely to be remembered, even if that feeling is nothing more than a little humor, surprise, or interest. We used a personalized, conversational style, because your brain is tuned to pay more attention when it believes you’re in a conversation than if it thinks you’re passively listening to a presentation. Your brain does this even when you’re reading. We included loads of activities, because your brain is tuned to learn and remember more when you do things than when you read about things. And we made the exercises challengingyet-do-able, because that’s what most people prefer. We used multiple learning styles, because you might prefer step-by-step procedures, while someone else wants to understand the big picture first, and someone else just wants to see an example. But regardless of your own learning preference, everyone benefits from seeing the same content represented in multiple ways. We include content for both sides of your brain, because the more of your brain you engage, the more likely you are to learn and remember, and the longer you can stay focused. Since working one side of the brain often means giving the other side a chance to rest, you can be more productive at learning for a longer period of time. And we included stories and exercises that present more than one point of view, because your brain is tuned to learn more deeply when it’s forced to make evaluations and judgments. We included challenges, with exercises, and asked questions that don’t always have a straight answer, because your brain is tuned to learn and remember when it has to work at something. Think about it—you can’t get your body in shape just by watching people at the gym. But we did our best to make sure that when you’re working hard, it’s on the right things. That you’re not spending one extra dendrite processing a hard-to-understand example, or parsing difficult, jargon-laden, or overly terse text. We used people. In stories, examples, pictures, etc., because, well, you’re a person. And your brain pays more attention to people than it does to things.

xxviii   intro

BULLET POINTS

the intro

Here’s what YOU can do to bend your brain into submission So, we did our part. The rest is up to you. These tips are a starting point; listen to your brain and figure out what works for you and what doesn’t. Try new things.

Cut this out an ick it on your refrigerdatst or. 1

Slow down. The more you understand, the less you have to memorize.

6

Speaking activates a different part of the brain. If you’re trying to understand something, or increase your chance of remembering it later, say it out loud. Better still, try to explain it out loud to someone else. You’ll learn more quickly, and you might uncover ideas you didn’t know were there when you were reading about it.

Don’t just read. Stop and think. When the book asks you a question, don’t just skip to the answer. Imagine that someone really is asking the question. The more deeply you force your brain to think, the better chance you have of learning and remembering. 2

Do the exercises. Write your own notes.

7

We put them in, but if we did them for you, that would be like having someone else do your workouts for you. And don’t just look at the exercises. Use a pencil. There’s plenty of evidence that physical activity while learning can increase the learning. 3

Read the “There Are No Dumb Questions” sections.

Make this the last thing you read before bed. Or at least the last challenging thing.

Part of the learning (especially the transfer to long-term memory) happens after you put the book down. Your brain needs time on its own, to do more processing. If you put in something new during that processing time, some of what you just learned will be lost. 5

Drink water. Lots of it.

Listen to your brain.

Pay attention to whether your brain is getting overloaded. If you find yourself starting to skim the surface or forget what you just read, it’s time for a break. Once you go past a certain point, you won’t learn faster by trying to shove more in, and you might even hurt the process. 8

Feel something!

Your brain needs to know that this matters. Get involved with the stories. Make up your own captions for the photos. Groaning over a bad joke is still better than feeling nothing at all.

That means all of them. They’re not optional sidebars—they’re part of the core content! Don’t skip them. 4

Talk about it. Out loud.

9

Create something!

Apply this to your daily work; use what you’re learning to make decisions on your projects. Just do something to get some experience beyond the exercises and activities in this book. All you need is a pencil and a problem to solve…a problem that might benefit from using the tools and techniques you’re studying for the exam.

Your brain works best in a nice bath of fluid. Dehydration (which can happen before you ever feel thirsty) decreases cognitive function. you are here 4   xxix

how to use this book

Read me This is a learning experience, not a reference book. We deliberately stripped out everything that might get in the way of learning whatever it is we’re working on at that point in the book. And the first time through, you need to begin at the beginning, because the book makes assumptions about what you’ve already seen and learned. We start off by building an app in the very first chapter. Believe it or not, even if you’ve never developed for iOS before, you can jump right in and starting building apps. You’ll also learn your way around the tools used for iOS development. We don’t worry about preparing your app to submit to the App Store until the end of book. In this book, you can get on with the business of learning how to create iOS apps without stressing over the packaging and distribution of your app out of the gate. But we know that’s what everyone who wants to build an iOS app ultimately wants to do, so we cover that process (and all its glorious gotchas) in an Appendix at the end. We focus on what you can build and test on the simulator. The iOS SDK comes with a great (and free!) tool for testing your apps on your computer. The simulator lets you try out your code without having to worry about getting it in the app store or on a real device. But, it also has its limits. There’s some cool iOS stuff you just can’t test on the simulator, like the accelerometer and compass. So, we don’t cover those kinds of things in very much detail in this book since we want to make sure you’re creating and testing apps quickly and easily. The activities are NOT optional. The exercises and activities are not add-ons—they’re part of the core content of the book. Some of them are to help with memory, some are for understanding, and some will help you apply what you’ve learned. Don’t skip the exercises. Even crossword puzzles are important—they’ll help get concepts into your brain so they stay there when you’re coding. But more importantly, they’re good for giving your brain a chance to think about the words and terms you’ve been learning in a different context.

xxx   intro

the intro

The redundancy is intentional and important. One distinct difference in a Head First book is that we want you to really get it. And we want you to finish the book remembering what you’ve learned. Most reference books don’t have retention and recall as a goal, but this book is about learning, so you’ll see some of the same concepts come up more than once. The Brain Power exercises don’t have answers. For some of them, there is no right answer, and for others, part of the learning experience of the Brain Power activities is for you to decide if and when your answers are right. In some of the Brain Power exercises, you will find hints to point you in the right direction.

System requirements To develop for the iPhone and iPad, you need an Intel-based Mac, period. We wrote this book using Snow Leopard and Xcode 4.0. If you’re running Leopard with an older version of Xcode, we tried to point out where there were places that would trip you up. For some of the more advanced capabilities, like the accelerometer and the camera, you’ll need an actual iPhone, iPod Touch, or iPad and to be a registered developer. In Chapter 1, we tell you where to get the SDK and Apple documentation, so don’t worry about that for now.

you are here 4   xxxi

the review team

The technical review team Michael Morrison

Joe Heck

Rich Rosen

Sean Murphy

d

er Eric Sheph

Rich Rosen is one of the co-authors of Mac OS X for Unix Geeks. He also collaborated with Leon Shklar on Web Application Architecture: Principles, Protocols & Practices, a textbook on advanced web application development. He began his career eons ago at Bell Labs, where his work with relational databases, Unix, and the Internet prepared him well for the world of web technology. He lives in New Jersey with his wife, Celia, whose singing provides a sweet counterpoint to the cacophony he produces in his Mac-based home recording studio. Sean Murphy has been a Cocoa aficionado for almost 10 years, contributes to open source projects such as Camino, and works as an independent iOS designer and developer. He lives in Pennsylvania with his best friends—a fiancée, horses, dogs, and cats­—and seriously loves hiking, hockey, and nature. Joe Heck is a software developer, technology manager, author, and instructor who’s been involved with computing for 25 years and developing for the iPhone platform since the first beta release. He’s the founder of the Seattle Xcoders developer group, which supports Macintosh and iPhone development in the Seattle area, and the author of SeattleBus, an iPhone app that provides real-time arrival and departure times of Seattle public transportation (available on the App Store). Eric Shepherd got started programming at age nine and never looked back. He’s been a technical writer, writing developer documentation since 1997, and is currently the developer documentation lead at Mozilla. In his spare time, he writes software for old Apple II computers—because his day job just isn’t geeky enough—and spends time with his daughter. His thorough review means that no one else has to go through the problems he had in actually making the code work. Michael Morrison is a writer, developer, and author of Head First JavaScript, Head First PHP & MySQL, and even a few books that don’t have squiggly arrows, stick figures, and magnets. Michael is the founder of Stalefish Labs (www .stalefishlabs.com), an edutainment company specializing in games, toys, and interactive media, including a few iPhone apps. Michael spends a lot of time wearing helmets, be it for skateboarding, hockey, or iPhone debugging. Since he has iPhone Head First experience, Mike was a great resource to have helping us.

xxxii   intro

the intro

Acknowledgments Our editors: Thanks to Courtney Nash, who has turned into not just our editor, but general all-around O’Reilly handler. She has listened to lots of rants, spent a week on camera with Dan, and still managed to carry us through two updates to the book, one that didn’t get published! She has had her hand in every single one of the over 600 pages in the book and it’s better because of her involvement.

Courtney Nash And to Brett McLaughlin, who kicked off the first edition of this book by responding to an IM that said, “What do you think about Head First iPhone?” and trained us both up in the ways of Head First.

Brett McLaughlin The O’Reilly team: To Karen Shaner, who, as always, kept things running smoothly, which is helpful when we keep changing things. And to Laurie Petrycki, who continues to let us write more Head First books, which apparently is habit forming. Our friends and family: To all the Pilones and Chadwicks, who have always been supportive of our efforts and helped us to become grown ups who can write this stuff. To all our friends at Element 84, who have made this book part of our company and helped us create a place where we enjoy working. And to Paul, who in addition to kicking off this Apple thing by bringing Macs into our house years ago, was employee #1 at Element 84 and a proofreader of this book. To Vinny and Nick, who think that everybody’s parents work together, thank you for putting up with us constantly talking about iOS development. We’re hoping they’re going to be ready to intern this summer. Finally, to Apple, as silly as it sounds, because iOS development has been good to us! We thought the iPhone was great, but the iPad has already changed how we read and interact with the Web. We’re looking forward to being a part of that change.

you are here 4   xxxiii

safari books online

Safari® Books Online Safari® Books Online is an on-demand digital library that lets you easily search over 7,500 technology and creative reference books and videos to find the answers you need quickly. With a subscription, you can read any page and watch any video from our library online. Read books on your cell phone and mobile devices. Access new titles before they are available for print, and get exclusive access to manuscripts in development and post feedback for the authors. Copy and paste code samples, organize your favorites, download chapters, bookmark key sections, create notes, print out pages, and benefit from tons of other time-saving features. O’Reilly Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from O’Reilly and other publishers, sign up for free at http://my.safaribooksonline.com.

xxxiv   intro

1 getting started

Going Mobile with iOS I just don’t see what all the iPhone and iPad fuss is about. This phone works just fine and paper does all I need!

The iPhone changed everything.  The iPhone 4 “changed everything, again.” And now you’ve got the iPad to contend with, too. iOS devices are now capable word processors, e-readers, and video cameras. They are being used in business and medicine as enterprise devices, and the App Store is a platform for every developer to use, from one-man shows to big-name companies. Apple provides the software and we’ll help you with the knowledge—we’re sure you’ve got the enthusiasm covered.

this is a new chapter   1

let’s get started

So, you want to build an iOS app... Maybe you use an iPhone all the time and wish “it could do that.” Maybe you have an app you love that could be so much better. You might have a business that wants to leverage computing power that fits in your customers’ hands. Or perhaps you have an idea for an app that could start a business. There are a lot of motivations to want to code for iPhone and iPad, and lots of customers, too.

e...

er Your App h

2   Chapter 1

going mobile with iOS

...’cause everyone wants one! There’s a big market out there for iPhone and iPad developers. iOS apps are rapidly becoming advertising tools that non-tech businesses want to use, like websites. Enterprises are starting to use iPhones, iPads, and iPod Touches for employees to perform work that they once did with a clipboard or even a laptop. But while there’s a lot of opportunity, there’s also plenty for you to learn, even as an experienced OO developer. Most companies are choosing to outsource iOS development work, making it a great opportunity for freelancers, too.

Just look at the app store At the time this book went to print, there were over 500,000 different apps available for download from the App Store. More than that, the percentage of apps for sale that are games has held steady, while the gross number of apps continues to rise. That means that the number of apps for sale that allow users to perform a task is going up­­; people are integrating mobile computing into their lives for more than just playing.

Let’s start with how an app gets from your head to a device... you are here 4   3

the lifecycle of an app

Apps live in an iTunes universe To get an app approved, sold, distributed, or installed, you need to work with the Apple iOS SDK before getting your app into the iTunes App Store. Here’s a quick picture of the cycle of which you’re going to be a part.

This is where we’ll be working for the rest of the book...

2

1

5

Code and test it in Xcode.

3

Come up with a great idea!

After a sync, it’s live on the device!

4

4   Chapter 1

Users buy and install your app from iTunes.

Use iTunes Connect for the approval process.

going mobile with iOS

Time to make a decision You’re probably brimming with app ideas, but for this first one, we’re going to start simple. Our first application is pretty straightforward: it is going to be a single view with a button that the user can push to make a decision. For now we’ll build this just for iPhone; we’ll get into iPads a bit later in the book. When users start up your application, the first thing they see is a view. It’s essentially the user interface, and it needs to be easy to use and focused on what your application is supposed to do. Throughout this book, whenever we start a new application, we’re going to take a little time to sketch up what we want our views to look like. iDecide

This is the st You can chooseattous bar. but unless you’re hide it, game, you should writing a probably leave it. This is a button. and Press the buttownill the label textl you what change to tel to do.

What should I do?

Let’s call it iDecide.

is The iPhone scpirexenels 320 x 480 earlier, for 3GS and 0 x iPhone 4 is 64 960. each UIs for hem. It G r u o p uild t tch u We’ll ske ion before we b school, but applicatem strangely oldof the best may se aper are some this. pen & p ou can use for tools y

We know what our app should look like, now we just need the tools to build it... you are here 4   5

starting with the SDK

It all starts with the iOS SDK Head over to http://developer.apple.com/ios. You can download the SDK (and other useful Apple development resources) for free with the basic registration— but to distribute a completed app in the App Store or install your app on an iPhone, iPod Touch, or iPad for testing, you’ll need to become a paid Standard or Enterprise Developer. The basic registration provides the SDK with a simulator for testing directly on your Mac, so you can go the free route for now to get started.

Register as a developer at http://developer.apple.com/ios.

Download the latest SDK; this book is based on the 4.3 SDK. Just look for the Download button at the top of the page.

Install the SDK. Once the Installation completes, you can find Xcode.app in /Developer/Applications.

6   Chapter 1

You will onto yourprobably want to d using it a Dock—we’re goingrag it lot. to be

going mobile with iOS

Take a look around Navigate over to the install directory, which is /Developer/Applications by default. If your installation went right, the install directory should have a few key things.

Dashcode is used to write dashboard widgets and webapps, not iOS apps. We won’t be using this. Instruments helps assess memory usage and performance of your app during testing. Quartz Composer can be used to create motion graphics for your app. But that’s for another book.

Xcode is the IDE used to write the Objective-C for your iOS application and just got a major overhaul with Xcode 4. We’re going to get to know it really well. The iPhone Simulator ( installed in /Developer/Platforms/ iPhoneSimulator.platform/Developer/Applications/iPhoneSimulator. App) lets you run iOS in a simulated device on your Mac for simplified app testing while you’re writing code. We’ll be firing this up shortly, too.

Now that you have everything installed, go ahead and double-click on Xcode to get it started. you are here 4   7

jumpstart things with templates

Xcode includes app templates to help you get started When you start Xcode, you’ll get a welcome screen where you can select Create a New Project. Each project is a collection of files (a.k.a. a template) that have some info and code already populated based on the type of project.

This is the very same Xcode that you’d use to develop for the Mac. Since we’re working with the iPhone, make sure iOS Application is selected..

These are the basic App templates. Based on your selection, different code and files are set up for you.

based Select a vnie. wapplicatio

e, If you click on each project ptyp fill hel l wil e the description her you in on some details. As we go through the book, we’ll use different types of projects and discuss why you’d choose one over another for each app. For iDecide, we have just one screen (or view), so start with the View-based Application and give it the Product Name iDecide. Leave the Device Family as iPhone and uncheck the box for Include Unit Tests. In the final dialog box, uncheck the box for Create Local Git Repository.

8   Chapter 1

going mobile with iOS

Xcode is a full-featured IDE Xcode is much more than just a text editor. As you’ve already seen, Xcode includes templates to get you started developing an application. Depending on your application, you may use all of a template or just parts of it, but you’ll almost always start with one of them. Once you get your basic app template in place, you’ll use Xcode for a lot more:

Maintaining your project resources Xcode will create a new directory for your project and sort the various files into subdirectories. You don’t have to stick with the default layout, but if you decide to reorganize, do it from within Xcode. Xcode also has built-in support for version control tools like Git and Subversion and can be used to check out and commit your project changes. Editing your code and resources You’ll use Xcode to edit your application code, and it supports a variety of languages beyond just Objective-C. Xcode also has a number of built-in editors for resource files like plists and xib and nib files (we’ll talk more about these later on). For resources Xcode doesn’t handle natively, double-clicking on one of those files in Xcode will launch the appropriate editor. Some file types Xcode can only view, like pictures, or it will merely list, like sound files. Building and testing your application Xcode comes with all of the compilers necessary to build your code and generate a working application. Once your application is compiled, Xcode can install it on the iOS Simulator or a real device. Xcode includes LLVM and GDB debuggers with both graphical and commandline interfaces to let you debug your application. You can also launch profiling tools like Instruments to check for memory or performance issues. Prepare your application for sale Once you get your application thoroughly tested and you’re ready to sell it, Xcode manages your provisioning profiles and code signing certificates that let you put your application on real devices or upload it to the iTunes App Store for sale. We’ve got more info on this process in the Appendix.

Turn the page to see what Xcode looks like. you are here 4   9

use Xcode for everything

Xcode is the hub of your iOS project When Xcode opens your new View-based project, it is populated with all of the files that you see here, but we’ve changed the view a bit. By expanding the project and selecting a .xib file (which is your view, more on that in a minute), the GUI editor is open on the left. To open the side-by-side assistant view, click the Assistant Editor button on the upper right of the editor.

Here is where you can configure whether to build your app for. the simulator or a real device We’ll stick with the simulator throughout the book.

The iDecide folder has all the Objective-C class files th up your app, as well as th at make files that will make up the .xib e views for your app.

Every class has a .h (header) and .m (implementation) file to go with it.. They work together to create each class.

main Supporting Files include yourwid e ntio lica app function and an and a, dat es, header file, pictur d other data that your app will nee to run. Frameworks shows a list of the libraries you’re using.

You don’t have group your filesto this way, but hi s is the default tfr the template. Fileom groupings are file locations. NOT

cted file with the The Editor Pane shows the sele cted the default view for appropriate editor. We’ve seleGU editor. iDecide, so you can see the I

10   Chapter 1

going mobile with iOS

includes The toolbar se tting options for building and breakpoints, application, running yournd showing searching, a file listings. warnings and

This button changes your view to show the Assista nt Editor.

We’ll be using some of the other tools that came with the SDK (especially the Simulator), but they are all working with the files that are included here.

These buttons are all used to split up the view to show the debugger, versions, etc.

The Assistant Editor automatically selects related e code and displays it here. Th was view controller for this view selected.

The files and frameworks shown were stubbed out based on our selection of a View-based application. As we go forward, we’ll use different types of apps, and that will lead to different defaults. For our app, the template includes iDecideViewController.h and iDecideViewController.m. Every Objective-C class in the template has both a header (.h) and an implementation (.m) file. When the app is compiled, they work together to create a class. We’ll dive into the view controller shortly.

Geek Bits Frameworks are what libraries are called in Objective-C. UIKit, CoreGraphics, and Foundation are loaded by default, but we’ll show you how to add more later.

you are here 4   11

Xcode up close

Xcode Files Up Close If you take a closer look at the files that were generated by Xcode, you’ll see iDecideViewController.h and iDecideViewController.m, as well as iDecideAppDelegate.h and iDecideAppDelegate.m. Those .h and .m pairs work together to create a class. You’ll also see the iDecideViewController.xib file. It creates the first view for the app.

OK, so I have an idea about how Xcode is organized. But what about the views? What are those .xib files anyway, and do I work with them in XCode, too?

Sort of—the GUI editor in XCode handles .xib files. Those .xib files (also called “nibs”) are XML documents that are loaded by the Cocoa Touch framework when the app starts up. We’ll talk a lot more about this in the next chapter, but for now it’s just important to understand that the GUI editor in XCode (often also called Interface Builder) is not creating Objective-C code. It’s creating an XML description of the GUI you’re building, and the Cocoa Touch framework uses that to actually create the buttons and whatnot for your application at runtime. Everything we do in Interface Builder could be done in pure Objective-C code, but as you’ll see, there are some things that are really just easier to lay out with a GUI builder.

12   Chapter 1

UI We’ll be using “I“Gnterface Editor” and mean the Builder” to same thing.

going mobile with iOS

Do this!

To edit the view files in Xcode, you’ll need to open up the .xib file and change some settings in the workspace.

1

Highlight the iDecideViewController.xib file.

2

Close the Assistant Editor by clicking this button.

Only necessary if you opened it earlier to see the editor. We’ll use it more later, but we’re focusing on the GUI for now. 3

Show the library by opening the Utilities Pane, here.

4

Show the Objects Library for the views by clicking on this button.

5

Adjust the size of the library by dragging this bar up.

you are here 4   13

interface building

Build your interface within Xcode When you open up any .xib file in Xcode, the GUI editor will be launched in the main window. With the view tweaking you just finished on the previous page, Xcode will be ready to work with the views. Now it shows an overview of items in your nib, your view, and a library of UI elements (on the right). You can drag and drop any of the basic library elements into your view, edit them, and work with the connections between the code and these elements, using the assistant editor. All of these elements come from the Cocoa Touch framework, a custom UI framework for iOS devices.

File’s Owner

First Responder

Each screen in your , which application is a viewa blank shows up first ass you slate like this. A, you can add UI elements ew will see what your vi y data look like (minus an loaded) that needs to be n build in the app. You caand/or views using code dropping by dragging and e library. controls from th

The library shows all the elements you can choose from to drop into your view. If you scroll around, you’ll see there are a lot of options. This section shows the objects and views that are currently created for that particular nib. File’s Owner and the First Responder exist for every nib, and the others will vary. We’ll talk about both in much greater detail later. 14   Chapter 1

going mobile with iOS

Add the button to your view To add elements, all you need to do is drag and drop the elements you want onto your view. For our app, we just need a button with text that can be changed.

1

2

Drag the rectangular button onto the view. The initial size of the button will be small, so resize it to be a bit bigger. Just grab the corners of the button and pull.

Drag a label onto the button. Edit the new label on the button to say “What should I do?” by double-clicking on the label and typing the new text. Then move the text around to center it on the button.

titles iOS buttons supporexttra label, an without using t app, we’re but for our firs the twogoing to go with ch to keep component approa pler. things a little sim

you are here 4   15

test drive

Test Drive Now, save your work. To build the file, make sure that the scheme selected is the “iPhone 4.x Simulator,” then press the Build and Run button here.

Welcom e to the iPho ne Simulato r!

16   Chapter 1

going mobile with iOS

The iOS simulator lets you test your app on your Mac The Simulator is a great tool for testing your apps quickly and for free. It doesn’t come with all the applications that a real device does, but for the most part, it behaves the same way. When you first start the simulator, you see the springboard—just like on a real iPhone (it’s the initial screen that shows all your app icons)—with iDecide installed (and a default icon that you can change later). Xcode then opens the app and your code is running.

You don’t have to do anything special to launch the Simulator other than build and run you r app.

There are some differences between using the Simulator and your iPhone. For starters, shaking and rotating your Mac won’t accomplish anything, so those don’t work in the Simulator. To approximate rotation and check landscape and portrait views, there are some commands under the Hardware menu. You also have limited gesture support, CPU and memory usage don’t represent reality, and hardware capabilities like tilt sensors (or the accelerometer or gyroscope) don’t exist at all. Even with these issues, testing on the simulator is just so much quicker and easier than a real device that you’ll find you use it for major portions of your development. You can always start with the Simulator and then move to a real device as your application (or hardware needs) mature.



The Simulator has limitations.

Memory, performance, camera, GPS, and other characteristics cannot be reliably tested using the Simulator. We’ll talk more about these later, but memory usage and performance are tough to test on the simulator simply because your Mac has so many more resources than the iPhone or iPad. To test these things, you need to install on an actual device (which means joining one of the paid development programs).

So, you probably want to push this button right now and see what happens, right? Go ahead... you are here 4   17

understanding iDecide OK, so the button exists, but it doesn’t do anything. It needs a little code, right?

UI behavior is implemented in Objective-C. Interface Builder creates your button, but to make that button actually do something, you’ll need to write some code that tells the button how to behave. Controls—which are the UI elements like the button you just added— trigger events when things happen to them, like the button being pressed. For events like button presses, Interface Builder can connect the view controls with code in your controller class for action methods, tagged with IBAction (for Interface Builder Action). We’ll talk more about the Objective-C syntax for actions a bit later, but for now, you’ll need to declare a method in your header (.h) file and implement it in the .m.

iDecide’s logic Any controls you create need a method that Interface Builder can use to connect the control to behaviors specified in the implementation file.

ribes the The .xib file descnf ured it in button as you co . ig Interface Builder

s a method This line declareressed that called buttonP er will recognize Interface Buildllback. -(IBAction) as a possible ca buttonPressed:(id) sender;

iDecideViewController.xib

Button

-(IBAction) buttonPressed:(id) sender

iDecideViewController.h

{

e files’ You’ll likely notice all throller” names have “ViewContthat for in them. Don’t sweat in a bit. now, we’ll explain that

do buttony kinds of things

}

iDecideViewController.m

18   Chapter 1

You provide the metheod implementation in th you .m file. Here’s where code up what should actually happen when the button is pressed.

going mobile with iOS

Changing the button text You know that the button is going to need an IBAction to respond to the button press and that we write up what the button should do in the implementation file. But just what is it that the button should do? We want to change the text in the button to provide an answer. So that means we need some way to reach back “out” to the interface and change the label text. We’ll use an IBOutlet to do that.

The code here provides a place for IB to give us a reference to the label so we can change it. IBOutlet UILabel *decisionText;

-(IBAction) buttonPressed: (id)sender;

The action responds to th interface to kick off th e e logic in the code.

We’ll get into actions and outlets more later. IBActions and IBOutlets are both key for understanding how to work with controls, and we’ll go into a lot more detail on both in the next chapter. For now, just remember that actions are used to react to events in the interface, and outlets are used to reach out from the code to change the interface.

you are here 4   19

no dumb questions

Q:

What happens if I don’ t implement everything in the .h file?

A:

At compile time, Xcode will complain. It’s going to check that you fully implemented the class you declared in the corresponding header file. Since that’s not the case, you’ll start with a warning telling you that happened. If you need that code to support anything else, then it’ll crash at runtime.

Q:

What exactly are frameworks? Are they the same thing as libraries?

A:

They’re really similar. They include shared, compiled code like libraries, but they also can bundle in images, headers, documentation, etc.

Q:

Do I have to use Interface Builder to build my views?

A:

No. You can do everything you can do in Interface Builder in code (and then some). However, using Interface Builder doesn’t mean you can’t do things in code, too. Since Interface Builder gives you a nice, graphical way of laying out your views, most apps have at least some of their UI built in Interface Builder then tweaked or animated in code.

Q:

Do I have to write my application in Objective-C?

A:

Earlier releases of the iOS Developer Agreement required the use of Objective-C for any application that will be distributed

20   Chapter 1

through the iTunes App Store. Apple later relaxed that requirement, opening up the possibility of using other tools or languages. Having said that, nearly everything you’ll find about iOS development will make the assumption you’re using Objective-C. All the underlying frameworks are written in Objective-C (or written to work with it), the documentation and sample code uses it, and the tool suite is built around it. Basically, if you’re serious about writing native applications, you should learn Objective-C and start writing in that.

Q:

Can I give applications I write out to friends?

A:

Yes and no. First, if you want to put an application on anyone’s actual device (including your own), you’ll need to register for the paid Apple iOS Developer program. Once you’ve done that, you can register up to 100 devices and install your application on them. However, that’s not really a great way to get your application out there, since Apple limits how many devices you can register this way. It’s great for testing your application, but not how you want to go about passing it around. A better way is to submit your application to the iTunes App Store. You can choose to distribute your application for free or charge for it, but by distributing it through the iTunes App Store, you make your application available to the world (and maybe make some money, too!). We’ll talk more about distributing apps later in the book. Finally, there’s an Enterprise Developer program you can join that lets you distribute

applications internally without using the App Store. This works, but is more expensive than the normal program.

Q:

Do I have to use an IDE? I’m really a command-line kinda developer.

A:

Technically speaking, no, you don’t have to use the Xcode IDE for straight development. However, the IDE makes iOS development so much easier that you really should ask yourself if you have a good reason for avoiding it, especially since testing on an actual device or the simulator is so tightly coupled to Xcode. This book uses the Xcode IDE as well as other Apple development tools, and we encourage you to at least try them out before you abandon them. For things like automated builds or automated testing, the SDK comes with a command-line build tool called xcodebuild that can build your application just like Xcode does, but you’ll most likely still want to do your actual development in Xcode.

Q:

Can I develop an app for the iPhone and then rebuild it for other phones like Windows Mobile or Android phones?

A:

In a word, no. When you develop for iPhone, you use Apple’s iOS frameworks, like Cocoa Touch, as well as Objective-C. Neither of these are available on other devices.

going mobile with iOS

Below is the code for when the button gets tapped. Add the bolded code to the iDecideViewController.h and iDecideViewController.m files. We are creating three things: the UILabel property, the IBAction to respond to the button press, and the IBOutlet to change the label when the button is pressed.

#import

@interface iDecideViewController :

UILabel *decisionText_;



}

UIViewController {

label We need to change thane sw er, r text to provide ou t to tle so we need an IBOu e label be able to get to thamework control that the fr . will build from our nib

@property (retain, nonatomic) IBOutlet UILabel *decisionText; -(IBAction)buttonPressed:(id)sender;

@end

We’ll talk more about properties later in the book.

ll be called Here’s the action thateswised. when the button is pr #import “iDecideViewController.h”

@implementation iDecideViewController

@synthesize decisionText=decisionText_; -(IBAction)buttonPressed:(id)sender { }

The @synthesize tells the compiler to create a getter and setter for the property we declared in the header file. We’ll get into that more in Chapter 3.

ion of This is the implementtsatca lled the method that ge essed. when the button is pr

decisionText_.text = @”Go for it!”;

- (void)dealloc {

[decisionText_ release];

}

iDecideViewController.h

[super dealloc];

We’ll use our reference eto the label to change th text.

ere you clean The dealloc method ise. wh We’ll talk more up your memory usag r 3, too. about this in Chapte

iDecideViewController.m

you are here 4   21

sharpen solution

Here’s the code from before in the context of the full files for iDecideViewController.h and iDecideViewController.m.

#import

@interface iDecideViewController : UIViewController {

UILabel *decisionText_;



}

@property (retain, nonatomic) IBOutlet UILabel *decisionText; -(IBAction)buttonPressed:(id) sender; @end

iDecideViewController.h #import “iDecideViewController.h”

@implementation iDecideViewController

@synthesize decisionText=decisionText_;

-(IBAction)buttonPressed:(id) sender {

[email protected]”Go for it!”;

of what you’ll This code is typical . There’s a see in a header file new IBOutlet declaration of the property for and IBAction and a our UILabel.

The IBOutlet is a refere nce to the label that we’ll chan ge the text on the button, and the IBAction specifies what will actually happen when the button is pressed. This is implementation code. Here, we’re defining the method that is called when the button is pressed. We use a consta nt string to change the text in the label. Remember, decisionText is a reference to th e UILabel we created in Interface Builder.

}

- (void)dealloc {

iDecideViewController.m

[decisionText_ release];

}

[super dealloc];

22   Chapter 1

mory management. The release call is for me nce counting for Objective-C uses refere l talk more about memory management (we’l to be released to this in a bit) and needs free up the memory.

going mobile with iOS

You’re using the Model View Controller pattern Model View Controller (MVC) is a pattern that is discussed at length in Head First Design Patterns. You see it used a lot with GUI applications, and it’s all over the Cocoa Touch framework. The short version is that as much as possible, you want to separate out your logic from your view and data. It’s especially key when you want to write one set of logic to support both iPhone and iPad UIs, which we’ll do later in the book.

View the A component represenurts user will GUI element that yo button. interact with, like a sembled Generally, it will be aseditor, with the Xcode GUI code, too. but it can be built in

Component

Model

The Mode data your lapcontains the which for iO p needs, datasource tS apps is a with the dat hat works plists, images, abases, information or general will need. that your app

Datasource

Controller Delegate

that ntains the logicion— co e at eg el d he T w of informat controls the floController. It saves and hence it’s our ation and orchestrates displays inform en when. iDecide follows which view is seour delegate is the this pattern—ontroller, which explains iDecideViewC the file names you’ve quite a few of seen so far.

Does our implementation of iDecide differ from MVC? If so, how?

you are here 4   23

iOS development secrets

iDecide is actually a little simpler For iDecide, we don’t have a datasource we’re dealing with—there’s nothing to store since we’re just changing the text label and can specify that in one line in iDecideViewController.m. So iDecide is simplified to more of a View - View Controller pattern. You can think of it like the MVC pattern without an M—we don’t need a model here.

iDecideViewController

Our View Controller the view we built andisitsassociated with handle events that come job is to view and make sure it’s shout of the right information at the owing the right time.

in a nib Our view is described ilt with bu we (.xib file) that mplate te e Th . er Interface Build this framework we used loaspdslays it. nib at startup and di

This pattern is the secret to iOS development If you only have room in your brain for one thing from this chapter, this is what should fill that space: the Model-View-Controller pattern and the View-ViewController pattern are really just specific examples of a general Delegation pattern used everywhere in iOS development. Something delegates responsibility out to another class that’s responsible for actually performing an action as a result. In this case, the view needs to delegate out to the ViewController (through UI events) and let the ViewController know that something happened. The ViewController (or delegate) then picks up the responsibility of reacting to that event and doing whatever the app has to do next. Sometimes setting up delegates is an explicit thing where you actually tell an object what its delegate is. Sometimes it’s a little more indirect and you do it by linking controls up to methods, like Interface Builder does.

24   Chapter 1

going mobile with iOS

Test Drive You’ve got your IBAction and IBOutlets set up, so build and run the code again. Try clicking on the button in the simulator and see if it works.

Nothing happens!

Why didn’t the button change? Who’s not doing their job?

you are here 4   25

hooking up

What happened? The Objective-C that we wrote is all set to handle things when the button is pressed, but the view hasn’t been set up to connect the button to that code. We need to use the GUI editor to hook up our button to the buttonPressed method we just wrote. Then when the .xib file is loaded by the framework, it will connect the button object it creates with our code.

This is the part we’re missin between the instantiated bug—the link that needs to get called. tton and the code

m from page 18?

Remember this diagra

-(IBAction) buttonPressed:(id) sender;

iDecideViewController.xib

Button

-(IBAction) buttonPressed:(id) sender {

do buttony things

}

iDecideViewController.m

Unless the UI components are hooked up to the code, nothing is going to happen. We need to connect the button’s “Hey, I just got pressed” event to our buttonPressed action method. That will get our method called when the user taps on the button. We then need to get a reference to the UILabel that the framework is going to create for us when the nib is loaded—that’s where the IBOutlet comes in. Let’s start with the outlet so we can change the UILabel text when the button is pressed. 26   Chapter 1

iDecideViewController.h

going mobile with iOS

Use the GUI editor to connect UI controls to code Highlight iDecideViewController.xib to bring up the GUI editor, and open the Assistant Editor so you can see iDecideViewController.h alongside. Now let’s hook up the button to our new code.

This property will be highlighted.

If you don’t have a two-button mouse, just hold CTRL and then click. 1

 ight-click on the label you dropped on the R button. This will bring up a list of events and references.

2

Click on the circle next to New Referencing Outlet and drag it to the @property statement for the Outlet in the .h file on the right. Now when the decisionText UILabel is generated, our decisionText property will reference the control, through the IBOutlet.

OK—I get how we can now change the label, but how does the GUI know that you pressed a button?

you are here 4   27

handy little lists

A component can trigger certain events We need to attach the right component event to our code. Remember that action method you wrote earlier that you can connect the button to?

essages All IBActionqmuired take one re he sender of - (IBAction) buttonPressed:(id)sender; argument: t This is the This is the name of the method the message. triggered IB = Interface tha t wil l get called. The name can be anything, element that Builder but the me tho d the action. must have one argument of type (id).

Now we need to pick the event that should trigger this method. If you right-click on the button in the editor, you’ll see a list of events it could dispatch. We want the TouchUpInside event.

l of the This list showsheal button events that t get into can send. We’ll events later the different in the book.

Most of these sound like what events they are.

We’ll be using the Touch Up Inside event.

Components dispatch events when things happen to them Whenever something happens to a component—for instance, a button gets tapped—the component dispatches one or more events. What we need to do is tell the button to notify us when that event gets raised. We’ll be using the TouchUpInside event. If you think about how you click a button on an iPhone or iPad, the actual click inside the button isn’t what matters: it’s when you remove your finger (or “touch up”) that the actual tap occurs. Connecting an event to a method is just like connecting an element to an outlet, which you just did on the previous page.

28   Chapter 1

going mobile with iOS

Connect your events to methods Just like with outlets, you drag the connection from the button event to the - (IBAction) code in iDecideViewController.h and select the action that should be called.

1

2

 ight-click on the button R you dragged onto the view. This will bring up a list of events and references, like it did with the label.

Then click on the circle next to Touch Up Inside and drag it to the IBAction in the .h file. Now when the button gets pressed, our buttonPressed method will be called.

Test Drive

Now that everything is hooked up, it’s ready to run. Make sure you save everything and then build and run.

you are here 4   29

test drive

Test Drive Get a message here!

Click here!

30   Chapter 1

It works!

going mobile with iOS

You’ve built your first iPhone app! All the pieces are fitting together:

The nibs (*.xib) describe the interface. iDecide is made up of two nibs: the MainWindow.xib and our iDecideViewController.xib. Together, these describe the UI the user sees. The views are connected to the code in the View Controller. Our views are connected to the implementation code through Interface Builder using IBOutlets and IBActions. IBOutlets give us references to UI components, and IBActions are called when events happen. Our application behavior is implemented in our View Controller. Following the MVC pattern (or really, just the VC pattern), we have all of our behavior implemented in our View Controller, cleanly separated from the view itself. The View Controller uses IBOutlets to get back to our actual UI controls if it needs to update them.

ƒƒ Interface Builder creates nib files (with a .xib extension) that describe the GUI in XML. ƒƒ Nib files are loaded by the Cocoa Touch framework and are turned into real instances of Cocoa Touch classes at runtime. ƒƒ In order to connect the components described in a nib to your code, you use IBOutlets and IBActions.

ƒƒ Xcode is where your code and files are maintained for your application. ƒƒ Xcode is the hub for your project development and offers support for editing your code, building your application, and debugging it once it’s running. ƒƒ The iPhone Simulator lets you test your application on your Mac without needing a real device.

you are here 4   31

no dumb questions

Q: A:

What is that File’s Owner thing?

Interface Builder has an expectation of what class will be the nib’s File’s Owner. You can change what class Interface Builder thinks it will be, but by default, a new project is set up so that the main View Controller created by Xcode is the File’s Owner for the main view created by Xcode. That’s why we didn’t have to change anything. Since the File’s Owner is set up to be our iDecideViewController, Interface Builder looks at the iDecideViewController header and sees that we have an IBOutlet named descriptionText and an IBAction named buttonPressed. When you connected the UILabel’s referencing outlet to File’s Owner descriptionText, Interface Builder saved the information necessary so that when the nib is loaded by the application, the references are set correctly in our iDecideViewController. The same thing happened with the TouchUpInside event, except in this case, instead of hooking up a component to a reference, it hooked up a component’s event to a method that should be called. Beware—Interface Builder’s expectation of the class that will load the nib does not mean that other classes can’t try—it just might not work well if that class doesn’t have the necessary properties and methods.

strings. Since it’s so common, Objective-C has built-in support for creating them from constants. You indicate a string constant should be an NSString by putting an @ symbol in front of it. Otherwise, it’s just a normal char* like in C or C++.

Q:

How much of what we’ve covered is iPhone-specific? Is developing for the iPod Touch or iPad very different?

A:

Not at all! One of the benefits of developing for iOS is that most things built for one iOS device can be used directly on another iOS device. You’ll need to be careful about device-specific hardware capabilities (for example, trying to use a camera on an older iPod Touch or iPad won’t work), and the screen resolution of your views and images will need to change, but otherwise, developing for different devices is largely hidden from you. If you keep the MVC pattern in mind and make sure to separate logic from views, it’s much easier to add new views for other devices to your app.

Q:

How does Xcode actually build an application?

A:

Why does our new text string have an @ in front of it?

We’re going to get into this more in the coming pages. The short version is that Xcode can gather up all your resources and code, link them together, and then spit out a nice package at the end that drops into the sandbox available on the iOS device.

The Cocoa Touch framework uses a string class named NSString for its text

Why do I keep hearing about Interface Builder? What is that really?

Q:

A:

32   Chapter 1

Q:

A:

Before, Xcode 4 views were edited in a separate application called Interface Builder. Now it’s a part of XCode and isn’t really called Interface Builder any more. Since this transition is still new, folks may still be working in and talking about IB.

Q:

When the views are compiled, why do they stay .xib files? Shouldn’t something change?

A: Q:

After compilation, the .xib files are actually .nib files in their binary form.

What exactly are these “undocumented APIs” I keep hearing about?

A:

These are private methods that Apple gets to use, but you don’t. An example is multitasking. It just became available for developer use with iOS 4, but some of Apple’s applications have been able to run in the background from the beginning (for example, the iPod application doesn’t stop playing when you switch to another app).

You typically see developers touching undocumented, private methods when trying to customize a standard iOS control or change what a physical button does. Don’t do this. Apple will catch it during the approval process and they will reject your application for it. Protecting these APIs is part of Apple protecting the platform, and what is undocumented now may be allowed in the future.

going mobile with iOS

Match each iOS development item to its description.

Item

Description

IBOutlet

The Model View Controller pattern where the view delegates behavior to a controller.

Functions of Xcode

Xcode, Instruments, Interface Builder, and the iOS Simulator.

MVC

Reference from the code to an object in the nib.

IBAction

Images, databases, the icon file, etc.

Components of the SDK

Maintaining and editing code and resources, debugging code, and preparing an app for deployment.

Application resources

Indicates a method that can be called in response to an event.

you are here 4   33

who does what solution

SOlUTion

Match each iPhone development item to its description.

Item

Description

IBOutlet

The Model View Controller pattern where the view delegates behavior to a controller.

Functions of Xcode

Xcode, Instruments, Interface Builder, and the iPhone Simulator.

MVC

Reference from the code to an object in the nib.

IBAction

Images, databases, the icon file, etc.

Components of the SDK

Maintaining and editing code and resources, debugging code, and preparing an app for deployment.

Application resources

Indicates a method that can be called in response to an event.

34   Chapter 1

going mobile with iOS

Pool Puzzle

If we put t learned earlioergether what we app works wit about how the about the codh the diagram button press, e for the of which files you get an idea do what.

Your job is to take filenames from the pool and place them into the blank lines pointing to the diagram. You may not use the same filename more than once, and you won’t need to use all the names. Your goal is to make a complete diagram that will show the design of iDecide.

Control

Datasource

Hint: We rea needed a datllay haven’t source yet...

Delegate

Note: each thing from the pool can only be used once!

iDecideViewDataSource.m iDecideViewController.m iDecideViewController.xib IBAction

MainWindow.xib

iDecideViewController.h button

you are here 4   35

pool puzzle solution

Pool Puzzle Solution Your job is to take filenames from the pool and place them into the blank lines pointing to the diagram. You may not use the same filename more than once, and you won’t need to use all the names. Your goal is to make a complete diagram that will show the design of iDecide.

iDecideViewController.xib MainWindow.xib Control

There isn’t one for this app. We’ll have one line of text, that’s in iDecideViewController.m, but no external datasources.

The view the user sees is made up of MainWindow.xib wi th the iDecideViewControlle r.xib embedded in it.

Datasource

iDecideViewController.m Delegate

iDecideViewController.h

Note: each thing from the pool can only be used once!

iDecideViewDataSource.m

IBAction

36   Chapter 1

button

going mobile with iOS

iOS cross Bend your brain around some of the new Untitled Puzzle terminology we used in this chapter.

Header Info 1 Header Info 2 etc... 1

2 3 4

5

6

7

8

9 10 11 12

13

Across

Down

4. Something that the simulator cannot reliably test. 5. This is used to set up an outgoing connection from the implementation code to the view. 7. The term to describe each screen of an iPhone app. 8. The framework used to write iPhone apps. 10. The folder used to organize the images for the app. 12. The name of the IDE for iPhone apps. 13. These are used in Xcode to provide classes to be accessed.

1. The language used to write iPhone apps. 2. This is used on a desktop to test an app. 3. This is used to recieve an event in code and trigger something. 6. This is the name of the editor used for Objective-C. 9. The iPhone is this kind of device. 11. The name of a file used to create a view.

you are here 4   37

iOS cross solution

iOS cross Solution

Untitled Puzzle

Bend your brain around some of the new terminology we used in this chapter.

Header Info 1 Header Info 2 etc... 1

O

2 3

I 4

B

P

E

R

F

O

I

B

O

U

T

J

L

T

N

C

E

E C

E

T

T

A 6

I O

I U 5

C

C

B

R M A

A

8

S

X

C

O

A

T

O

U

N

C

T H

N

12

I

V

9

O 10

O 11

I 7

R

M

E

S

D N

T

E

O

I

E S

W

E U

R

C

E

L

D

E

R

W O

R

K

B R

F

A

C

E

B

B

U

I L

13

F

R

A M E

S

Across

Down

4. Something that the simulator cannot reliably test. [PERFORMANCE] 5. This is used to set up an outgoing connection from the implementation code to the view. [IBOUTLET] 7. The term to describe each screen of an iPhone app. [VIEW] 8. The framework used to write iPhone apps. [COCOATOUCH] 10. The folder used to organize the images for the app. [RESOURCES] 12. The name of the IDE for iPhone apps. [INTERFACEBUILDER] 13. These are used in Xcode to provide classes to be accessed. [FRAMEWORKS]

1. The language used to write iPhone apps. [OBJECTIVEC] 2. This is used on a desktop to test an app. [SIMULATOR] 3. This is used to recieve an event in code and trigger something. [IBACTION] 6. This is the name of the editor used for Objective-C. [XCODE] 9. The iPhone is this kind of device. [MOBILE] 11. The name of a file used to create a view. [NIB]

38   Chapter 1

going mobile with iOS

Your iOS Toolbox CHAPTER 1

You’ve got Chapter 1 under your belt and now you’ve added basic iOS app interactions to your tool box.

Views are constructed in Interface Builder. A view is made up of nib (*.xib) files and the GUIs are edited with Interface Builder.

Then, you write the code that makes the views work... This code is almost always written in Objective-C using Xcode, and includes IBActions and IBOutlets.

...and connect component events to the code. Back in Interface Builder, you connect your actions and outlets to the components you included in your view.

The Simulator runs your app virtually. As you build your app, you can run your code in the simulator and test it to see if it works.

you are here 4   39

2 iOS app patterns

Hello, Renee! mailto: grandmom body: please bring me some soda. I’m so over the milk. Sent from my iPhone

Apps have a lot of moving parts. OK, actually, they don’t have any real moving parts, but they do have lots of UI controls. A typical iOS app has more going on than just a button, and it’s time to build one. Working with some of the more complicated controls means you’ll need to pay more attention than ever to how you design your app. In this chapter, you’ll learn about some of the fundamental design patterns used in the iOS SDK, and how to put together a bigger application.

this is a new chapter   41

mike needs your help

Author’s note: not take Head First doesy for Mike’s any responsibilitoblems. relationship pr You have this friend Mike. He has a great girlfriend, Renee, but they’ve been having some problems. She thinks that he doesn’t talk about his feelings enough.

I could use my iPhone to send her emails about my feelings, but it’s so much work. Can you speed that process up?

Mike

42   Chapter 2

There’s (about to be) an app for that. Using some solid design and the basic controls included in the Interface Builder library, you can have Mike sending off emails in no time. The app can handle filling out some basics, setting it up to go straight to Renee, and Mike can just fill in some blanks. But first, what should his messages say?

iOS app patterns

First, we need to figure out what Mike (really) wants Mike isn’t a complex guy. He wants an easy interface to create his emails and he really doesn’t want to have to type much.

Here’s what Mike handed you at the end of the night.

t Here’s wha

I want:

h typing - Not muc unication m m o c t n a - Inst use __ - Easy to is: I’m ___ h t e k li s il - My ema _____ about it.” and feeling

App Magnets

Now that we know what Mike wants, what do we need to do? Take the magnets below and put them in order of the steps you’ll follow to build his email app.

Build the GUI

Determine app layout Figure out how to use the controls

Handle the data

The controls are the standard use. iOS UI elements we’re going to

the email Generate

Data here means Mike’s feelings and actions. you are here 4   43

start with the app layout

App Magnets Solution

Now that we know what Mike wants, what do we need to do? Take the magnets below and put them in order of the steps you’ll follow to build his email app. Determine app layout

ed on the After you’ve laesnd ign, you need to general app d oc umentation a get into the d e out how to little and figurcontrols you’ve implement the chosen.

Build the GUI

Figure out how to use the controls

Here we need to m anage the data coming fr om the controls.

Q: A:

Handle the data

How do you figure out the app layout?

Are we always going to start with a sketch?

Yes! Good software design starts with knowing what you’re building and how the user is going to work with the app. Expect to spend about 50 percent of your development time on the UI—it’s what separates the great apps from the rest.

Q:

How do we talk to the email messaging framework?

44   Chapter 2

Some people writ first—we’re goinge tobackend code forth depending on go back and but to get started, our project, GUI first this time. we’ll do the

We’ll help you out w ith this last step.

the email Generate

We’re going to give you a couple to choose from to get started, but in general, it’s important to think about what your app needs to do and focus on those features first.

Q: A:

Before you start coding anything, sketch up what you’re thinking.

A: Q: A:

Don’t worry, we’ll give you some code to help you to work with that. Why isn’t Mike texting Renee? Why send an email?

Simply put, because that won’t work in the simulator. Once you have a good handle on creating the email, porting that to texting isn’t hard.

Q: A:

Does every control work differently than the others?

For the most part, no—once you learn a few basic patterns, you’ll be able to find your way through most of the SDK. Some of the controls have a few peculiarities here and there, but for the most part, they should start to look familiar.

iOS app patterns

App Layout Construction Here are two designs to evaluate. Based on aesthetics, usability, and standard iPhone app behavior, which one is better for Mike?

Option #1

Renee’s email here Pre-populated text, so just insert a couple of words Text field for user name Text field for email password

Option #2

InstaEmail I’m and feeling Send

Cancel

Labels that will be part of the email text

InstaEmail and feeling I’m hello awesome worlding

about it. Send Button

r Spinning controlle filled in with activities and feelings

The button will have email addresses and passwords

preconfigured

Which UI is better? Why? (Be specific.)

Why not the other?

you are here 4   45

keep it simple

App Layout Construction Here are two designs to evaluate. Based on aesthetics, usability, and standard iPhone app behavior, which one is better for Mike?

Bad

Option #1

InstaEmail

Lots of typin This isn’t alwaygs in here. but we can do bebad, tter. here for More typinrgobably won’t stuff he p er the first change aft time... ere. ...and again h

Which app is better?

I’m and feeling Send

Cancel

need to type Your user doesnd’tress again. Since in the email ad same, we can it’s always thethis for him. take care of would be The send buttonbottom of better at the stuck between the page, not his. controls like t Cancel wha never have t“ ? iPhone apps almost If the user Quit”-type buttons. hits the homchanges his mind, he is moved to e button and the app the backgrou nd.

#2.

Option #2 has a lot less typing and fewer fields overall. Since the user doesn’t need to change his username or password often, there’s no reason to put it on the main view every time he runs the app.

Why? (Be specific.)

Why not the other?

46   Chapter 2

Option #1 has a lot of typing and settings to remember. The buttons are confusing.

iOS app patterns

! d o o G App flows from top tcoleanly bottom. is shown as e t x e t n o m m v Co e doesn’t hrasor ik M l— e b la a ove the cu to try to m around it.

Option #2

InstaEmail and feeling I’m hello awesome worlding

about it. Send Button

ton that Smart send butem ailing, not keeps the user sswords or remembering pa URLs.

Q:

Do I really need to care about usability and aesthetics so much?

A:

Usability and aesthetics are what made the iPhone a success, and Apple will defend them to the death. Even more important, you don’t get to put anything on the App Store or on anyone else’s iPhone without their approval. Apple has sold billions of apps—if yours doesn’t fit with the iPhone look and feel or is hard to use, people will find someone else’s app and never look back.

Q:

We got rid of the username, password, and email fields. The email one I understand, but what about the other two?

type in Instead of having Mike feelings, what he’s doing and his ker to select we can give him a picwe r options from. This means fe ermi ned, but since they’re predet We’d want it’s way easier to use. with him, but to double-check this with what this seems consistent Mike’s after.

This is the one you’re going to build for Mike.

A:

Anytime your app needs configuration information that the user doesn’t need to change frequently, you should keep it out of the main task flow. Apple even provides a special place for these called a Settings bundle that fits in with the standard iPhone settings. We’re not going to use that in this chapter (we’ll just hard code the values), but later we’ll show you how to put stuff in the Settings page. That’s usually the right place for things like login details.

Q:

How am I supposed to know what Apple thinks is good design or aesthetically pleasing?

A:

Funny you should ask...go ahead, turn the page.

you are here 4   47

what’s your (app) type?

App design rules—the iOS HIG The iOS Human Interface Guide (HIG) is a document that Apple distributes for guidance in developing iOS Apps for sale on the App Store. You can download it at http://developer.apple.com/ios. This isn’t just something nice they did to help you out; when you submit an app for approval, you agree that your app will conform to the HIG. We can’t overstate this: you have to follow the HIG, as Apple’s review process is thorough and they will reject your application if it doesn’t conform. Complain, blog with righteous anger, and then conform. Now let’s move on. Apple also distributes a few other guides and tutorials, including the iPhone Application Programming Guide. This is another great source of information and explains how you should handle different devices, like the iPhone, devices with old versions of iOS, and the iPod Touch. Not paying attention to the iPod Touch is another great way to get your app rejected from the App Store.

hors do nost t u a e h t e il Note: Wh esting these methodApp suggest t rejected from the rity of being can say with autho Store, wey work. that the

Application types The HIG details three main types of applications that are commonly developed for the iPhone. Each type has a different purpose and therefore offers a different kind of user experience. Figuring out what type of application you’re building before you start working on the GUI helps get you started on the road to good interface design. Immersive Apps

Games are a classic example, but like this simulated level, immersive apps use a very custom interface that allows the user to interact with the device. As a result, HIG guidelines aren’t as crucial in this case. Productivity Apps

n e informatio Help manaleg te tasks. Info and comp hical, and you is hierarc by drilling navigate more levels of down into detail.

48   Chapter 2

Utility Apps

info ic set of ttle if c e p s a li Get r with as to the usioe n or settings interact ation as possible. configur

e more Usually havdesign than a interface ity app, and are productiv to stay very expected with the HIG. consistent

iOS app patterns

Below are a bunch of different application ideas. For each one, think about what kind of app it really is and match it to the app types on the right.

App Description

Type of App

InstaEmail 1.0: Allows you to email with minimal typing.

News Reader: Gives you a list of the news categories and you can get the details on stories you choose.

Marble Game: A marble rolling game that uses the accelerometer to drive the controls.

Stopwatch Tool: Gives you a stopwatch that starts and stops by touching the screen

Immersive Application

Utility Application

Productivity Application

Recipe Manager: A meal listing that allows you to drill down and look at individual recipes.

you are here 4   49

who does what solution

SOlUTion Match each app description to its application type.

App Description

Type of App

InstaEmail1.0: Allows you to create an email with minimal typing.

Since this has a list-dArpp News Reader: Gives you a list of drill-down iven, the news categories and you can get the details on stories you interface, Productivitity ’s choose. .

The accelerometer controls a big rolling, marble. We want a very focused stopwatch GUI, no real data to work through.

Marble Game: A marble rolling game that uses the accelerometer to drive the controls.

Stopwatch Tool: Gives you a stopwatch that starts and stops by touching the screen

Recipe Manager: A meal listing that allows you to drill down and look at individual recipes.



Since we have one screen and no typing, InstaEmail is more of a Utility App.

Immersive Application

Utility Application

Productivity Application

Lots of data to work through here: tabl a drill-down to recipes—definitely producties, vity.

We’re going to get into more HIG elements later.

This is just the beginning of our relationship with the HIG. As we start designing apps, we’re going to be using platform-specific technologies (like cameras) and working with some physical analogs for the iPad.

50   Chapter 2

iOS app patterns

HIG guidelines for pickers and buttons The HIG has a section on the proper use of all the standard controls, including the two that we’ve selected for InstaEmail—a picker and a button. Before you build the view with your controls, it’s a good idea to take a quick look at the recommendations from Apple. You’ll find this information in the HIG, under iOS UI Element Usage Guidelines.

P ic k e r

t, If you have units (feelay , they seconds, etc.) to dispthe need to be fixed to selection bar here.

InstaEmail and feeling I’m hello awesome worlding

about it. Send Button

We just finished with this...

Let’s get started building this...

splays a few The picker only dien at a time, so items on the scre ur user isn’t remember that yo see all the going to be able to options at once.

The picker’s overall although you can hidesize is fixed, it be part of the vi it or have do in InstaEmail). ew (like we

Bu t t o n

p layout Determine ap

he GUI Build t

The rounded rectangle button is pretty straightforward, but keep in mind it should always perform some kind of action. Figure out how to use the widgets

Handle the data

the email Generate

Now let’s move on to building the GUI.

you are here 4   51

a new view

Create a new View-based project for InstaEmail Once you’ve started Xcode, select File→New→New Project. Just like iDecide, for InstaEmail we have one screen and we’re not going to be animating it (like the utility app) or starting with a particular navigation for the app, so again choose the View-based Application targeted for iPhone and name it InstaEmail.

Do this! To write an app that can send an email, we’ll need to add a new framework. With the project and its targets highlighted, select Build Phases, expand the Link Binary With Libraries section, and push the + button. Then select MessageUI Framework from the list and click Add.

This is the project highlighted.

Here are the highlighted targets.

Just to keep things organized, drag the new Framework into the Frameworks folder.



The new project type in Xcode is not necessarily the same as your app type. For example, a Productivity App can be written as a View-based Application, a Window-based Application, Navigation-based Application, or a Tab Bar Application.

52   Chapter 2

We’ll be working with these other project ok. types later in the bo

iOS app patterns

Alright, wait a minute. I still don’t really get this “a .xib file is a nib is a view” thing. I get that I can edit this view in Interface Builder, but how does it all fit in with everything else?

Fair enough. It can get a little confusing. Let’s take a look at how a nib really becomes a view...

you are here 4   53

where your view comes from

The life of a root view In Chapter 1, we touched on how the Xcode GUI editor creates XML descriptions of your view, called a nib, and that the Cocoa Touch framework turns that into a real view in your application. Let’s take a closer look at what’s going on under the hood. Like in most other languages, main(...) gets called first. When your application is launched by the user, the iPhone provides a quick animation of your app zooming into the screen (this is actually a PNG file you can include with your app), and then calls your main method. Main is provided by the templates and you almost never need to touch it.

1 Main method

2

Main Window

Main kicks off a Cocoa Touch Application. The standard main(...) kicks off a Cocoa Touch UIApplicationMain, which uses the information in your application’s Info.plist file to figure out what nib to load. With the View template we used, it’s a nib called MainWindow.xib.

InstaEmailViewController 3

MainWindow.xib contains the connections for our application. If you look in MainWindow.xib, you’ll see it has an instance of our InstaEmailAppDelegate for its UIApplicationDelegate and an instance of our InstaEmailViewController. When the Cocoa Touch framework loads MainWindow. xib, it will create an instance of our InstaEmailViewController and tell it to load our InstaEmailViewController.xib.

Remember how critical? We’ll taldelegates were them in a minute. k more about 54   Chapter 2

This is the View Controlle It subclasses r. UIViewControlle r.

e’s ric proxy Feilnib e n e g e h t d th e use ctions. When t the nib, w When we builoutlet and action connoebject there to receivreoller. Owner for loaded, there’s a real e InstaEmailViewCont is actually ections. For us, it’s th those conn

iOS app patterns

4

The Cocoa Touch framework creates our custom view from the InstaEmailViewController.xib. When we constructed the nib, we used the File’s Owner proxy object to stand in for the object that owns the nib contents. At this point, the framework is loading the nib on behalf of our InstaEmailViewController class, so that instance is used for connections. As the framework creates instances of our components, they’re connected up to the instance of InstaEmailViewController.

This is our view. is launched by theIt framework, on of our ViewContthe behalf roller.

The nib file cont instances of obje ains serialized configured them cts as we control objects . They are usually labels, but can belike buttons or anything that ca be serialized. n

5

When events occur with components, methods are invoked on our controller instance. The actions we associated between the controls and the File’s Owner in the nib were translated into connections between the controls and our controller instance (InstaEmailViewController). Now when a control fires off an event (like the Send Email button), the framework calls a method on our InstaEmailViewController instance.

you are here 4   55

no dumb questions

Q:

Isn’t good design vs. bad design a little subjective?

A:

Yes and no. Obviously, different people will have differing opinions about what UI looks better. However, Apple has very specific guidelines about how certain controls should be used and best practices that should be followed. In general, if you’re using a common iOS control, make sure you’re using it in a way that’s consistent with existing applications.

Q:

Can I run apps that I build on my own device?

A:

To get an app you write installed on your iPhone, you’ll need to sign up for either the Standard or Enterprise Developer programs at http://developer.apple.com/ios/. Everything in this book is designed to work with just the Simulator, so don’t feel like you need to go do that just yet. We’ll talk more about putting apps on an actual phone later in the book.

Q:

The InstaEmail icon looks horrible. What can I do?

56   Chapter 2

A:

The icon for an application is just a PNG file in your project. We’ll add and configure icons later, but for now, just know that you’ll need a .png file in the resources directory for that purpose—we’ll hook you up with some cool icons in a bit.

nib. Xcode is able to serialize non-control classes, too. That’s how it saves out our InstaEmailViewController in MainWindow.xib. When the nib is restored from disk, objects in the nib are reinstantiated and populated with the values you gave them in Interface Builder.

Q:

You can think of it like “freeze-drying” the objects and then restoring them at runtime.

A:

So does the editor save out the File’s Owner, too?

Do I have to use the GUI builder for the view?

No. Everything you do in the Xcode editor can be done in code. Interface Builder makes it a lot easier to get things started, but sometimes you’ll need that code-level control of a view to do what you want. We’ll be switching back and forth depending on the project and view.

Q:

I’m still a little fuzzy on this nib thing. Do they hold our UI or regular objects?

A:

They can hold both. When you assemble a view using the Interface Builder GUI editor, it keeps track of the controls you’re using and the links to other classes. These controls are serialized into an XML document; when you save it out, this is your

Q:

A:

No, File’s Owner is a proxy. File’s Owner represents whatever class is asking to have this nib loaded. So the File’s Owner proxy isn’t actually stored in the nib, but Interface Builder needs that proxy so you can make association with controls you used in your view. When the nib is restored (and the control objects are instantiated), the nib loading code will make the connections to the real owning object that asked to load the nib.

iOS app patterns

w

It’s time to build the view. Double-click on InstaEmailViewController.xib in the InstaEmail folder, and launch the Interface Builder GUI editor inside of Xcode. Using drag and drop, pull over the elements from the Utilities panel that you need to build the View. 1

is If you don’t see th ur library, check yo here. buttons here and

 ind each of the elements (we’ve given them the F proper names for you) in the objects library and drag and drop them into the View window.

Labels

Picker

Round Rect Button, titled “Send Email”

Edit label text here. InstaEmail and feeling I’m hello awesome worlding

about it. Send Email

2

Select the top label to start setting the text. The title can be edited in the top of the utilities panel.

3

 dit the labels and button text for the E title, “I’m”, “and feeling”, and “about it”, as well as the title for the button. Don’t worry about the picker values (like “hello worlding”) just yet. Once you save it, your view should look like this... you are here 4   57

preview your view

wv

The View is all built and ready to go. Here’s what you should have on your screen now. Once you tweak everything to look just how you want it, we’ll run InstaEmail in the Simulator.

not be this Your labels maylt, the label big. By defau to the font, will not resize space. To but to fit the, just resize make it largers at the using the dot label field. edges of the

Filling in the picker data requires some code, and we’ll get to that in a minute. What you see here are default values.

Did you notice the blue guidelines in the simulato r? They’re in the view when you’re laying out elements to help you center things and keep them lined up with each other.

58   Chapter 2

iOS app patterns

Test Drive Now it’s time to check out InstaEmail in the Simulator. Save in Interface Builder, go back into Xcode, and hit Run from the Build menu (or use the keyboard shortcut ⌘+R).

Close, but the picker isn’t showing up?!?!

Why do you think the picker isn’t showing up?

you are here 4   59

digging up some data

We need data For the picker to actually show up, we have to give it data to display. And Mike is just the guy to help. He likes what you have put together for the UI, so now we need a little more information from him to give to the picker.

I like the interface. Here’s my list of what I do and how I feel about it so you can fill in the rest. Can’t wait until it’s done because I’m soooo over talking about it...

Things I do: sleeping eating working thinking crying begging leaving shopping

60   Chapter 2

Things I feel: awesome sad happy ambivalent nauseous psyched confused hopeful anxious

iOS app patterns

Use pickers when you want controlled input In our case, the picker is the perfect element for our app. No typing at all, but it allows Mike to have control over what gets selected. There’s some terminology that you need to know about pickers before we get our data in there.

We want two columns. The picker calls columns components. ment a large ealend is r e k ic p A n width) (full screaell size cannot be the over. changed

The number of rows, or items, comes from Mike’s list, so 9 for each component. Remember the screen apps? The longest wordsize issue when building iPhone going to be abbreviated. needs to fit in a column or it’s There’s not a lot of spac work with. e to

OK, so we can just set the picker rows with the values Mike gave us like we did with the button label, right?

The picker is different. The picker doesn’t want to be told what to do, it’s going to ask when it wants your input. You’re going to see this pattern show up with controls that could use a lot of data like pickers, and later we’ll see something similar with table views. Let’s take a closer look...

you are here 4   61

datasources and delegates

Pickers get their data from a datasource... Many of the elements in the Cocoa Touch framework have the concept of datasources along with delegates. Each UI control is responsible for how things look on the screen (the cool spinning dial look, the animation when the user spins a wheel, etc.), but it doesn’t know anything about the data it needs to show or what to do when something is selected. The datasource provides the bridge between the control and the data it needs to display. The control will ask the datasource for what it needs and the datasource is responsible for providing the information in a format the control expects. In our case, the datasource provides the number of components (or columns) for the picker and the total number of rows for the picker. Different controls need different kinds of datasources. For the picker, we need a UIPickerViewDatasource.

Hey—the user just spun me to row 3. Picker control

Delegate

So how many rows and components do I need?

What’s the word for row 3? Datasource

...and tell their delegates when something happens As we saw in Chapter 1, a delegate is responsible for the behavior of an element. When someone selects something—or in this case, scrolls the picker to a value—the control tells the delegate what happened and the delegate figures out what to do in response. Just like with datasources, different controls need different kinds of delegates. For the picker, we need a UIPickerViewDelegate.

When in doubt, check out Apple’s documentation You’re probably wondering how to use that picker, so don’t hesitate to check out the API documentation. In Xcode, go to the Help menu and then the Xcode Help option.

Search for “UIPickerView” and it will pull up all the information on the class that you need to implement for the picker. 62   Chapter 2

iOS app patterns

Q: A:

Why is the delegate providing the content? That really seems like data.

That’s something particular to a picker and it has to do with the fact that the picker delegate can change how the data is shown. In the simplest form, it can just return strings to the picker. If it wants to get fancy, it can return an entire View (yes, just like the View you built with Interface Builder, but smaller) to use images or special fonts, for example.

That pattern is back Control

You’ve seen this Control-Datasource-Delegate pattern before, when we briefly discussed MVC in Chapter 1. Nearly all the complex controls use it. You’ll probably remember that even the View-View Controller relationship we’ve been using follows this pattern (minus the datasource). Datasource

Delegate

Controls have their own specific datasources and delegates Each control has specific needs for its datasource and delegate, and we’ll talk about how that’s handled in Objective-C in a minute. However, it’s important to realize that while the responsibilities are split between the datasource and the delegate in the pattern, they don’t necessarily have to be implemented in different classes. The control wants a delegate and a datasource—it doesn’t care whether they’re provided by the same object or not: it’s going to ask the datasource for datasource-related things and the delegate for delegate-related things. Let’s take a closer look at how the UIPicker uses its datasource and delegate to get an idea of how all of this fits together.

you are here 4   63

picking apart the picker

The Picker Exposed This week’s interview:

How to avoid spinning out of control... Head First: Hello, Picker, thanks for joining us. Picker: My pleasure. I don’t usually get to talk to anyone but my datasource and delegate, so this is a real treat. Head First: I’m glad you brought those up. So we’ve worked with controls like buttons and labels, but they just had properties. What’s going on with this delegate and datasource business? Picker: Well, to be clear, I have properties too— there just isn’t too much exciting going on there. What makes me different is that I could be working with a lot of data. I might only have one row or I might have a hundred; it just depends on the application. Head First: Ah, OK. A label only has one string in it, so there can be a property that holds just that string. No problem. Picker: Exactly! So, instead of trying to cram all of the data into me directly, it’s cleaner to just let me ask for what I need when I need it. Head First: But you need to ask for it in a specific way, right? Picker: That’s the beauty of my setup. I ask for what I need to know in a specific way—that’s why there’s a UIPickerDatasource—but I don’t care where my datasource gets its information. For example, I need to know how many rows I need to show, so I ask my datasource. It could be using an array, a database, a plist, whatever—I don’t care. For a picker, I’m not that picky...all I need to know is how many rows. Head First: That’s really nice—so you could be showing data coming from just about anything, and as long as your datasource knows how to answer

64   Chapter 2

your questions, you don’t care how it stores the data internally. Picker: You got it. Now the delegate is a little different. I can draw the wheels and all that, but I don’t know what each application wants to do when someone selects a row, so I just pass the buck to my delegate. Head First: So the delegate is implemented so that when you tell the delegate what happened, it performs the right action. Like saving some value or setting a clock or whatever. Picker: Exactly, so by using the delegate, you don’t have to subclass an object to customize behavior. Now, I have to confess I have one little oddity going on... Head First: Oh, I was waiting for this. This is where you ask the delegate for the value to show in a row, right? Picker: Yeah—other controls ask their datasource. I could come up with a lot of excuses, but...well, we all have our little quirks, right? Head First: I appreciate your honesty. It’s not all bad, though; your delegate can do some neat things with each row, can’t it? Picker: Oh yeah! When I ask the delegate for a particular row, it can give me back a full view instead of just a string. Sometimes they have icons in them or pictures—really, anything you can cram in a view, I can display. Head First: That’s great. Well, we’re out of time, but thanks again for stopping by. Picker: My pleasure! Now I’m off to take my new datasource for a spin.

iOS app patterns

Match each picker characteristic to where it belongs—the delegate or the datasource. You’ll need to go digging in the API to figure out where the three methods go.

Picker characteristic (or method)

Delegate or datasource?

Directions for drawing the view for the items The number of components

Delegate

pickerView:numberOfRowsInComponent

pickerView:titleForRow:forComponent

Datasource

The row values (strings or views)

numberOfComponentsInPickerView

you are here 4   65

picker parts

SOlUTion Match each picker characteristic to where it belongs—the delegate or the data source. You’ll need to go digging in the API to figure out where the three methods go.

Picker characteristic (or method)

Delegate or datasource?

Directions for drawing the view for the items

The number of components

Delegate

pickerView:numberOfRowsInComponent

A required part of the UIPickerViewDataSource Protocol that returns the number of rows.

pickerView:titleForRow:forComponent

Delegate protocol; Part of the UIPickerVie ew try in the picker. returns a title for on en

Datasource

The row values (strings or views)

numberOfComponentsInPickerView

A required part of the UIPickerViewDataSource Protocol; returns the number of components.

66   Chapter 2

Working together, the delegate and the datasource provide what is needed to render the picker.

iOS app patterns Hang on—there are protocols in both the datasource and the delegate?

Protocols define what messages the datasource and delegates need to respond to. Pickers (and other controls that use delegates and datasources) have specific messages to which their supporting classes need to respond. We’ll get into them in more detail in the next chapter, but for now what you need to know is that messages are defined in protocols. Protocols are Objective-C’s idea of a pure interface. When your class can speak a particular protocol, you’re said to conform to it.

Hey—the user just spun me to “row 3.”

So how many rows and components do I need?

Protocols tell you what methods (messages) you need to implement

Delegate

e as class we uusr picker r e v e t a h W te for o the delegoanform to the tocol. has to c ViewDelegate pro UIPicker

What’s the value for row 3?

e e to bst v a h t n’ ju Datasource e for ese dothere are need s u h e t w s : s r la ; c ls we form to membe jects whatever Likewise, source needs to cocne protocol. Reifferent obent protoco d differ t. our dataickerViewDatasour two orry abou IP U e th to w

Protocols typically have some required methods to implement and others that are optional. For example, the UIPickerViewDatasource protocol has a required method named pickerView:numberOfRowsInComponent; it has to be in the datasource for the picker to work. However, UIPickerViewDelegate protocol has an optional method named pickerView:titleForRow:forComponent, so it doesn’t need to be in the delegate unless you want it. So how do you know what protocols you need to worry about? The documentation for an element will tell you what protocols it needs to talk to. For example, our UIPickerView needs a datasource that speaks the UIPickerViewDataSource protocol and a delegate that speaks the UIPickerViewDelegate protocol. Click on the protocol name and you’ll see the documentation for which messages are optional and which are required for a protocol. We’ll talk more about how to implement these in the next chapter; for now, we’ll provide you the code to get started. you are here 4   67

sometimes it’s okay to conform

First, declare that the controller conforms to both protocols Now that you know what you need to make the picker work, namely a delegate and a datasource, let’s get back into Xcode and create them. Under Classes, you have two files that need to be edited: InstaEmailViewController.h and InstaEmailViewController.m. Both files were created when you started the project. The .h and .m files work together, with the header file (.h) declaring the class’s interface, variable declarations, outlets, and actions, etc.; the implementation file (.m) holds the actual implementation code. We need to update the header file to state that our InstaEmailViewController conforms to both the UIPickerViewDataSource and the UIPickerViewDelegate protocols.

d add Go ahead aned. what’s bold

#import

@interface InstaEmailViewController : UIViewController

{ NSArray* activities_;



Here’s where we say outhr e class will conform to urce UIPickerViewDataSo ate and UPickerViewDeleg protocols.

}

NSArray* feelings_;

@end

We’re going to set up two arrays for Mike: one for activities and one for feelings. We’ve underscored the name here to keep the! code easier to understand. It’s not required

InstaEmailViewController.h

Next, add Mike’s activities and feelings to the implementation file Now we’re into InstaEmailViewController.m file, the actual implementation. We’ll need to add some methods to implement the required methods from the protocols, but we’ll get back to that in a second. First, let’s add the list from Mike. We’re going to use the two arrays we declared in the header to store the words that Mike gave us.

tation code All implemen @implementation. goes after indicate Here, we’ll realizing the that we’re ViewController InstaEmail we defined in the interface header. 68   Chapter 2

#import “InstaEmailViewController.h”

@implementation InstaEmailViewController

The break here skips commented default code that we’re not usin out g.

InstaEmailViewController.m

iOS app patterns

en add the s that were here and pothssible methods Remove the /* markes me with lots of code. Xcode templat apcop, we’re using viewDidLoad. stubbed out. For this

This method gets called on your view controller after the view is loaded the .xib file. This is where you can from initialization and setup for the do some view.

// Implement viewDidLoad to do additional setup after ... - (void)viewDidLoad {

[super viewDidLoad];

activities_ = [[NSArray alloc] initWithObjects:@”sleeping”, @”eating”, @”working”, @”thinking”, @”crying”, @”begging”, @”leaving”, @”shopping”, @”hello worlding”, nil]; feelings_ = [[NSArray alloc] initWithObjects:@”awesome”, @”sad”, @”happy”, @”ambivalent”, @”nauseous”, @”psyched”, @”confused”, @”hopeful”, @”anxious”, nil];

}

The “@” before those strings tel make them NSStrings instead of ls the compiler to are real Objective-C objects, as char*. NSStrings C-style character pointer. Most opposed to a simple NSStrings instead of char*’s. Objective-C classes use

- (void)dealloc {

[activities_ release]; [feelings_ release];

}

[super dealloc];

Here, we establish the arrays with Mike’s lists. We’ll call them in a bit to fill in the picker.

InstaEmailViewController.m

You need to release all these objects to free up the memory they were using. We’ll talk about memory management a lot more in Chapter 3.

InstaEmailViewController.m

Now we just need the protocols... you are here 4   69

how many rows?

The datasource protocol has t wo required methods Let’s focus on the datasource protocol methods first. We said in the header file that InstaEmailViewController conforms to the UIPickerViewDatasource protocol. That protocol has two required methods: numberOfComponentsInPickerView:pickerView and pickerV iew:numberOfRowsInComponent. Since we know we want two wheels (or components) in our view, we can start by putting that method in our implementation file.

#pragma mark -

#pragma mark Picker Datasource

Here are the two required methods for the picker.

in go anywhereter n a c e d o c is Th the file. Af the body oafd works great! viewDidLo

The #pragma notation is for Xcode; it helps break up the code, but doesn’t provide any Protocol logic.

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *) pickerView {



}

How many components?

return 2;

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent :(NSInteger)component { How many rows in each

if (component == 0) {



}



else {



}

}

return [activities_ count];

component? They come from different arrays, so we need to treat them separately.

return [feelings_ count];

Our second method needs to return the number of rows for each component. The component argument will tell us which component the picker is asking about, with the first component (the activities) as component 0. The number of rows in each component is the just the number of items in the appropriate array.

InstaEmailViewController.m

Now that we have the methods implemented, let’s wire everything up to the picker. 70   Chapter 2

iOS app patterns

Connect the datasource just like actions and outlets Now that the datasource protocol is implemented, the data is in place and it’s just a matter of linking it to the picker. Hop back into Interface Builder to make that connection:

1

To see this view of the items in the editor, click this arrow button here.

2

 ight-click on the R Picker in the view to bring up the picker connections box.

 otice that the File’s Owner for this view is our InstaEmailViewController, which N realizes the datasource and delegate protocols we need. You need to connect the picker’s dataSource to our controller, or the File’s Owner. To do that, click inside the circle next to the dataSource, and drag over the to File’s Owner.

you are here 4   71

getting to a specific row

There’s just one method for the delegate protocol The UIPickerViewDelegate protocol only has one required method (well, technically there are two optional methods, and you have to implement one of them). We’re going to use pickerView:titleForRow: forComponent. This method has to return an NSString with the title for the given row in the given component. Again, both of these values are indexed from 0, so we can use the component value to figure out which array to use, and then use the row value as an index.

#pragma mark -

#pragma mark Picker Delegate Protocol

The signature these messages focor right out of the mes UIPickerViewDel egate and UIPickerViewDat documentation. J aSource and paste it if youst cut u want.

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

if (component == 0) {



}



else {



}

}

Our choice of two methods, one of which needs to be implemented.

return [activities_ objectAtIndex:row];

return [feelings_ objectAtIndex:row];

return nil;

Return the string in the array at the appropriate location—row 0 is the first string, row 1 second, etc.

InstaEmailViewController.m

Now back to the view to wire up the delegate... 72   Chapter 2

iOS app patterns

1

 ight-click on the picker R again and bring up the connections window.

2

 he File’s Owner T realizes the delegate protocol as well. Click inside the circle next to the delegate and drag over the to File’s Owner.

Test Drive Save your work in and Run (⌘+R) the app. When the Simulator pops up, you should see everything working!

Spin those dials—they all the things on Mike’s’re list and they work great!

you are here 4   73

no dumb questions

Q:

What happens if I don’t implement a required method in a protocol?

A:

Your project will compile, but you’ll get a warning. If you try to run your application, it will almost certainly crash with an “unrecognized selector” exception when a component tries to send your class the missing required message.

Q:

What if I don’t implement an optional method in a protocol?

A:

That’s fine. But whatever functionality that it would provide isn’t going to be there. You do need to be a little careful in that sometimes Apple marks a couple of methods

as optional but actually, you have to implement at least one of them. That’s the case with the UIPickerViewDelegate. If you don’t implement at least one of the methods specified in the docs, your app will crash with an error when you try to run it.

Q:

Are there limits to the number of protocols a class can realize?

A:

Nope. Now, the more you realize, the more code you’re going to need to put in that class, so there’s a point where you really need to split things off into different classes to keep the code manageable. But technically speaking, you can conform to as many as you want.

ƒƒ The picker needs a delegate and a datasource to work. ƒƒ In a picker, each dial is a component.

74   Chapter 2

Q:

I’m still a little fuzzy, what’s the difference between the interface we put in a header file and a protocol?

A:

An interface in a header file is how Objective-C declares the properties, fields, and messages a class responds to. It’s like a header file in C++ or the method declarations in a Java file. However, you have to provide implementation for everything in your class’s interface. A protocol, on the other hand, is just a list of messages—there is no implementation. It’s the class that realizes the protocol that has to provide implementation. These are equivalent to interfaces in Java and pure virtual methods in C++.

ƒƒ In a picker, each item is a row. ƒƒ Protocols define the messages your class must realize—some of them might be optional.

iOS app patterns

OK, that’s great and all. It looks really nice. But the “Send Email” button doesn’t do anything yet...

Now let’s get that button sending emails... We got the picker working, but if you try out the “Send Email” button, nothing happens when something’s selected. We still need to get the button responding to Mike and then get the whole thing to generate and send an email.

Think about what we need to do to get the button working. What files will we use? What will the button actually do?

you are here 4   75

no dumb questions

So we just need to go back to the view and wire up the TouchUpInside event for the “Send Email” button again, right? Just like in iDecide?

Yes! We still need to write the actual delegate functionality the button is going to trigger, but that’s the idea.

?

Q: A:

What is an event again?

UI controls trigger events when things happen to them. You can “wire” these events to methods so that your method is called when an event is triggered. Most of the time, you can wire events to methods using Interface Builder, but you can also do it in code (we’ll do this later in the book).

Q:

Why didn’t we have to wire up the picker methods? All we did there was set up a delegate and datasource.

76   Chapter 2

A:

Great question! Some controls have very fine-grained events that they trigger. Those individual events can be wired to specific methods on other objects (like File’s Owner). Other controls want higher-level concepts, like a Datasource or a Delegate. In order to be a Datasource or a Delegate for those controls, you have to provide a set of methods that the control can call. Those methods are defined in a protocol. In the case of the UIPickerView, it doesn’t have lots of fine-grained events—instead, it has two key protocols and just wants to know which object(s) will implement them. You can’t choose to have half the Datasource methods go to one object and the other half to go somewhere else. You simply give it a datasource and it will call all of the datasource methods on that object as it needs to.

iOS app patterns

Actions, outlets, and events Back in iDecide, we used actions and outlets with a button press. Let’s go back for a second to revisit what happened:

- (IBAction) buttonPressed

Press the button! iDecideViewController class

- (IBOutlet) “Change the

text”

Here’s the action we created for the button press in Chapter 1:

- (IBAction) buttonPressed:(id)sender;

IB = Interface Builder

This is the name of the method called. The name can be anything,that will get must have one argument of type but the method (id).

essages All IBActiongmument: the take one ar he message. sender of t ement that This is the elhe action. triggered t

you are here 4   77

IBActions and IBOutlets

Tonight’s talk: IBActions speak louder than...a lot of things

IBAction:

IBOutlet:

Hi, Outlet. What’s it like to only be an enabler? What are you talking about? I do stuff. Uh—I’m an Action, all about doing. My job is to do something when something else happens—an event. That’s getting something done. You just sit there and point to stuff going on. Big deal. At least I’m aware of everything going on. Yeah, but when the user does something, I make it happen! I do the saving, I do the email sending! Listen, it’s true that I’m just an instance variable that works with an object in a nib, but that doesn’t mean I’m not important. Really, because the compiler just ignores you! It does, but I tell Interface Builder in Xcode a lot. You’re not very tight with IB, are you? Well, for starters, the “IB” in IBAction stands for Interface Builder! Big deal, I have “IB” in my name, too. Well, we do have that in common. Anyway, Interface Builder knows when I’m around so that some event in a nib can set me off and keep me informed. Thanks. That’s nice of you to admit.

78   Chapter 2

Well, I guess that is pretty important.

iOS app patterns

IBAction:

IBOutlet: But I’m secure in my relationship with Interface Builder. Without me, the code couldn’t change anything in the UI.

Care to explain? Sure. An IBOutlet variable can point to a specific object in the nib (like a text field or something), and code (yes, probably your code) can use me to change the UI, set a text field’s content, change colors, etc. Oh—I see. You know, there is one thing that you have that I’ve always wanted. What’s that? You can be anything! Stick IBOutlet in front of a UI component’s variable name and you’re good. I have more complicated syntax, because I need to have the idea of a sender in there. I do like the freedom! Glad we could work things out. Me too.

w to cre

Change both the header and implementation files for the InstaEmailViewController to include the IBaction info we need.

1

Start with the header and add an IBAction named sendButtonTapped.

2

Then provide an implementation for that method in our .m file, and write a message to the log so you know it worked before creating the email and sending it off.

you are here 4   79

exercise solution

wv

Declare your IBAction in the header file and provide the implementation in the .m file.

1

#import

@interface InstaEmailViewController : UIViewController {

NSArray* activities_; NSArray* feelings_;



}

The IBAction is what allows the code to respond to a user event, remember...

- (IBAction) sendButtonTapped: (id) sender; @end

Declare your IBAction here so we can use it in the .m file and Interface Builder knows we have an action available.

2

#pragma mark -

#pragma mark Actions

Add this bit of code after the picker code.

Same method declaration as the .h

- (IBAction) sendButtonTapped: (id) sender {



}

InstaEmailViewController.h

NSLog(@”Email button tapped!”);

This will give you the output on the console.

InstaEmailViewController.m

80   Chapter 2

iOS app patterns

Connect the event to the action Now that you have an action, you need to wire up the event to the action. Open up InstaEmailViewController.xib in Xcode with the Utilities pane, right-click on the button, and drag the circle for Touch Up Inside to File’s Owner. Link it with your new sendButtonTapped method.

Do this!

Test Drive

Save, then click Build and Run. You should get the “Email button tapped!” message in the console in Xcode. To see it, you need to enable the debugging pane and select All Output.

you are here 4   81

test drive

Test Drive

Save, then click Build and Run. You should get the “Email button tapped!” message in the console in Xcode. To see it, you need to enable the debugging pane and select All Output.

n here Choose this butto ebugging to open up the tdtom of pane at the bodow. the Xcode win

Select All Output here.

82   Chapter 2

iOS app patterns

So now we need to get the data from that picker for the email, right? Would an IBOutlet be the right thing for that?

Yes! An IBOutlet provides a reference to the picker. In Chapter 1, we used an outlet to access and change the text field value on the button. Now, to gather up the actual message to create the email, we need to extract the values chosen from the picker, then create a string including the label text. So far, the picker has been calling us when it needed information; this time, when Mike hits the “Send Email” button, we need to get data out of the picker. We’ll use an IBOutlet to do that.

you are here 4   83

getting the data from the picker

Add the IBOutlet and property to your view controller We need to declare an IBOutlet property and a class attribute to back it. We’ll talk more about properties in the next chapter, but in short, that will get us proper memory management and let the Cocoa Touch framework set our emailPicker property when our nib loads.

Start with the header file by adding the code in bold below:

#import

@interface InstatwitViewController : UIViewController {

UIPickerView *emailPicker_;



NSArray* feelings_;

}

NSArray* activities_;

Here, we declare a field in . the class called emailPicker_ The type is a pointer to a UIPickerView.

@property (nonatomic, retain) IBOutlet UIPickerView *emailPicker; - (IBAction) sendButtonTapped: (id) sender; @end

Here’s our outlet declaration. This lets Interface Builder know you have something to connect to. IBOutlet identifiers are actually #defined to nothing; they’re just there for Interface Builder. The pointer exists, though.

84   Chapter 2

The property for emailPicker has some memory management options that we’ll explain more in Chapter 3.

InstaEmailViewController.h

iOS app patterns

Next, synthesize the property... Once we set up the declarations for the property and instance variable, we need to link them up in the implementation file. We do that with the @ synthesize directive.

#import “InstaEmailViewController.h”

@implementation InstaEmailViewController @synthesize emailPicker=emailPicker_;

...and clean up after yourself

along with the @ @ synthesize goraestion in the .h file. property declato come in Chapter 3 More on this as well...

InstaEmailViewController.m

When our view controller is destroyed, its dealloc method will be called. We need to make sure we release any memory we’ve allocated. We’ll talk a lot more about memory management in Chapter 3, but for now, make sure you release each of your class attributes.

- (void)dealloc {



[emailPicker_ release];



[feelings_ release];



}

[activities_ release];

[super dealloc];

The last thing you need to do wit is release our reference to it—anoh emailPicker memory thing. We’ll come back to ther management in Chapter 3, we pro the memory mise.

@end

InstaEmailViewController.m

What’s next? you are here 4   85

connect the outlet to the code

Connect the picker to our outlet You’re probably expecting this by now! Back into the view to make the connection from the UIPickerView to the IBOutlet in your view controller. Right-click on the UIPickerView, grab the circle next to the “New Referencing Outlet,” and drop it on File’s Owner—our InstaEmailViewController sporting its new emailPicker outlet.

When you click and drag up etoto File’s Owner, you will be abler connect it to the emailPick outlet you just created.

What do you need to do now to get the data out of the picker and into your email message? Think about the “Send Email” button action and how that will need to change...

86   Chapter 2

iOS app patterns

Use your picker reference to pull the selected values Now all that’s left is to use our reference to the picker to get the actual values Mike selects. We need to change the sendButtonTapped method to pull the values from the picker. Looking at the UIPickerView documentation, the method we need is selectedRowInComponent:. That method returns a row index we can use as an index into our arrays.

need our callback. Wethe r fo n io at t en em Here’s the implring and fill in the values fromreplaced to create a st @” in the string format get picker. The “% we pass in. with the values

To figure out we need to askwthat Mike chose on the picker, selected for eachhe picker which row is corresponding st component, and get the ring from our ar rays.

- (IBAction) sendButtonTapped: (id) sender {

NSString* theMessage = [NSString stringWithFormat:@”I’m %@ and feeling %@ about it.”, [activities_ objectAtIndex:[emailPicker_ selectedRowInComponent:0]], [feelings_ objectAtIndex:[emailPicker_ selectedRowInComponent:1]]]; NSLog(@”%@”,theMessage); }

NSLog(@”Email button tapped!”);

one We’ll pull this log message out and put in the be. will age mess il above to see what the final ema

, ll email text intit fu he t h it w e ng InstaEmailViewController.m a new stri hFormat method to crea it We want to build W ng u ri yo st ns ’s io ng t ri so we’ll use NSSritng. There are lots of othererops, integers, st act ed at a templ format, like char lected ng ri st a h it w rt e inse the two se to could us ed ne st ju e w w etc., but for no use %@. ll e’ w so For now we’re just going to log this message to the console so strings, we can see the string we’re building, and then we’ll generate the email to send out in just a minute. Let’s make sure we implemented this correctly first before emailing Renee.

you are here 4   87

test drive

Test Drive

OK, try it out. You should get some convincing text in the console.

what il code, this is a em e h t d d a Once we show up in the email. will actually

88   Chapter 2

iOS app patterns

Ready Bake Code

To send the email, we’re going to use the messaging API. Rather than go into all the details of the messaging framework right now, we’ll give you the code you need. As you get further in the book, this type of framework will become easy to use. Add the bolded code you see below into the appropriate files, and you’ll be ready to go.

#import

#import

@interface InstaEmailViewController : UIViewController {

UIPickerView *emailPicker_;

#pragma mark -

InstaEmailViewController.h

#pragma mark Actions

- (IBAction) sendButtonTapped: (id) sender {

NSString *theMessage = [NSString stringWithFormat:@”I’m %@ and feeling %@ about it.”,

[activities_ objectAtIndex:[emailPicker_ selectedRowInComponent:0]], [feelings_ objectAtIndex:[emailPicker_ selectedRowInComponent:1]]];



NSLog(@”%@”, theMessage);



if ([MFMailComposeViewController canSendMail]) {



MFMailComposeViewController* mailController = [[MFMailComposeViewController alloc] init];

mailController.mailComposeDelegate = self;



[mailController setMessageBody:theMessage isHTML:NO];



}

else {



}

}

[mailController setSubject:@”Hello Renee!”];

[self presentModalViewController:mailController animated:YES]; [mailController release];

NSLog(@”%@”, @”Sorry, you need to setup mail first!”);

InstaEmailViewController.m

you are here 4   89

ready bake code

Ready Bake Code Cont.

#pragma mark -

#pragma mark Mail composer delegate method - (void)mailComposeController:(MFMailComposeViewController*) controller didFinishWithResult:(MFMailComposeResult)result

{



}

error:(NSError*)error;

[self dismissModalViewControllerAnimated:YES];

InstaEmailViewController.m

we talked aboutor and a n he w r be em em R een the simulatcompose an differences betw simulator can real device? T’the actually send it to a mail email but won can see everything work, server. So youually email Renee. but can’t act

90   Chapter 2

After adding that code, you can just save, build, and go. It will now show up as a preformatted email!

iOS app patterns

Test Drive

That is great! Now, Renee is happy and feels included and I don’t actually have to talk out loud about my feelings. At all. Ever.

you are here 4   91

iOS cross

iOS cross Untitled Puzzle

Header Info 1 Header Info 2 etc...

Flex your vocab skills with this crossword.

1 3

2 4

5 6

7

8 9

10

11

Across

Down

3. This typically handles the information itself in the app. 6. This is the document Apple uses to evaluate apps for the App Store. 7. You see this listed in the view and it controls the view. 9. This component allows for controlled input from several selections. 10. This type of app is typically one screen, and gives you the basics with minimal interaction. 11. These define to which messages the datasource and delegate respond.

1. This typically contains the logic that controls the flow of information in an app. 2. The best way to figure out what protocols you need to worry about is to check the ____________. 4. This app type typically involves hierarchical data. 5. This app type is mostly custom controllers and graphics. 8. The other name for an *.xib file.

92   Chapter 2

iOS app patterns

We’ve listed a couple of descriptions of a some different apps. Using the app description, sketch out a rough view and answer the questions about each one. 1

Generic giant button app There are several of these currently up for sale on the App Store. This app consists of pushing a big button and getting some noise out of your iPhone. What type of app is this?

What are the main concerns in the HIG about this app type?

2

Book inventory app This app’s mission is to keep a list of the books in your library, along with a quick blurb of what each is about and the author. What type of app is this?

What are the main concerns in the HIG about this app type?

you are here 4   93

iOS cross solution

iOS cross Solution Untitled Puzzle

Header Info 1 Header Info 2 etc...

Flex your vocab skills with this crossword.

1 3

5 6

H

D

A

T

A

S

O

U

R

C

I

U M A

N

I

N

T

E

R

F

A

C

E

M 7

F

I

L

E

S

O W N

E

R

P

S 10

U

T

I

L

I

T

Y

V E

E

D

P

O

L

R

C

E

O

U

G

U

I

D

E

M

U

E

T

C

N

T

T

B

I

A

F

V

T

I

I

N

9

4

A 8

R

2

D

I

C

K

E

R

I 11

L

P

E

R

O

T Y

O

C

O

L

S

N

Across

Down

3. This typically handles the information itself in the app. [DATASOURCE] 6. This is the document apple uses to evaluate apps for the App Store. [HUMANINTERFACEGUIDE] 7. You see this listed in the view and it controls the view. [FILESOWNER] 9. This component allows for controlled input from several selections. [PICKER] 10. This type of app is typically one screen, and gives you the basics with minimal interaction. [UTILITY] 11. These define to which messages the datasource and delegate respond. [PROTOCOLS]

1. This typically contains the logic that controls the flow of information in an app. [DELEGATE] 2. The best way to figure out what protocols you need to worry about is to check the ____________. [DOCUMENTATION] 4. This app type typically involves hierarchical data. [PRODUCTIVITY] 5. This app type is mostly custom controllers and graphics. [IMMERSIVE] 8. The other name for an *.xib file. [NIBFILE]

94   Chapter 2

iOS app patterns

We’ve listed a couple of descriptions of a some different apps. Using the app description, sketch out a rough view and answer the questions about each one. 1

Generic giant button app There are several of these currently up for sale on the App Store. This app consists of pushing a big button and getting some noise out of your iPhone.

Big button that you push

What type of app is this?

An immersive app What are the main concerns in the HIG about this app type?

The big thing Apple cares about is that controls “provide an internally consistent experience.” So everything can be custom, but it needs to focused and well organized. 2

Book inventory app. This app’s mission is to keep a list of the books in your library, along with a quick blurb of what each is about and the author. What type of app is this?

A productivity app

Just one View

Some navigation stuff here

Book list

What are the main concerns in the HIG about this app type?

The HIG has many more specific rules about this app type, because you’ll be using standard controls. EACH control needs to be checked out for proper usage. Another View for details, need to figure out how to get to it... you are here 4   95

your iOS toolbox

Your iOS Toolbox You’ve got Chapter 2 under your belt and now you’ve added protocols, delegates, and datasources to your toolbox.

Protocomlesssages your

Define the and delegate must datasource . respond to a header in d e r la c e yd Are typicall (.h) file. uired and q e r h t o b in May conta thods. m optional e

ƒƒ The picker needs a delegate and datasource to work. ƒƒ In a picker, each dial is a component.

96   Chapter 2

Delegateor the behavior of a

f Responsible . t UI elemen ols that contr ic g lo e h t Contains f information, like the flow o isplaying data, and saving or d is seen when. which view the e object as wn m a s in e b so Can , but has it e c r u o s a t a d tocols. specific pro

Datasourbrcidege between the

e o Provides th the data it needs t d n control a show. , bases, plists fo a t a d h it w in ral Works other gene to display. d n a , s e g a im pp will need that your a sa me object a specific a s e h t e b wn Can ut has its o b , e t a g le e d protocols.

ƒƒ In a picker, each item is a row. ƒƒ Protocols define the messages your class must realize—though some of them might be optional.

This is Renee, Mike’s girlfriend.

CHAPTER 2

iOS app patterns

It’s so great that Mike and I are communicating now! But I’ve noticed that Mike’s starting to sound like he’s in a rut, saying the same thing over and over again. Is there something we need to talk about?

Sounds like Mike is going to need some modifications to InstaEmail to keep his relationship on solid ground...

you are here 4   97

3 objective-c for iOS

Email needs variety I know these are letters and all, but I have no idea what you’re saying...

We did a lot in Chapter 2, but what language was that? Parts of the code you’ve been writing might look familiar, but it’s time you got a sense of what’s really going on under the hood. The iOS SDK comes with great tools that mean you don’t need to write code for everything, but you can’t really write apps without learning something about the underlying language, including properties, message passing, and memory management. Unless you work that out, all your apps will be just default widgets! And you want more than just widgets, right?

this is a new chapter   99

renee wants more

Renee is catching on.... Mike has been diligently using InstaEmail to communicate his feelings, but his girlfriend is starting to think something weird is going on. Even for Mike, who is a guy who likes his routines, his emails are starting to sound suspicious.

InstaEmail is working great and is so easy to use! But I think Renee is on to me. She said I sound like I’m in a rut. I need to be able to add some variation to my emails or this isn’t going to work much longer.

We need to make some adjustments to our InstaEmail design. Take a look at the various UI controls available in Interface Builder, and think about what would be a quick and easy way for Mike to add to his emails.

100   Chapter 3

objective-c for iOS

Make room for custom input

We’ll put text fieldthe in here.

It’s nothing fancy, but Mike could add a little personal flavor to his emails with a text field at the start. It means he’ll need to do some typing, but in the end his emails will be more unique.

Scoot this stuff down a little.

Design Magnets

Using what you know from adding the picker and the button, match the magnet with the method or file that you’ll need to edit to add the text field.

1



2



to the top of InstaEmailViewController.m.

3



to the dealloc in InstaEmailViewController.m.

4



to InstaEmailViewController.h.

using Interface Builder. 5



to the property created in step #1, using Interface Builder. Create a de legate and datasource for the no tesField

Declare the UITextField and a property that’s an IBOutlet Add notesField_ to @synthesize

Add [notes field_ rele ase]

Add UITextField to the view

Add an IBAction for the UITextField Link the UITextField to the IBOutlet

you are here 4   101

code magnets solution

Design Magnets Solution

Using what you know from adding the picker and the button, match the magnet with the method or file that you’ll need to edit to add the text field.



1

Declare the UITextField and a property that’s an IBOutlet

to InstaEmailViewController.h.

@interface InstaEmailViewController : UIViewController {

UIPickerView *emailPicker_;



NSArray *feelings_;



}

NSArray *activities_;

UITextField *notesField_;

ent extField. To implasems IT U a is ed ne e w cl What need to declare a the new field, we’ell call notesField_ and add a member that w d as an IBOutlet. property marke

@property (nonatomic, retain) IBOutlet UIPickerView *emailPicker; @property (nonatomic, retain) IBOutlet UITextField *notesField;

Wait a minute. We keep adding code to this .h file, but I still don’t know what a .h file really does! What gives?

InstaEmailViewController.h

A .h file is a header file. It’s where you declare the interface and methods for a class. All of the classes we’ve used so far, like UITextField, NSString, and NSArray, have header files. Take a minute to look through a couple and start thinking about what is happening in those files.

102   Chapter 3

objective-c for iOS

Header files describe the interface to your class In Objective-C, classes are defined with interfaces in the header file. It’s where you declare whether your class inherits from anything, as well as your class’s instance variables, properties, and methods.

Interfaces, clasles’ss, method instance variab d properties declarations, an InstaEmailViewController.h

@interface InstaEmailViewController : UIViewController @property (nonatomic, retain) IBOutlet UIPickerView *emailPicker;

- (IBAction) sendButtonTapped: (id) sender;

Here’s our current InstaEmailViewController.h file. Fill in the blanks and explain what each line does. #import

#import @interface InstaEmailViewController : UIViewController {

UIPickerView *emailPicker_;



NSArray* feelings_;



}

NSArray* activities_;

UITextField *notesField_;

@property (nonatomic, retain) IBOutlet UIPickerView* emailPicker; @property (nonatomic, retain) IBOutlet UITextField* notesField; - (IBAction) sendButtonTapped: (id) sender;

- (IBAction) textFieldDoneEditing:(id) sender; @end

InstaEmailViewController.h

you are here 4   103

sharpen solution

Here’s our current InstaEmailViewController.h file. Fill in the blanks and explain what each line does.

#import

#import

# import incorporates another file (almost always a header file) into this file when it’s compiled. It’s used to pull in classes, constants, etc. from other files.

s #include, except It’s almost identical toprC’events including the that it automatically times (so no more #ifndef same header multiple MY_HEADER). Next comes the class name and, if it inherits from e u’r yo s te ica ind ce fa er @int then a colon and something, ss. cla a re cla the super de to ing go class’s name.

Objective-C doesn’t support multiple inheritance...

@interface InstaEmailViewController: Here, we specify what we inherit from UIViewController { Any protocols yo

brackets separatu implement go in angle are like Java int ed by commas. Protocols classes in C++, anerfaces or pure virtual many as you wan d a class can realize as t.





UIPickerView *emailPicker_;



NSArray* feelings_;



}

NSArray* activities_;

UITextField *notesField_;

InstaEmailViewController.h

104   Chapter 3

This is where we can declare instance variables of our class. The syntax for Basic types like instance variables is just like in types use an ast int and float are used as-is; C++: protected accesserisk. By default, all fields arpointer @private or @pu , but you can change that w e given ith blic sections simila r to C++.

objective-c for iOS

Once you’ve closed the field section of your interface, you can declare properties. @property tells Objective-C that there will be accessor methods for the given property and let you use the ‘.’ notation to access them.

d tells the compiler The @property keywor will be backed by this is a property that er methods. getter and (maybe) sett ibutes; we’ll These are property atetrshortly... talk more about thes

@property (nonatomic, retain) IBOutlet UIPickerView* emailPicker;

type and Here are ouarme, just like the property n e class. field in th

Builder to recognize IBOutlet allows Intercafanceattach to controls (like properties that you InstaEmail). our notes property in

@property (nonatomic, retain) IBOutlet UITextField* notesField;

an instance The minus sign meansit’it’s sa class method). method (a + means tive-C are public. All methods in Objec

These are the method declarations.

- (IBAction) sendButtonTapped: (id) sender;

- (IBAction) textFieldDoneEditing:(id) sender;

s Interface IBAction letntify methods Builder ide e attached to that can b events.

@end

IBAction method signatures can have no argument s, one argument of type id (which is like an Obje ct refe renc in Java), or two arguments where one is the id of e sender and one is a UIEvent*.containing the even the t that triggered the call.

@end: ends your class interface declaration.

InstaEmailViewController.h

you are here 4   105

magnets solution

Design Magnets Solution (Continued)

Back in that design we were working on...

Using what you know from adding the picker and the button, match the magnet with the method or file that you’ll need to edit to add the text field.



1



Declare the UITextField and a property that’s an IBOutlet

to InstaEmailViewController.h.

NSArray *feelings_;

InstaEmailViewController.h

UITextField *notesField_;



}

@property (nonatomic, retain) IBOutlet UIPickerView *emailPicker; @property (nonatomic, retain) IBOutlet UITextField *notesField;

2

@ Add notesField to ze synthesi



@synthesize emailPicker=emailPicker_; @synthesize notesField=notesField_;

InstaEmailViewController.m

Here, you synthesize e accessor methods for the property. Thth e eq ua compiler that we’re mapp ls sign tells the property to the notesFieling our notesField class. It’s notesField_ th d_ field on the at actually holds our values.

OK, so if we declared a property in the .h file, then adding @synthesize in the .m file must auto-generate some code, right?

Yes! It generates the getter and setter methods. Using @property lets the compiler know we have a property, but that’s not enough. Using the @synthesize keyword in the implementation files, we can have the compiler auto-generate the setter and getter method we talked about earlier. The compiler will generate a getter, and, if it’s a readwrite property, a setter and implement it based on the @property attributes declared in the .h file. So what do the different @property attributes do...?

106   Chapter 3

to the top of InstaEmailViewController.m.

objective-c for iOS

Below is a list of the most commonly used property attributes and definitions. Match each attribute with its definition.

readonly

When you want the property to be modifiable by people. The compiler will generate a getter and a setter for you. This is the default.

retain

When you’re dealing with basic types, like ints, floats, etc. The compiler just creates a setter with a simple myField = value statement. This is the default, but not usually what you want.

readwrite

copy

assign

When you’re dealing with object values. The compiler will retain the value you pass in (we’ll talk more about retaining in a minute) and release the old value when a new one comes in. When you don’t want people modifying the property. You can still change the field value backing the property, but the compiler won’t generate a setter. When you want to hold onto a copy of some value instead of the value itself; for example, if you want to hold onto an array and don’t want people to be able to change its contents after they set it. This sends a copy message to the value passed in, then keeps that.

you are here 4   107

who does what solution

SOlUTion Below is a list of the most commonly used property attributes and definitions. Match each attribute with its definition.

readonly

retain

readwrite

copy

assign

Q:

How does the compiler know what field to use to hold the property value?

A:

By default, the compiler assumes the property name is the same as the field name, however, it doesn’t have to be. We prefer to explicitly name them differently so you know when you’re using the property versus the field. You can specify the field to use to back a property when you @synthesize it like this: @synthesize secretString=superSecretField_;.

108   Chapter 3

When you want the property to be modifiable by people. The compiler will generate a getter and a setter for you. This is the default. When you’re dealing with basic types, like ints, floats, etc. The compiler just creates a setter with a simple myField = value statement. This is the default, but not usually what you want. When you’re dealing with object values. The compiler will retain the value you pass in (we’ll talk more about retaining in a minute) and release the old value when a new one comes in. When you don’t want people modifying the property. You can still change the instance variable value backing the property, but the compiler won’t generate a setter. When you want to hold onto a copy of some value instead of the value itself; for example, if you want to hold onto an array and don’t want people to be able to change its contents after they set it. This sends a copy message to the value passed in, then keeps that.

Q: A:

What about that nonatomic keyword?

By default, generated accessors are multithread safe and use mutexes when changing a property value. These are considered atomic. However, if your class isn’t being used by multiple threads, that’s a waste. You can tell the compiler to skip the whole mutex thing by declaring your property as nonatomic. Note that just making your properties atomic doesn’t mean your whole class is thread safe, so be careful here.

objective-c for iOS

Auto-generated accessors also handle memory management Objective-C under iOS doesn’t have a garbage collector and instead uses a mechanism called reference counting. That involves keeping up with how many references there are to an object, and only freeing it up when the count drops to zero (it’s no longer being used). You can think of this as object-ownership. An object can have more than one owner, and as long as it has at least one, it continues to exist. If an object doesn’t have any owners left (its retain count hits 0), it’s freed and cleaned up. When using properties, the compiler handles it for us. The properties we’ve declared so far have all used the retain attribute. When the compiler generates a setter for that property, it will properly handle memory management for us, like this:

ect type and we want Retain says we’re using an objsed to the setter. to hang onto the object pas

ans no

e Nonatomic m locks.

@property (nonatomic, retain) NSString* secretString;

The @pr @synthesize be in you operty line wou r @inter ld face.

secretString=secretString_;

- (NSString*) secretString {

}

ns er just ret.ur il p m o c e h t , g Here thing excitin the value, no

return secretString_;

the property y sa ’t n id d Since we , the compiler will is readonly setter for us. generate a

- (void) setSecretString: (NSString*) newValue {

if (newValue != secretString_) {



}

}

[secretString_ release];

secretString_ = [newValue retain];

the in keyword, sure a t e r e h t d Since we usesetter checks to makedoes generated lue is different, then retain the new va n the old value and a of a release o one—taking ownership on the new assed in.. the value p

Write the code that Objective-C generates for each property declaration below. Assume each one is backed by a field named myField_. 1. @property (nonatomic, readonly) NSString* myField 2. @property (nonatomic, retain) NSString* myField 3. @property (nonatomic, assign) NSString* myField

you are here 4   109

sharpen solution

Below is the code that the compiler will generate for each property. Assume each one is backed by a field named myField_. 1. @property (nonatomic, readonly) NSString* myField

- (NSString*) myField { return myField_; } 2. @property (nonatomic, retain) NSString* myField

- (NSString*) myField { return myField_; } - (void) setMyField: (NSString*) newValue { if (newValue !=myField_) { [myField_ release]; myField_ = [newValue retain]; } }

3. @property (nonatomic, assign) NSString* myField

- (NSString*) myField { return myField_; } - (void) setMyField: (NSString*) newValue { myField_ = newValue; }

110   Chapter 3

Be careful with this one...N tri ngs are reference-counted objects, SS so wh will technically work, having an assile this property for an NSString is pro ign bably a bad idea. leans and floats, However, for basic types like boo Assignment is you can’t do reference counting. almost always what you want.

objective-c for iOS

I bet that release just lets go of the memory that your properties use up, right?

Yes, but more importantly, it gives up object ownership. When you care about an object sticking around, you can take ownership of it by sending it a retain message (and remember, an object can have multiple owners). By sending an object a release message, you relinquish that ownership. If no one else owns the object, it will be cleaned up. Because of this, it’s critically important that you don’t send release messages to objects you don’t own (that is, objects you haven’t already sent a retain message to).

There are times when you want to give up ownership of an object, but you need it to stick around long enough for someone else to take it over. In those cases, Objective-C has the concept of an autorelease pool. This is basically an array of objects that the runtime will call release on after it’s finished processing the current event. To put something in the autorelease pool, you simply send it the autorelease message (instead of a plain release message): [aString autorelease];



It will still have the same retain count and stick around, but after the current event loop finishes, it will be sent a release message on your behalf. You won’t want to use this all the time because it’s not nearly as efficient and releases objects as soon as you’re done with them. It’s not a bad thing to use, but it’s better to explicitly retain and release when you can.

To keep your memory straight, you need to remember just t wo things Memory management can get pretty hairy in larger apps, so Apple has a couple of rules established to keep track of who’s in charge of releasing and retaining when.

We’ll expla about retainin more just a bit—h counts in ang on.

1

You own objects you create with alloc, new, copy, or mutableCopy. If you create an object with alloc, new, copy, or mutableCopy, it will have a retain count of 1, and you’re responsible for sending a release when you’re done with the object. You can also put the object in the autorelease pool if you want the system to handle sending a release later.

2

Assume everything else could go away at the end of the event loop unless you take ownership. If you get an object by any other means (string formatters, array initializers, etc.), you should treat the object as having a retain count of 1 and it will be autoreleased. This means that if you want to hang onto that object outside of the method that got the object, you’ll need to take ownership by sending it a retain (and a corresponding release later when you’re done with it). you are here 4   111

memory management up close

Memory Management Up Close

This is some of the memory management code that YOU have already written!

c { - (void)deallo release]; [emailPicker_ release]; [activities_ lease]; [feelings_ re release]; [notesField_ c]; [super deallo }

Memory management is definitely important on iOS, but that doesn’t mean it’s complicated. Once you get the hang of a few key principles, you’ll be able to structure your app so that it doesn’t leak memory and get you kicked out of the app store. When you create an object, it starts with a count of 1, and different things you do can raise and lower the count. When the count reaches 0, the object is released and the memory is made available.

here’s no At first,atllocated.... NSString memory

*aString = [[NSString alloc] init];

to 0, ount gets is called [aString release]; Once the c ’s dealloc the objecmt emory is freed. and the 1 s reference it e s a le e r . ...and one with it when it’s d [someArray removeObject:aString];

If anoth this arrayer object (like hang onto ) needs to it will tak your object, by sending e ownership message to a retain it.... 112   Chapter 3

s, alloc a clas ...when yoruence count is the refe set to 1.

1

[aString retain];

ll adds one a c in a t e r Each ount... to the c 2

2 1 [someArray addObject:aString];

[aString release];

ase call ...and a resleone. subtract

objective-c for iOS

Determine how many references are left at the end of the chunk of code and whether we have to send it a release for each string. Final Reference Count NSString *first = [[NSString alloc] init];

NSString *second = [[NSString alloc] init];

[someStringArray addObject:second];

NSString *third = [[NSString alloc] init];

[third autorelease];

NSString *fourth = [NSString stringWithFormat:@”Do not read %@”, @”Swimming with your iPhone by TuMuch Monee”];

NSMutableArray *newArray = [[NSMutableArray alloc] init];

NSString *fifth = [[NSString alloc] initWithFormat:@”Read this instead: %@”, “Financing your iPhone 4G by Cerius Savar”]; [newArray addObject:fifth]; [newArray release];

NSString *sixth = [NSString stringWithString:@”Toughie”];

NSArray *anotherArray = [NSArray arrayWithObjects:sixth count:1];

NSDictionary *newDictionary = [NSDictionary dictionaryWithObjects:sixth forKeys:@”Toughie” count:1]; NSString *ignoreMe = [sixth retain];

you are here 4   113

keeping count

Determine how many references are left at the end of the chunk of code and whether we have to send it a release for each string. Final Count

NSString *first = [[NSString alloc] init];

NSString *second = [[NSString alloc] init];

[someStringArray addObject:second];

NSString *third = [[NSString alloc] init];

[third autorelease];

NSString *fourth = [NSString stringWithFormat:@”Do not read %@”, @”Swimming with your iPhone by TuMuch Monee”];

114   Chapter 3

1

Reference count will be 1 because alloc automatically sets count to 1.

“second” will have a retain count of 2 after this block of code: 1 from the alloc, 1 from inserting it into 2 the array. Arrays automatically take ownership of items added to them.

1

1

This still has a retain count of 1 because of the alloc, but is now in the autorelease pool, meaning it will be sent a release automatically by the system later.

This will have a retain count of 1, but will be in the autorelease pool because we don’t own it (we didn’t get it via an alloc, new, or some form of a copy).

objective-c for iOS

Determine how many references are left at the end of the chunk of code and if we have to send it a release for each string. Final Count

“fifth” will have a retain count of 1: NSMutableArray *newArray = [[NSMutableArray alloc] init];

NSString *fifth = [[NSString alloc] initWithFormat:@”Read this instead: %@”, “Financing your iPhone 4G by Cerius Savar”]; [newArray addObject:fifth];

First, it gets a retain count of 1 from the alloc. Next, it goes to 2 because “newArray” takes 1 ownership by sending a retain when an object is inserted. Then it goes back to 1 because an array will send a release to all its items when the array is destroyed.

[newArray release];

“sixth” starts out with an autoreleased retain count of 1 from the initial creation (note it wasn’t from alloc, so it’s autoreleased). NSString *sixth = [NSString stringWithString:@”Toughie”];

NSArray *anotherArray = [NSArray arrayWithObjects:sixth count:1];

NSDictionary *newDictionary = [NSDictionary dictionaryWithObjects:sixth forKeys:@”Toughie” count:1]; NSString *ignoreMe = [sixth retain];

Next, another retain from inserting it into the array. Note the array wasn’t alloc’ed either, so it will be autoreleased, too. 4 Then one more retain from the dictionary, also not alloc’ed and will be autoreleased. Finally, an explicit retain... So, even though “sixth” has a retain count of 4, we, the developers, only need to send one release to “sixth” and let everything else clean up with the autorelease pool.

Hey, could we get back to my app please?

you are here 4   115

magnets solution

Design Magnets Solution (Continued)

Using what you know from adding the picker and the button, match the magnet with the method or file that you’ll need to edit to add the text field.



1



Declare the UITextField and a property that’s an IBOutlet

to InstaEmailViewController.h.

NSArray *feelings_;

InstaEmailViewController.h

UITextField *notesField_;



}

@property (nonatomic, retain) IBOutlet UIPickerView *emailPicker; @property (nonatomic, retain) IBOutlet UITextField *notesField;

@ Add notesField to synthesize



2

to the top of InstaEmailViewController.m.

Here, you synthesize from @property. You thnee accessor methods new @synthesize line.. ed to create a

@synthesize emailPicker=emailPicker_; @synthesize notesField=notesField_;

InstaEmailViewController.m



3

Add [notesField_ releas e]

ill automatically —we The property wnc passed to it retain a refere the at in dealloc. need to release

- (void)dealloc {

[emailPicker_ release]; [activities_ release]; [feelings_ release];

[notesField_ release]; [super dealloc];

116   Chapter 3

to the dealloc in InstaEmailViewController.m.

freed and our When we’re being we need to release dealloc is called, the text field. our reference to

InstaEmailViewController.m

We don’t need an action or a datasource for the notes field.

Add an IBAction for the UITextField and delegate Create a esField t o n e e for th c r u o s a t a d

objective-c for iOS



4

Add UITextField to the view

using Interface Builder.

To get into this, you’ll need to open up InstaEmailViewController.xib and find the text field in the library. Then drag and drop the text field in between the “InstaEmail” label and the “I’m .... and feeling ...” labels. You’ll also need to put a label that says “Notes” in front of the text field.

You will probably need to labels, and button down move the picker, a little.

Right-click on the Fi icon and create a newle’Res Owner ference to “notesField.”

5



Link the UITextField to the IBOutlet

to the property created in step #1, using Interface Builder.

Save it and then... you are here 4   117

test drive

Test Drive Now that everything is saved, go back into Xcode and click Build and Run, and launch the Simulator.

Click here to write a note to customize the email....

The UI works!

118   Chapter 3

Hey, we didn’t even have to do anything to make the keyboard show up for the text field. Cool!

objective-c for iOS

Objective-C Exposed This week’s interview:

Who are you anyway?

Head First: Hello Objective-C! Thanks for coming. Objective-C: Thanks! It’s great to be here. I’ve been getting a lot of attention recently with this whole iPhone thing. Head First: So you have a pretty strong lineage, right? Why don’t you tell us a little about yourself ?

object around, you just tell me you want to retain a reference to it. Done with it? Just release your reference. When there aren’t any references left, I’ll clean up the object and free up the memory for you. Head First: Nice. Any other tricks?

Head First: What do you mean you come from C roots?

Objective-C: Oh yeah. You know those getter and setter methods you need to write for other OO languages to wrap fields in a class? Not here. I can automatically generate them for you. Not only that, you can tell me how you want to handle the memory associated with them. Oh, and one of my favorites: I can graft new methods onto classes without a problem. They’re called categories.

Objective-C: Well, nearly all of my syntax is just like C syntax. For loops, types, pointers, etc. You can easily use other C libraries like SQLite with me. Things like that.

Head First: Oh, that’s slick. We’re about out of time, so just one more question. What’s up with all those “NS”s all over the place, like NSString and NSInteger?

Head First: But you’re more than just that, right?

Objective-C: Ah—those are all part of the Cocoa Touch framework. I mentioned my strong lineage earlier; most of the core classes that people use on iOS come from Cocoa Touch, which is a port of Cocoa, which came from OpenStep, which came from NeXTStep, and that’s where the NS comes from. The frameworks are written in Objective-C, but they’re frameworks, not really language things. When you write for iOS, you’ll be using things like that all the time. For example, instead of using char*s for strings, you usually use NSStrings or NSMutableStrings. We all kind of blur together.

Objective-C: Sure. I’m an object-oriented language, so I have classes and objects, but I come from strong C roots. My OO concepts come from Smalltalk. Really, there’s not much to me.

Objective-C: Oh yeah, definitely. Most obviously, I am an OO language, so classes, abstract interfaces (which I call protocols), inheritance, etc. all work great. Head First: So what about memory management? Malloc and free like C? Objective-C: Well, malloc and free work just like they do in C, but I have a really nice memory management model for objects. I use reference counting. Head First: Ah—so you keep track of who’s using what? Objective-C: Yup. If you want to keep an

Head First: This is great information! Thanks again for coming by, and best of luck with the iPhone! Objective-C: No problem. Thanks for having me.

you are here 4   119

no dumb questions

Q:

What happens if I don’t retain an object I’ll need later?

A:

Most likely, the object’s retain count will hit 0 and it will be cleaned up before you get to use it. This will crash your application. Now here’s the sad part: it might not crash your object on the simulator every time. The simulator has a lot more memory to work with and behaves differently than a real iPhone, iPad, or iPod Touch. Everything might look great until you put it on your device to test it. Then sadness ensues.

Q:

What if I release my object too many times?

A:

Basically the same thing. When the reference count hits 0, the object will be released and memory will be freed. Sending that now-freed memory another release message will almost certainly crash your application.

Q:

What if my project works on the simulator and dies on the real phone? Could that be a memory problem?

A:

Absolutely. Memory on a real device is much tighter than on the simulator. We’ll talk more about debugging these and using Instruments to track memory usage and leaks in a later chapter.

Q:

How can I check if I’m managing my memory effectively?

A:

The iOS SDK comes with a great

120   Chapter 3

memory tool called Instruments that can show you how your memory is being used, peak memory usage, how fast you’re allocating and deallocating it, and possibly most importantly, if you’re leaking memory.

Q: A:

What happens if I set things to nil?

Well, it depends on what you’re setting to nil. If it’s just a local variable, nothing. The variable is now nil, but the memory for the object it used to point to is still allocated and you’ve almost certainly leaked something. Now, if it’s a property, things are probably a little different. Read on to the next question.

Q:

Do I have to retain things I want to set on my properties?

A:

No. Well, probably not. That’s what the “retain” parameter is on the @property declaration. If you put retain there, the property will automatically send values retains and releases when the property is set or cleared. Be careful about clearing properties in your dealloc, though. If you have a property with a retain parameter and it still has a value when your object is released, then whatever that property is set to hasn’t been freed. You must send the instance variable an explicit release in your dealloc. One more quick note: the automatic retain/ release ability of properties only works if you use the “.” notation or the generated setters and getters. If you explicitly modify the field that backs the property, there’s nothing the property can do about it and it can’t retain/ release correctly.

Q:

Doesn’t Objective-C have garbage collection like Java or .NET?

A:

Actually, on the Mac it does. Apple didn’t provide garbage collection on iOS, however, so you need to fall back to reference counting with retain and release.

Q:

What about malloc and free? Can I still use them?

A:

Yes, but not for object types. Malloc and free work fine for basic blocks of memory as they do in C, but use alloc to instantiate classes.

Q:

What’s with that init call that you always put after the alloc?

A:

Objective-C doesn’t have constructors like other object-oriented languages do. Instead, by convention, you can provide one or more init methods. You should always call init on any class you allocate, so you almost always see them together as [[SomeClass alloc] init].

Q:

How do I know if something retains my object, like an array or something?

A:

Basically, you shouldn’t care. Follow the memory rules that say if you got it from alloc, new, copy, or mutableCopy, you have to send it a release. Otherwise, retain/ release it if you want to use it later. Beyond that, let the other classes handle their own memory management.

objective-c for iOS

But when Mike’s finished typing... The textField works great, but how do I get the keyboard to go away?

The keyboard is permanent? Go ahead, play with it and try to get the keyboard to go away. Return won’t do it, and neither will clicking anywhere else on the screen. Not so cool.

BE the architect

Your job is to be the architect and plan how the keyboard needs to behave. Fill in the pattern diagram below to explain what needs to happen to make it go away!

View

How should the view mm unicate what has to happen tocoth e What should the user see? user?

View Controller

What does the Controller need View make these View to do to changes happen? you are here 4   121

be the architect solution

BE the architect solution

Your job is to be architect and plan how the keyboard needs to behave. Fill in the pattern diagram below to explain what needs to happen to make it go away!

View

View Controller

This kind o the View anfd back and forth betw and is going View Controller is c een Remember, a to show up all over thommon behavior for View Controller prov e place. ides the a View.

Q:

Why didn’t we have to do anything to make the keyboard appear in the first place?

A:

When the users indicate that they want to interact with a specific control, iOS gives that control focus and sets it to be the “first responder” to events. When certain controls become the first responder, they trigger the keyboard to show automatically.

122   Chapter 3

The user needs to understand what to do to make the keyboard go away, so change the “return” button to say “done”.

The View Controller needs to receive the “done”message and then make the keyboard go away.

Conventions like using “done” to let the usere hide the keyboard ar G. discussed in Apple’s HI There are lots more; “done” is just one of them.

Let’s start with the View.

objective-c for iOS

Customize your UITextField

r Here’s the button foct . pe the Attributes Ins or

In InstaEmailViewController.xib, select Mike’s custom field and then open up the Attributes Inspector in the Utilities pane. You can specify an initial value of the text in the field (Text); text that the field shows in grey if there’s no other text to display (Placeholder); left, center, or right Alignment; different Borders, etc. For now, you don’t need to add anything for field, so leave these alone.

Change the label on the return key Changing the name of the button in the keyboard (so it’s “done” instead of the “return”) is another option in the inspector. The big thing that changing the label on the button brings to the table is that it clearly communicates to the user what to do to make the keyboard go away. Click on the Return Key pop-up menu and pick Done.

for the “return” There are other optionsare obvious (like button—some of themare little more “Google”) and others ple’s HIG for when to subtle. Check out Ap r ones. use some of the othe



Watch out for the HIG! Beyond the Text and Placeholder fields, changing some of the other options may hurt your usability and make Apple unhappy, so be careful.

Now, get the keyboard to talk to the View Controller... you are here 4   123

message passing

Components that use the keyboard ask it to appear... When users click in the text field, iOS gives that control focus and assigns it as “first responder” for later events. A component can get focus a number of ways: the users explicitly tap on the control, the keyboard is set up so that the Return key moves to the next control they should fill out, the application sets some control to explicitly become first responder because of some event, etc. What a component does when it becomes the first responder varies by component, however; for a UITextField, it asks iOS to display the keyboard. All this chatter between the application and components is fundamental to writing an application, and it all happens through message passing.

...and you ask for things by passing messages to other objects The idea is that whenever one object (whether that object is your ApplicationDelegate, another component, or the GPS in the iPhone) wants some other object to do something, it sends it a message.

When one object wants to communicate with another object, it sends it a message. [activities objectAtIndex:row]

Here, we’re sendin objectAtIndex mesgsathe activities array. ge to the Activities Array

@”sleeping”

And it responds to that message with the value @“sleeping”. In Objective-C, you send a message to an object and it responds to that message (basically returning a value from a method). The Objective-C runtime turns those messages into method calls on objects or classes (in the case of static methods), but get used to thinking about these as messages; you’ll see things like “the receiver of this message will...” all over Apple’s documentation. Now, let’s use message passing to get rid of the keyboard when the user is done with it. 124   Chapter 3

objective-c for iOS

Ask the UITextField to give up focus In order to get the keyboard to go away, we need to tell the text field that the user is done with it. We do this by asking the UITextField to resign its first responder status. Sending messages in Objective-C is easy: you list the receiver of the message, the message to send, and any arguments you need to pass along.

This is a statement any other—don’t fo like rget the semicolon.

ge passing a s s e m d n Surrou uare brackets. [notesField resignFirstResponder]; with sq This is where you put the actual message. Inall This is the receiver for the messa our case, we have no arguments, so this is ge, in our case, the notesField. we need. See the Apple documentation for details on what messages each component will respond to. Is that how the View is sending the View Controller information?

Yes! Our View Controller can respond to a number of messages like sendButtonTapped and viewDidLoad. You’ve been responding to messages all this time. Now here’s the trick: the textField can send a message when the user taps the Done button on they keyboard. We just need to tell it that our ViewController is interested in knowing when that happens.



You can pass messages to nil with no obvious problems. Objective-C lets you send messages to nil without complaining. If you’re used to NullPointerExceptions from other languages, this can make debugging tricky. Be careful of uninitialized variables or nil values coming back as other nil values when you debug.

you are here 4   125

messages up close

Handling Messages Up Close You’ve been handling messages since Chapter 1, but we really haven’t talked about the syntax to make it work. Method declarations go in your header files and the implementation goes in the .m. Here are some snippets from our sendButtonTapped implementation from InstaEmail. Implementation

fi then the name of leths (.m) start with @implementation, e class you’re implem enting.

@implementation InstaEmailViewController

se You specify the respon re type in parentheses befo the message names.

the Next is the name of ssa name message. The full me lleged a ca (with arguments) is d tends to selector in Obj-C an e. be long and descriptiv

If there are arguments to r message, follow the messageyou name with a colon, then the type and of the local variable. Additional name arguments get names, types, and variable names, too.

- (IBAction) sendButtonTapped: (id) sender {

New method code here.



}

The “-” means it method (whereas ’s an instance a static or class a “+” means method).

@end

Finally, provide the implementation of the method declared in our interface.

ssage The syntax for declaring a meto the in a header file is the similarends differently. implementation file, it just

Implementation files all

end with @end. InstaEmailViewController.m

126   Chapter 3

objective-c for iOS

Messages in Objective-C use named arguments In Objective-C, message names tend to be long and descriptive. This really starts to make sense when you see arguments tacked on. When you send a message with arguments, the message and argument names are all specified. Objective-C messages read more like sentences. Let’s look at a method declaration from UIPickerViewDataSource. This method returns the number of rows for a given component in a picker view. It’s declared like this:

Return type

First argument type

Method name

Local argument name

Public name of t second argumen

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfR owsInComponent:(NSInteger)component;

Local name of second argument

Type of second argument

Methods can have internal and external names for arguments; the external name is used when sending the message to the receiver. So when something wants to send this message to our delegate, it creates a call like this:

Receiver

value

Message name

Second argument name

Second argument value

[pickerDelegate pickerView:somePicker numberOfRowsInComponent:2];

Q:

You keep switching terms back and forth between methods and messages. Which is it?

A:

Both are correct, depending on your context. In Objective-C, you send messages to objects and they respond. The Objective-C runtime turns your message into a method call, which returns a value. So, generally you talk about sending some receiver a message, but if you’re implementing what it does in response, you’re implementing a method.

Q:

So about those arguments to methods...what’s the deal with the name before the colon and the one after the type?

have to be the same, so you can use a nice friendly public name for people when they use your class and a convenient local name in your code.

A:

In Objective-C, you can have a public name and a local name for arguments. The public name becomes part of the selector when someone wants to send that message to your object. That’s the name before the colon. The name after the type is the local variable; this is the name of the variable that holds the value. In Objective-C, they don’t

More on selectors in a minute.

you are here 4   127

message to controller

Use message passing to tell our View Controller when the Done button is pressed

Messages going here between UITextField and the controller.

The UITextField can tell our ViewController when the Done button was pressed on the keyboard; we just need to tell it what message to send. We can do this with Interface Builder. You’ll need to declare an action in the .h file and implement it in the .m file: 1

Add the IBAction to InstaEmailViewController.h. Just like we did with the “Send Email” button, go back into Xcode and add this: - (IBAction) sendButtonTapped: (id) sender;

Notice how IBAction is inthe parenthese the return s?t That’s which is secr ype #defined to etly just void.

2

- (IBAction) textFieldDoneEditing:(id) sender;

@end

e ‘-’ says it’s an Here’s the new action.dThtextFieldDoneEditing, instance method, callent. The sender is of type and takes one argumeinter to something). ‘id’ (which means a po

The signat method (vouidre for this and one argu return type ‘id’) is requir ment of type ed for IB actions.

InstaEmailViewController.h

Add the method implementation in InstaEmailViewController.m. Now that we have an action that will be called when the Done button is pressed, we just need to ask the textField to resign its first responder status and it will hide the keyboard.

Implement this just before the dealloc method in your InstaEmailViewController.m. Since the sender is the UITextField, we can send the resignFirstResponder right back to it.

- (IBAction)textFieldDoneEditing:(id) sender {



}

[sender resignFirstResponder];

gument will The sender arne that be the compo evntent. In triggered the ill be the our case, it w UITextField.

InstaEmailViewController.m

Almost there, we just need to wire it up... 128   Chapter 3

objective-c for iOS

3

Connect the UITextField event in the editor. Now that the actions are declared and implemented, go back into Interface Builder by double-clicking on InstaEmailViewController.xib. If you right-click on the UITextField, you’ll bring up the connections.

In the list of events that the UITextField can send, choose the “Did End on Exit” event and connect it to the File’s Owner’s “textFieldDoneEditing” action we just created.

Geek Bits The UITextField has a number of events it can raise, just like the round rectangular b utton. Take a second and check out the list that’s there. Along with the customizing you can do in the Inspector with the field, you can wire up different (or even multiple!) responses to interaction with the field. Keep it in mind for your own apps.

you are here 4   129

no dumb questions

Q:

Why did we send the message back to the sender in our action and not to our notesField property?

A:

Either one would work fine; they’re both references to the same object. We used the sender argument because it would work regardless of whether we had a property that was a reference to our UITextField.

Q:

You mentioned selectors, but I’m still fuzzy on what they are.

A:

Selectors are unique names for methods when Objective-C translates a message into an actual method call. It’s basically the method name and the names of the arguments separated by colons. For instance, look at the code using the



selector pickerView:numberOfRowsInCom ponent. You’ll see them show up again in later chapters when we do more interface connecting in code. For now, Interface Builder is handling it for us.

Q:

When we send the resignFirstResponder message to sender, the sender type is “id”. How does that work?

A:

“id” is an Objective-C type that can point to any Objective-C object. It’s like a void* in C++. Since Objective-C is a dynamically typed language, it’s perfectly ok with sending messages to an object of type “id”. It will figure out at runtime whether or not the object can actually respond to the message.

Q:

What happens if an object can’t respond to a message?

A:

You’ll get an exception. This is the reason you should use strongly typed variables whenever possible—it will generate warnings at compile time, not just runtime problems. However, there are times when generic typing makes a lot of sense, such as callback methods when the sender could be any number of different objects.

Q:

So seriously, brackets for message passing?

A:

Yes. And indexing arrays. We all just have to deal with it.

Beware of private framework headers Sometimes you’ll come across a really tempting method that’s not defined in the Apple Documentation. Using undocumented APIs will get your app rejected from the iTunes store.

ƒƒ In Objective-C, you send messages to receivers. The runtime maps these to method calls.

ƒƒ Method arguments are usually named, and those names are used when sending a message.

ƒƒ Method declarations go in the header (.h) file after the closing brace of an interface.

ƒƒ Arguments can have an internal and external name.

ƒƒ Method implementations go in the implementation (.m) file between the @implementation and the @end.

130   Chapter 3

ƒƒ Use a “-” to indicate an instance method; use “+” to indicate a static method.

objective-c for iOS

Test Drive

Do some typing, go ahead!

It works! The keyboard goes away and you can play around with the text field and add some notes now. Tap the “done” button...

you are here 4   131

custom note is missing

Where’s the custom note? You’re ready to try out the custom field with a demo for Mike, but when he puts in his custom text and sends his message...

Mike checks out the logs

No custom info at all...

The custom note doesn’t do anything! It’s not going to show up in my email.

You can fix this with no problem now that you’ve gotten the hang of events and message passing... 132   Chapter 3

objective-c for iOS

BE the architect

Your job is to be architect and figure out how the UITextField and the send email button need to work together using the View - View Controller model.

View

View Controller

you are here 4   133

build the email

BE the architect solution

Your job is to be architect and figure out how the UITextField and the Send Email button need to work together using the View - View Controller model.

1. Show typed text View

2. Communicate button push

1. Respond to button-tapped message View Controller

2. Get the text from the view 3. Update the datasource with the text.

Build the email with strings We need to incorporate the note text into our email. In order to do that, we’re going to do a little string manipulation with the NSString class. You’ve already built a message to create an email, but this time we have more text to include. Before you refactor the code to send the email with the new text in it, let’s take a closer look at what you did in Chapter 2: Th String

This string didn’t come from allo new, copy, or mutableCopy, so it’lc, l be autoreleased.

e @ before NS method onat and ic t m a e st a ns this shouthe quotes a This is es a string form h ld be it w t rs that tak he format placeholduements. norteated as an NSStrin g, a char*. replaces t you provide as arg s e lu the va

NSString* theMessage = [NSString stringWithFormat:@”I’m %@ and feeling %@ about it.”,



[activities_ objectAtIndex:[emailPicker_ selectedRowInComponent:0]], [feelings_ objectAtIndex:[emailPicker_ selectedRowInComponent:1]]];

NSLog(@”%@”, theMessage);

String you pass NSLog prints out whatever NS r app won’t to the console. End users of you see these message.

te hFormat to crea it W ng ri st he t is e Here, we us ring. Note the %@, which a our message str a string. placeholder fo

Now all you need to do is update this to include the text from the Notes field. Take a look at the magnets on the next page and get it working.

134   Chapter 3

objective-c for iOS

Xcode Magnets

You need to modify InstaEmailViewController.m file to add the custom field to the message. Using the information you just learned and the magnets below, fill in the missing code.

- (IBAction) sendButtonTapped: (id) sender {

NSString* theMessage = [NSString stringWithFormat:@” I’m %@ and feeling %@ about it.”,



[activities_ objectAtIndex:[emailPicker_ selectedRowInComponent:0]],



NSLog(@”%@”, theMessage);



[feelings_ objectAtIndex:[emailPicker_ selectedRowInComponent:1]]];

init]; [[NSString alloc]

notesField_.text

,

%@

[theMessage release]; tResponder sender resignFirs

theMessage

?

: @””

notesField_.text

notesField_.text

you are here 4   135

magnets solution

Xcode Magnets Solution

You need to modify InstaEmailViewController.m file to add the custom field to the message. Using the information you just learned and the magnets below, fill in the missing code.

Here’s our new stri ng placeholder for the notes text .

InstaEmailViewController.m

- (IBAction) sendButtonTapped: (id) sender {

NSString* theMessage = [NSString stringWithFormat:@” %@ I’m %@ and feeling %@ about it.”, notesField_.text

notesField_.text

?

: @””

,

[activities_ objectAtIndex:[emailPicker_ selectedRowInComponent:0]], [feelings_ objectAtIndex:[emailPicker_ selectedRowInComponent:1]]]; NSLog(@”%@”, theMessage);

g. pty strin m e n a d n , so l se is nil, we’lo be an NSString it if d n t ...a quotes. r, it has Remembethe @ before the we put

We have to hand text. If the textle the case where the user didn’t en Here we use the C field is empty, its text property ter any isn’t nil, it will use style ternary operator. If notesFwill be nil. ield.text the value in notesF ield.text.

just like in The ? is a ternary operator,exp ression Java or C++, where if the value; is true it returns the first ond. otherwise, it returns the sec

init]; [[NSString alloc]

[theMessage release]; tResponder sender resignFirs

136   Chapter 3

theMessage

notesField_.text

objective-c for iOS

Test Drive Go ahead and build and run the app with the new text code in it.

Now it has custom text! w00t! It’s so great that we can talk about our feelings...

you are here 4   137

objective-c cross

Objective-C cross Practice some of your new Objective-C Untitled Puzzleterminology.

Header Info 1 Header Info 2 etc...

1

2

3

4

5

6

7

8 9

10

Across

Down

5. The control with focus has _____________ status. 6. This incorporates another file. 7. Unique names for methods after Objective-C translation are _________. 8. Signals that the compiler will retain the object. 9. Automatic methods. 10. This tells the compiler to skip mutexes.

1. An array of objects that will be released after the current event. 2. A "+" before a method declaration indicates that it's a ____________. 3. This is sent between objects. 4. _________ management is important for iPhone apps.

138   Chapter 3

objective-c for iOS

Your Objective-C Toolbox CHAPTER 3

You’ve got Chapter 3 under your belt and now you’ve added Objective-C to your toolbox.

Attribute

You want it...

readwrite

When you want the property to be modifiable by people. The compiler will generate a getter and a setter for you. This is the default. When you don’t want people modifying the property. You can still change the field value backing the property, but the compiler won’t generate a setter. When you’re dealing with basic types, like ints, floats, etc. The compiler just creates a setter with a simple myField = value statement. This is the default, but not usually what you want. When you’re dealing with object values. The compiler will retain the value you pass in and release the old value when a new one comes in. When you want to hold onto a copy of some value instead of the value itself. For example, if you want to hold onto an array and don’t want people to be able to change its contents after they set it. This sends a copy message to the value passed in, then keeps that.

readonly assign retain copy

C e v i t c e j b O s of iOS app

age Is the langu e ted languag n ie r o t c e j Is an ob for memory g in t n u o c e nc Uses referet managemen and dynamic g in s s a p e g a Uses mess typing ces and interfa e c n a it r e h Has in

Memory Management

You own objects you create through an alloc, new, copy, or mutableCopy. You need to take ownership of objects you get through other means if you want to stick around by sending them a retain message. You must release objects you own. Assume everything else will be cleaned up later (pretend it has a retain count of 1 and is in the autorelease pool). you are here 4   139

objective-c cross solution

Objective-C cross Solution Untitled Puzzle

Header Info 1 Header Info 2 etc...

Practice some of your new Objective-C terminology.

1

A U

2 5

F

I

R

S

S T

R

E

S

P

A 6

I

M P

O

R

@

10

N

P

O

R

N

O

A

P

T

O

T

E

I

L

C

E

E

A R

T

I

E

E

H

P

D

N

D

E

I

C

M

R

E

S 7

8

R

S

M E

L

E

C

T

O

A

R

G

Y

E

T

A

I

R

S

N

S

T O M

4

M

R

M 9

3

T

O O L

Across

Down

5. The control with focus has _____________ status. [FIRSTRESPONDER] 6. This incorporates another file. [IMPORT] 7. Unique names for methods after Objective-C translation are _________. [SELECTORS] 8. Signals that the compiler will retain the object. [RETAIN] 9. Automatic methods. [@PROPERTIES] 10. This tells the compiler to skip mutexes. [NONATOMIC]

1. An array of objects that will be released after the current event. [AUTORELEASEPOOL] 2. A "+" before a method declaration indicates that it's a ____________. [STATICMETHOD] 3. This is sent between objects. [MESSAGE] 4. _________ management is important for iPhone apps. [MEMORY]

140   Chapter 3

4 multiple views

A table with a view I like my coffee with two sugars, cream, a sprinkle of cinnamon, stirred twice, then...

Most iOS apps have more than one view. We’ve written a cool app with one view, but anyone who’s used a smartphone knows that most apps aren’t like that. Some of the more impressive iOS apps out there do a great job of working with complex information by using multiple views. We’re going to start with navigation controllers and table views, like the kind you see in your Mail and Contacts apps. Only we’re going to do it with a twist...

this is a new chapter   141

mix it up

Look, I don’t have time for talking about my feelings. I need to know a ton of drink recipes every night. Is there an app for that?

Sam, bartender at the HF Lounge

This chapter is about apps with more than one view. What views would you need to have for a bartending app?

142   Chapter 4

multiple views

UI Design Magnets

Using the components shown below, lay out the two views we’ll be using for the app.

View #1

View #2 UITextField with placeholder text

Navigation title bars UITableView

UILabels

Keyboard

UITextViews

you are here 4  143

ui design magnets solution

UI Design Magnets Solution

Using the components shown below, lay out the two views we’ll be using for the app.

We’ll call iter. Drink Mix

This bar w back and foilrl have buttons, like the ward button browser. s in a web

It will also show your app’s title.

UITableView

UITextField with placeholder text

UILabels UITextViews

View #1

Sam needs a list of drink names and to be able to look up what’s in them. He’ll also want to know how much he needs of each ingredient and any instructions—what’s on the rocks, whether to shake or stir, when to light things on fire, etc. So for our two views, we’ll put the drinks in a list (View #1), then when Sam taps on one, we’ll show the details (View #2). 144   Chapter 4

View #2

We’re not going to use the keyboard for now—it’s a reference app, and Sam just needs to read stuff...

multiple views

So, how do these views fit together? Before you pick the template for our bartending app, take a minute to look at how you want the user to interact with the drink information. We’re going to have a scrollable list of drink names, and when the user taps on a row, we’ll show the detailed drink information using view #2, our detail view. Once our user has seen enough, they’re going to want to go back to the drink list.

with rs are done n, se u r u o e c n O informatio the detailetdion bar gives the Navigay to get back to them a wa the list.

Title Name: Ingredients:

Directions:

Title

Title

drink Tapping on ae list will name in thhe detail bring up t hat drink. view for t

Name: Ingredients:

Directions:

Drink #1 Drink #1 Drink #1

o We’re going ktind of e m so want tween transition s.be.. these view

We need a list work with... of items to

Title Name: Ingredients:

Directions:

o be coming We’re going otf this view in and out h time our user a lot—eac rink. selects a d

Below are the templates available for an app. Which do you think we should use for DrinkMixer? Window-based Application

View-based Application

Utility Application

Tab Bar Application

OpenGL ES Application

Navigation-based Application

Split view-based Application

you are here 4  145

working with hierarchical data

The navigation template pulls multiple views together For this app, we’re going to use a Navigation-based project. The navigation template comes with a lot of functionality built in, including a navigation controller, which handles transitions between views, and the ability to deal with hierarchical data. Hierarchical data means there's layers to it, and each view gives you more detail than the previous one.

The built-in navigation controller provides back buttons, title bars, and a view history that will keep your user moving through the data without getting lost.

Lemon Drop

Firecracker

Lemon Drop: Citron vodka, lemon, and sugar. Add sugar to Firecracker: Wild the rim of glass, turkey and hot sauce. pour ingredients into Pour ingredients into shaker...a rocks glass filled with ice.

The navigation template us to move through hierahelps data, starting with a ta rchical that lists all the drinks. ble view

146   Chapter 4

Do this!

The Navigation Controller provides transitions between views, with really nice animations.

To get started, go into Xcode and choose the File→New Project option. Choose the Navigation-based application and name it DrinkMixer. Make sure that “Use Core Data” and “Include Unit Tests” are not checked.

multiple views

The table view is built in The navigation template comes with a navigation controller and a root view that the controller displays on startup. That root view is set up with a table view by default, and that works great for our app, so we'll keep it that way. A table view is typically used for listing items, one of which can then be selected for more details about that item.

Navigation template

find This is where you’s,ll forward on tt the back bu tle of the buttons, and the ti view you’re in.

this The contents of ad from inner view are lo leed b. xi r. RootViewControl

The navigation controller provides a navigation bar.

The table view

The table view provides an easy way to work with data. It starts with an empty, scrollable list for the main view of your application.

you are here 4  147

no dumb questions

Q:

If the navigation template is about handing lots of views, why does it only come with one?

A:

Most navigation-based applications start out with a table view and show detailed views from there. The number of detailed views, what they look like, etc., are very application-specific, so you have to decide which views you want and add those views. The navigation template doesn’t assume anything beyond the initial table view.

Q:

Which built-in apps on iOS use the Navigation control?

A:

Contacts and Mail, which are both core iOS apps, use this design. It’s a good idea to spend some time with those apps on your phone to see how the entire template is implemented. For a neat twist, take a look at the Messages (SMS) app. That one uses a Navigation Controller but frequently starts

148   Chapter 4

in the “detail” view, showing the last person you sent or received a message from. On the iPad, Mail uses navigation control, too, but it’s part of a split view-based application. We’ll get into that more in a couple of chapters.

Q:

The UITextField looks editable. Shouldn't we fix that?

A:

Maybe. We're going to go on to use that field in a few different ways, so think about it again at the end of the app in a couple of chapters.

To make it look less editable, you can change the font or change the border.

Q:

Do I have to use a table view for my root view?

A:

No, it’s just the most common, since it provides a natural way to show an overview

of a lot of data and have the user drill down for more information. Table views are very easily customized, too, so some apps that might not seem like table views really are, like Notes or the iTunes store, for example.

Q:

How does the navigation controller relate to view controllers?

A:

We'll talk a lot more about this in a minute, but the Navigation Controller coordinates the transition between view controllers. Typically, each top-level view is backed by a View Controller, and as views transition onscreen, the corresponding View Controller starts getting events from its view. There's a whole view lifecycle that we'll work through that lets a View Controller know what's going on with its view.

multiple views

Test Drive Add a title to the main view, and take a look at what your empty table view will look like. Open up MainWindow.xib in Interface Builder.

Left-click on the navigation control and bring up the utilities.

And add the title in the Inspector, here.

you are here 4  149

test drive

Test Drive

Add a title to the main view right away, and take a look at what your empty table view will look like. Open up MainWindow.xib in Interface Builder.

—we’ll avigation basrsoon. n e h t 's e r e H nal button add directio

This is the tabholeld view and will our drink list. Each line is an empty table cell.



If you don’t add the title here, you won’t have a back button later. The navigation controller uses the title of the current view as the label in a back button when presenting a second, more detailed view.

150   Chapter 4

multiple views

The Table View Up Close Navigation controllers and table views are almost always used together to work with hierarchical data. When you selected the navigation-based project as your template, Xcode created a different view setup than we’ve used in the past. The template includes MainWindow.xib, which has a single UINavigationController in it. That controller starts out with a main view, which is a UITableView that is loaded from RootViewController.xib (which is actually a subclass of UITableViewController.xib).

ller understands This UINavigationContvierows, can move the idea of multiple e animations, and between them with nicfor buttons on the has built-in support navigation bar.

MainWindow.xib

This is the UITableView that is loaded from the RootViewController.xib. The UITableViewController provides some basic table behavior, like configuring the datasource and delegate if it’s loaded from a nib, and providing editing state controls. We’ll talk about these more as we go.

RootViewController.xib

you are here 4  151

View

come together, right now

Model

A table is a collection of cells The UITableView provides a lot of the functionality we need right away, but it still needs to know what data we’re actually trying to show and what to do when the user interacts with that data. This is where the datasource and delegate come in. A table view is easy to customize and is set up by the template to talk to the datasource and delegate to see what it needs to show, how many rows, what table cells to use, etc.

The navigation ntroller, not the table view, co pr navigation bar. S ovides the editing the view ince we’re navigation contronow, there’s no l. Table views have support for edit built-in contents, includining their around, deleting g moving rows adding new ones. rows, and Table views can t user taps on a ceell you when your the section and ll. It’ll tell you row that was tapped. We’re using the defa table view cell, but you can creault te and lay them out any wayour own y you want.

Datasource

Controller

Delegate Remember that MVC pa tt er n from Chapter 1? Now we're adding datasource for our drininkthe Model piece with a s list.

ade up of A table view is cemlls. The table multiple table w many cells (or view will ask ho ch section. rows) are in ea

, multiple sections A table can haioven can have a header and each sect We only have one and a footer. don’t need either for section, so we DrinkMixer. have one A table can onlyu can put column, but yowant in that whatever you omizing your column by cust table cells.

Look through some of the apps you have on your device. What are some of the most customized table views you can find? Are they using sections? Are they grouped? How did they lay out their cells?

152   Chapter 4

Table

multiple views

Table Cell Code Up Close Below is an excerpt from our updated RootViewController.m file. This is where we create table cells and populate them with the drink list information. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } // Customize the number of rows in the table view. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection :(NSInteger)section { These methods tell the table view return [self.drinks count]; } how many sections we have and how

many rows are in each section. This method is called resentation of the The indexPath contains a repthe when the table view needed cell. section and row number for needs a cell.

// Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath :(NSIndexPath *)indexPath { static NSString *CellIdentifier = @”Cell”;

iers representing Table cells have identif try to find a a cell type, so when yon ube sure you’re cell for reuse, you ca d. grabbing the right kin

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellI dentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDe fault reuseIdentifier:CellIdentifier] autorelease]; } th the table view to

Here, we check wi reusable cells with see if there are any ier available. the given cell identif

If there aren’t any available for reuse, e. we’ll create a new on

// Configure the cell. cell.textLabel.text = [self.drinks objectAtIndex:indexPath.row]; return cell; }

e text in the Here, we customize thtio for the cell with the informa ton show. specific drink we need

you are here 4  153

populate your table view

Q:

row, you can check that queue of cells to see if there are any available for use.

A:

I don’t understand the cell identifier...does it have to be “Cell”?

How do cells get into that reusable list to begin with?

The table view handles that. When cells scroll off the screen (either the top or the bottom), the table view will queue up cells that are no longer needed. When it asks the datasource for a cell for a particular

Q:

A:

No—that’s just the default. When you do more complex table views, you can create custom cell types depending on what

data you’re trying to display. You use the cell identifier to make sure that when you ask for a reusable cell, the table view gives you back the type you expect. The identifier can be anything you want—just make sure you have a unique name for each unique cell type you use.

It’s time to start displaying some drinks. You’ll need to make some modifications to both the RootViewController.h and RootViewController.m files.

1

Declare the drinks array. Using syntax similar to what we used for the picker, declare an array called "drinks" in RootViewController.h with the necessary properties declaration.

2

Implement and populate the array. In RootViewController.m, uncomment and expand the viewDidLoad method to create the array with the three drinks from the drink list here.

3

Tell the table how many rows you have. The auto-generated code needs to be modified to tell the table that it will have the same number of rows as there are drinks in the array. Modify the implementation file under this line: // Customize the number of rows in the table view.

4

154   Chapter 4

Populate the table cells. Implement the code that we talked about in “Table Cell Code Up Close” so that the table gets populated with the items from the array.

Drink List: Firecracker Lemon Drop Mojito

multiple views Wait, memory in iOS is a big deal, right? Three drinks is no problem, but what happens if we add a whole bunch of drinks?

You're right. Like everything else on iOS, the UITableView does have to worry about memory. So, how does it balance concerns about memory with an unknown amount of data to display? It breaks things up into cells.

Each drink gets its own temporary cell

n ai pt Ca

ll a view has to scro When the tablethe screen, it asks the new row onto r a cell for that row. datasource fo

ecr ack er

The UITableView only has to display enough data to fill an iPhone screen—it doesn’t really matter how much data you might have in total. The UITableView does this by reusing cells that scrolled off the screen.

The cells that are off th bucket until iOS needs mee view go into a view can reuse them when mory or the table the user scrolls.

As the use some cells slridscrolls, the screen. e off Firecracker

checks the cell The datasourceif there are any bucket to see to reuse. If so, it cells available he row’s contents just replaces the row. and returns t Datasource

any for If there aren’tasource reuse, the dat one and creates a new t. sets its conten

bleview The tsa the new take nd scrolls cell a it in.

Lemon Drop

Absolut Mixer

This is the active view with the tab cells that ar le currently vis e ible.

Bee Stinger Cupid’s

Mojito

Miami Vice Captain

In this part of the MVC pattern, the datasource (M) updates the view (V) whenever it has to scroll to a new row. you are here 4  155

sharpen your pencil solution

It’s time to start displaying some drinks. You’ll need to make some modifications to both the RootViewController.h and RootViewController.m files.

Declare the drinks array.

1

handles UITableViewController leg ate the datasource and deyou don’t protocol for you, so re. need to declare it he

@interface RootViewController : UITableViewController {



}

NSMutableArray* drinks_;

Add the new drinks array.

@property (nonatomic, retain) NSMutableArray* drinks;

@end

after our instance Note that we use _' er tiate it variable name to diff meen—you can't from our property na the wrong one. accidentally refer to

Declare the properties for the drinks array.

RootViewController.h

@synthesize drinks=drinks_;

RootViewController.m

(void)dealloc {

[drinks_ release];

}

@end

156   Chapter 4

[super dealloc];

multiple views

Implement and populate the array. In RootViewController.m, uncomment and expand the ViewDidLoad method.

2

- (void)viewDidLoad {

e We’re using tahble instance varie since directly her wn the we already o new array.

[super viewDidLoad];

drinks_ = [[NSMutableArray alloc] initWithObjects:@”Firecracker”, @”Lemon Drop”, @”Mojito”, nil]; s we gave you.

Here's the three drink

RootViewController.m

3

Tell the table how many rows you have.

//Customize the number of rows in the table view.

This used to say return: 0.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsIn Section:(NSInteger)section { Now it tells the table view that we return [self.drinks count];

}

have the same number of rows as the number of items in the drinks array.

Populate the table cells.

4

// Configure the cell.

od The full metphage is shown on 153.

cell.textLabel.text = [self.drinks objectAtIndex:indexPath.row];

}

return cell;

Here, we customize the text in the cell with the information for the specific drink we need to show.

you are here 4  157

a taste of what’s to come

Test Drive

Now you’re ready to go. Save it and run it, and you’ll see the three drinks in your app in the main view.

e Try it out—th scroll, too!

list will

Everything looks great. I’ll just email over our complete list—it’s 40 drinks...

158   Chapter 4

multiple views

Q:

before our drink names?

You mentioned the table view’s datasource and delegate, but why didn’t I have to conform to anything like we did with UIPickerView?

A:

Great catch. Normally you would, but the navigation-based template we used has already set this up. To see what’s happening, look at the RootViewController.h file. You’ll see that it is a subclass of UITableViewController, and that class conforms to the UITableViewDataSourceProtocol and the UITableViewDelegateProtocol. If you look in RootViewController.xib, you’ll see that the table view’s datasource and delegate are both set to be our RootViewController. If we weren’t using a template, you’d have to set these up yourself (we’ll revisit this again later in the book).

Q:

I noticed we used an NSMutableArray. Is that because we had to initialize it?

A:

No—both NSMutableArray and NSArray can be initialized with values when you create them. We’re using an NSMutableArray because we’re going to manipulate the contents of this array later. We’ll get there in a minute.

Q:

What’s the nil at the end of the drink names when we create the drink array?

A:

NSMutableArray’s initializer takes a variable number of arguments. It uses nil to know it’s reached the end of the arguments. The last element in the array will be the value before the nil—nil won’t (and can’t) be added to the array.

Q:

Tell me again about that @ symbol

A:

The @ symbol is shorthand for creating an NSString. NSArrays store arrays of objects, so we need to convert our text names (char*s) to NSStrings. We do that by putting an @ in front of the text constant.

Q:

When we customized the table view cells, we used the cell.textLabel. Are there other labels? What’s the difference between cell.textLabel and cell.text?

A:

Before the iPhone 3.0 SDK, there was just one label and set of disclosure indicators in the default cell, and it was all handled by the cell itself. You just set the text you wanted on the cell.text property. Nearly everyone wanted a little more information on the table cells, so in the iPhone 3.0 SDK, Apple added a few different styles with different label layouts. Once they did that, they introduced specific properties for the different text areas, like textLabel, detailLabel, etc., and deprecated the old cell.text property. You shouldn’t use cell.text in your apps—Apple will likely remove it at some point in the future. We’ll talk more about the other labels later in the chapter.

Q:

You mention that we can use section headers and footers—how do you specify those?

A:

The datasource is responsible for that information, too. There are optional methods you can provide that return the title for section headers and the title for section footers based on the section number. They work a lot like our cellForRowAtIndexPath, except they only return strings.

Q:

What’s the difference between a plain table view and a grouped table view?

A:

The only difference is the appearance. In a plain table view, like the one we’re using, all the sections touch each other and are separated by the section header and footer if you have them. In a grouped table view, the table view puts space between the sections and shows the section header in bigger letters. Take a look at your contact list, then select a contact. The first view, where all of your contacts are listed together and separated by letters, is a plain table view. The detailed view, where the phone numbers are separated from email addresses, etc., is a grouped table view.

Q:

So why are you putting underscores after instance variables again?

A:

Remember there are really different things in play when we talk about instance variables and properties. Instance variables are the actual attributes that hold data. We use _'s after those names to help indicate that those are actually internal class information, not really something we want other classes poking around with (we also mark them as private so the compiler enforces that for us). Next, we have properties. By declaring something as a property, we get the ability to use the dot notation. Accessing a property through the dot notation gets turned into a call to setSomeProperty(...) or getSomeProperty(...). Memory management, copying values we want copied, etc., are all handled in those accessors. To make sure we don't accidentally refer to an instance variable when we really want the functionality of the accessors and vice-versa, we name the instance variable with an '_' and property without.

you are here 4  159

what’s in a neon geek?

Just a few more drinks...

r ke ac op r ec Dr Fir on m to r Le Moji ixe tM r olutinge tail s Ab e S ock uiri Be ’s C Daq ea pid rry e T Cu be d Ic oke raw lan C St g Is and e n n ic Lo ptai mi V a a C Mi xcar w Bo Meo ini t’s art Ca le M tan t in p at Ap anh er M M inn lph D do ch e t r Ru Bea f A ed e e R t th re ya nT a D Melo

u at The drink menou e has L Head First ng 40 cocktails.

Rum Run ner Blue Dog Key Wes t Lemon Neapolita ade n Polo Coc ktail Purple Y umm Neon Ge y ek Flaming Nerd Letter Bo mb Bookma ker’s Luc k Baked A pple Deer Hu nter Mexican Bomb Aftersho ck Black Ey ed Susan Beetle J uice Termina tor Gingerb read Man Lost in S p Music Cit ace y Sunset Cafe Joy Sandbar Sleeper

Get ready to start typing... 160   Chapter 4

multiple views

This sucks. Can’t we just import the list Sam sent us somehow?

We could, but not the way we’re set up now. Since the drinks are populated with an array that’s hardcoded into the implementation file, we can’t import anything. What would work well is a standardized way to read and import data; then we would be able to quickly get that drink list loaded.

There must be a better way to handle this. How can we speed up getting 40 drinks in our list?

you are here 4  161

put your data in a plist

Plists are an easy way to save and load data Plist stands for “property list” and has been around for quite a while with OS X. In fact, there are a number of plists already in use in your application. We’ve already worked with the most important plist, DrinkMixer-Info.plist. This is created by Xcode when you first create your project, and besides the app icons, it stores things like the main nib file to load when the application starts, the application version, and more. Xcode can create and edit these plists like any other file. Click on DrinkMixer-Info.plist to take a look at what’s inside.

ems Some of these eitthe are obvious, likthe main icon file and nib to load.

, Others are less obviousou t ab but we’ll talk more rs. te them in later chap

162   Chapter 4

multiple views

Built-in types can save and load from plists automatically All of the built-in types we’ve been using, like NSArray and NSString, can be loaded or saved from plists automatically. They do this through the NSCoding protocol. We can take advantage of this and move our drink list out of our source code and into a plist.

-

t our drink list ou We need to movcoe de here and into a of the source plist instead...

(void)viewDidLoad {



[super viewDidLoad];

drinks_ = [[NSMutableArray alloc] initWithObjects:@”Firecracker”, @”Lemon Drop”, @”Mojito”, nil];

}

RootViewController.m

Before you import Sam’s list, let’s create a sample plist that’s the same format. We’ll make sure we get that working properly, and then pull in Sam’s list. 1

2

Create the empty plist. Go back into Xcode and expand the Supporting Files folder. Rightclick on Resources and select New file→Mac OS X Resource, and Property List. Call the new list DrinkArray.plist.

ck “Resource” Make sure you piX—plists aren’t under Mac OS S Resources. listed under iO

Format and populate the plist. Open up the file using the Open As→Source Code and change the to and save. Then right-click and use the Open As→ASCII Property List option. Right-click inside the plist editor and click Add Row. You will need three string items. Then you can populate the names for the drinks.

Drink List: Firecracker Lemon Drop Mojito

you are here 4  163

exercise solution

With the sample list created, we can use it for testing before we get the big list. 1

Create the empty plist. Go back into Xcode and expand the Supporting Files folder. Right-click on Resources and select New file→Mac OS X Resource, and Property List. Call the new list DrinkArray.plist.

Plists are used in Mac development as well as iOS development, so they’re listed here. 2

Click Next and name .your plist DrinkArray.plist

Format and populate the plist. Open up the file using the Open As→Source Code and change the to and save. Then right-click and use the Open As→ASCII Property List option. Right-click inside the plist editor and click Add Row. You will need three string items. Then you can populate the names for the drinks.

When you open it as the ASCII property list...

This is the plist as source. Change this tag to array.

164   Chapter 4

... right-click in the empty plist and select “Add Row.” Then add the 3 drinks, all strings.

multiple views

Arrays (and more) have built-in support for plists Changing the array initialization code to use the plist is remarkably easy. Most Cocoa collection types like NSArray and NSDictionary have built-in support for serializing to and from a plist. As long as you’re using built-in types (like other collections, NSStrings, etc.), you can just ask an array to initialize itself from a plist. The only piece missing is telling the array which plist to use. To do that, we’ll use the project’s resource bundle, which acts as a handle to applicationspecific information and files. Add the bolded code below to your RootViewController.m file.

-(void)viewDidLoad {

[super viewDidLoad];

Ask the app bundle fo path to our DrinkArrarya pli

st.

Initialize the array using the contents of the plist.

NSString *path = [[NSBundle mainBundle] pathForResource:@”DrinkArray” ofType:@”plist”];





drinks_ = [[NSMutableArray alloc] initWithContentsOfFile:path]; }

RootViewController.m

Test Drive After you’ve finished up these two things, go ahead and build and run. It should look the same, with just the three drinks.

Ready Bake plist

Once this list works, head over to http://www.headfirstlabs.com/ books/hfiphonedev/ and download the DrinkArray.plist file. It has the complete list of the drinks from the Head First Lounge. Drop this in on top of your test plist, rebuild DrinkMixer, and try it out!

you are here 4  165

a full drink list

Test Drive

The whole list is in there now!

PLists work great fo built-in types. If you’rre going to be using custo types, you will need to m write some serializat code. Check out NSCoiondi for more information. ng

166   Chapter 4

By moving the drinks ouant of the code and inton external file, you ca change the drink list uch a without needing to to line of code.

y to save PLists are just one wa lk data on the iOS­—we’llthtae book. about others later in

multiple views

Now we just need to get that detail view all set up, right?

Creating your detail view will complete the app. The entire list of drinks is great, but Sam still needs to know what goes in those views and how to make them. That information is going to go in the detail view that we sketched up earlier.

How are we going to get from the list to the detail view? And how are we going to display the details?

you are here 4  167

a familiar pattern

Use a detail view to drill down into data Earlier, we classified DrinkMixer as a productivity app and we chose a navigation controller because we have hierarchical data. We have a great big list of drinks loaded, but what Sam needs now is the detailed information for each drink: what are the ingredients, how do you mix them, etc. Now we’ll use that navigation controller to display a more detailed view of a drink from the list. The standard pattern for table views is that you show more information about an item when a user taps on a table cell. We’ll use that to let the user select a drink and then show our detailed view. The detail view follows the same MVC pattern as our other views.

ller The table view’s contro r) lle ro nt Co (our RootView h uc will get the to the information. It will tell the ow sh to r nav controlle detailed view.

When the user ta we’ll display the depstaon a drink, il view. Touch here. View Controller

Detail View

Since the detail view only cares about the specific drink it’s the showing details for, on datasource will focus one drink.

Datasource

Detail View Controller

168   Chapter 4

all The detail view shows ke up ma at th s the element d an s nt die re ing a drink: the how to mix them.

views, the r e h t o r u o Just like w will have a view detail vier. This one will be controlle for filling in the responsiblew. detail vie

multiple views

A closer look at the detail view We sketched out the detail view earlier—but let's look more closely at what we’re about to build.

omes with c n o t t u b The backcontroller. the nav

It will be populated with “Name:” and the drink info, so we don’t need a label. A couple o the bottomf labels for two fields

Back button

UITextField for the drink name

w for UITextViedients the ingre

or UITextView nfs the directio

Let’s start building... you are here 4  169

create your detail view

You’ve got the hang of this now. Start building your detail view by creating the files and code you’ll need, then put it together in Interface Builder and wire it up. Get to it! 1

Create the files you’ll need. To create the new view, you need a new *.xib file, as well as the supporting header and implementation files. The file type is a Cocoa Touch Class type, and it’s a UIViewController subclass. Back button

2

Lay out the new view in Xcode. Use the object library to drag and drop the elements that you need and build the view we sketched out earlier. Hint: to reserve the space for the navigation controller, just bring up the Utilities Panel and choose the Attributes Inspector. Under Simulated Metrics, Top Bar, select Navigation Controller. That will make sure that you lay out your view below the navigation bar.

170   Chapter 4

3

Write the code to handle the declarations and outlets for the new properties. You’ll need to work in both DetailViewController.h and DetailViewController.m. Call the new properties nameTextField, ingredientsTextView, and directionsTextView. Don’t forget to create the instance variables then synthesize and release everything.

4

Connect the detail view to the new outlets. Just like we did for InstaEmail, use Interface Builder to link the controls to the properties.

5

Make the text fields uneditable. Using the inspector, find the checkbox that makes the fields uneditable.

multiple views

Q:

We keep drawing the datasource, view, and View Controller as separate things, but then we stick the datasource and controller together into the same class. What’s going on?

A:

It’s all about the pattern. In general, you’ll have a view defined in a xib, a View Controller backing it, and a set of data it needs to work on. Whether these are combined into one class or not really depends on the complexity of your application. If you’re not using Interface Builder, you can go completely off the deep end and have your single class create the view programmatically. We’ll show more of that later in the book. Conceptually, however, you still have a view that’s calling into the View Controller when things happen. Likewise, you usually have one or more datasource protocols being realized somewhere that are providing data to your view.

Q:

Why do we have to move the *.xib file into the Resources group?

A:

You don’t have to, but we recommend it to help keep your code organized. Different

developers use different groups, things like “User Interface,” “Business Objects,” “Data Objects,” etc. Xcode really doesn’t care; it’s just important that you know how your code is organized and you can find what you’re looking for. Reusing a structure that others will recognize is a good practice so people can pick up your code quickly and you can understand their code. We use the templated defaults in this book.

Q:

What are other ways to save and load data?

A:

There are quite a few of them. We’ll cover the more common ones in this book in different projects. The one you’re using now, plists, is the simplest, but it does limit what you can save and load. That doesn’t make it bad; if it works for what you need, it’s a fine solution—it’s just too limited for everything. There’s a serialization protocol called NSCoding that works well for custom objects, but can make version migration a challenge. iOS supports saving and loading to a database using SQLite. This used to be the preferred way to go if you had a lot of data or needed to search and access it without loading it all into memory. However, with the iPhone 3.0 SDK (now just iOS), Apple introduced Core Data. Core Data is

a very powerful framework that provides an OO wrapper on persistence and has nearly all the benefits of using SQLite. It’s definitely not trivial to get started, but it’s really powerful. We’ll build an app on it later.

Q:

Why didn’t you use a label for the name field?

A:

UITextFields allow you to have placeholder text that appears in the field when it’s empty. Rather than using up screen space with a Name label, we chose to use the placeholder. If the meaning of the text shown on the screen is obvious to the user, consider using placeholder text.

Q:

So why didn’t we use it for the ingredients and directions?

A:

We could have, but since those contain multiple lines of text, we wanted to break them up with labels clearly showing what they were. Ultimately it’s an aesthetic and usability decision, not a technical one.

ƒƒ Productivity apps work great with hierarchical data.

ƒƒ iOS tables only have one column but can render custom cells.

ƒƒ Navigation controllers are a good way to manage multiple views.

ƒƒ Tables need a datasource and a delegate.

ƒƒ Table views usually go with navigation controllers.

ƒƒ Multiple views usually mean multiple *.xib files.

you are here 4  171

long exercise solution

Here’s all the info for the new detail view. After this, you should have a working (but still empty) detail view.

1

Create the files you’ll need. We need a new .xib for the detail view. To create one from scratch, go back into Xcode and click on the File→New→New File menu option.

Make sure that you the Cocoa Touch Clashas ve selected under iOS. line

Select the UIViewContrller subclass.

After clicking Next, you can confirm the subclass of UIViewController. In our case, we need both the nib and the supporting files, so leave the "With XIB for user interface" box checked and click Next.

One more thing. Xcode will create all your files in the DrinkMixer group, keeping them with the other class files. Name the file “DrinkDetailViewController.m.”

172   Chapter 4

multiple views

2

Lay out the new view in Xcode.

Controller from This came by choosing NavigatiBaonr, option in the the Simulated Metrics -Toputilities panel. Attributes Inspector in the This is the same UITextField that we used in InstaEmail. It doesn’t scroll.

tor This is the inspTecextField. for the first as a Put “Name:” in placeholder.

This is a UITextView like we used in InstaEmail. It doesn't scroll.

you are here 4  173

long exercise solution

Here’s all the info for the new detail view. After this, you should have a working (but still empty) detail view. 3

Write the code to handle the declarations and outlets for the new fields.

#import

@interface DrinkDetailViewController : UIViewController { @private

UITextField *nameTextField_;



UITextView *directionsTextView_;



UITextView *ingredientsTextView_;

}

@property (nonatomic, retain) IBOutlet UITextField *nameTextField;

@property (nonatomic, retain) IBOutlet UITextView *ingredientsTextView; @property (nonatomic, retain) IBOutlet UITextView *directionsTextView; @end

DrinkDetailViewController.h

@implementation DrinkDetailViewController

@synthesize nameTextField=nameTextField_, ingredientsTextView=ingre dientsTextView_, directionsTextView=directionsTextView_;

-(void)dealloc {

[nameTextField_ release];



[directionsTextView_ release];



}

[ingredientsTextView_ release]; [super dealloc];

DrinkDetailViewController.m

174   Chapter 4

multiple views

4

Connect the detail view to the new outlets. All three outlets—the directionsTextView, the ingredientsTextView, and the nameTextField—need to be connected to their control on the new view.

Use the drag and drop from the fields into k.the code to make the lin

le's You can right-click onnisFihed fi Owner when you're . to see all the outlets

you are here 4  175

almost there...

5

Make the text fields un-editable. We need to disable both the UITextField and the two UITextViews to prevent the user from making changes. Simply click on each field and toggle the Enabled or Editable checkboxes to off.

Uncheck this to freeze thews. contents of the UITextVie

Once those changes are made, the keyboard issue goes away, because there won’t be one!

Test Drive Build and run your app. You just put in a lot of work, and it’s a good time to check for errors. You won't see a difference yet, just the drinks list again...

176   Chapter 4

multiple views OK, so I have an order for a Boxcar... but I still don’t see the drink details when I click on it.

Touch here

We still need to get that detail view to load when Sam selects a drink.

When your users browse through the drink information, they’re going to need to switch between the list and detail views. Take a few minutes to think about how to do that while keeping the user from getting lost. 1

How does the user navigate between views?

2

How can we keep track of what view to show?

3

How does the detail view know what drink to show?

4

How do you get the user back to the table view?

you are here 4  177

keeping track

When your users browse through the drink information, they’re going to need to switch between our list and detail views. Think about how we do that and keep the user from getting lost.

In the simulator, Xcode will generate a back button with the text that says “Drink Mixer”.

1

How does the user navigate between views?

The user is going to tap on the cell of the drink name

that they want to see. 2

How can we keep track of what view to show?

The Navigation Controller will keep track with back

buttons and the title of the pane. 3

How does the detail view know what drink to show?

That’s based on the table cell that

the user selects. 4

How do you get the user back to the table view?

button that can get us back to the main view. 178   Chapter 4

The Navigation Controller can supply a back

multiple views

Use the Navigation Controller to switch bet ween views Now that we’ve got the table view populated and the detail view built, it’s time to manage moving between the two views. The navigationbased template comes preloaded with the functionality we need: 1

A view stack for moving between views As users move back and forth, you can ask the Navigation Controller to display the appropriate view. The Navigation Controller keeps track of where the users are and gives them buttons to go back.

2

A navigation bar for buttons and a title The Navigation Controller interacts with the navigation bar to display buttons that interact with the view being shown, along with a title to help the users know where they are.

3

A navigation toolbar for view-specific buttons The Navigation Controller can display a toolbar at the bottom of the screen that shows custom buttons for its current view. Table View

Nav Controller

The UINavigationController supports a delegate, called the UINavigationControllerDelegate, that gets told when the controller is about to switch views, but for DrinkMixer, we won’t need this information. Since the views get told when they’re shown and hidden, that’s all we need for our app.

Detail View

Now we need to get the Table View and Nav Controller working together to display the detail view. you are here 4  179

stacked views

Navigation Controllers maintain a stack of View Controllers We’ve been dragging the Navigation Controller along since the beginning of this project, and now we finally get to put it to use. The Navigation Controller maintains a stack of views and displays the one on top. It will also automatically provide a back button, as well as the cool slide-in and-out animations. We’re going to talk more about the whole Navigation Controller stack in the next chapter, but for now, we’re just going to push our new view onto the stack and let the controller take care of the rest. We just need to figure out how to get that new view.

Drink Table View

Add Drink View

Once the new view is created, we’ll use the Navigation Controller to push the view onto the screen.

When a row is IndexPath: indextapped, tableview:didSelectRow Path is sent to t A he delegate. t The table passes cellForRowAtInd an NSIndexPath (just like a knows which row exPath) to the delegate so it was selected.

View Controller

od is When the delegate meCothntroller called, our RootView to create and (the delegate) needs Controller. push the detail View

Here's where things get interesting: our RootViewController is our delegate, so it needs to hand off control to a new View Controller to push the detail view onto the screen. How do you think we should handle that?

180   Chapter 4

multiple views

Instantiate a View Controller like any other class The only piece left to create is the View Controller for the detail view. Instantiating a View Controller is no different than instantiating any other class, with the exception that you can pass in the nib file it should load its view from: [[DrinkDetailViewController alloc] initWithNibName:@”DrinkDetailView Controller” bundle:nil];

Once we’ve created the detail View Controller, we’ll ask the NavigationController to push the new View Controller onto the view stack. Let’s put all of this together by implementing the callback in the delegate and creating the new View Controller to push onto the stack:

#import “RootViewController.h”

#import “DrinkDetailViewController.h”

#pragma mark -

#pragma mark Table view delegate

te Since we’re going to crlleear, we the new View Contro ader. need to include its he

eady You have this method ealrexisting th started, just edit code. Here’s the delegate callback—the indexPath tells us which row (drink) was selected.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath { controller...

Instantiate the

DrinkDetailViewController *detailViewController = [[DrinkDetailViewController alloc] initWithNibName:@”DrinkDetailViewControll er” bundle:nil]; // ...

// Pass the selected object to the new view controller.

[self.navigationController pushViewController:detailViewController animated:YES]; onto the



[DrinkDetailViewController release];

}

ion Controller Now that the Navigatlle we can has the detail contro tor,it. release our reference

...then push it navigation stack.

RootViewController.m

Let’s try this out... you are here 4  181

the nav controller in action

Test Drive

Now that both views can talk to each other, go ahead and build and run.

Tap here to make the detail view come up.

Try clicking in the text fields—there's no keyboard because they’re not editable! 182   Chapter 4

multiple views

So, now we can get to the detail view from the drink list, but there aren’t any details in there. We don’t have that info in our plist, do we?

Yep, we’ve outgrown our array. All that’s left is to get the ingredients and directions in the detail view, and we’ll have a bartender’s brain. To save you from having to type in the ingredients and directions, we put together a new file with all the extra information. The problem is we can’t just jam that information into an array. To add the drink details to this version, we need a different data model.

Which options below are possible ways to load the drink data? Create a database with drink information

Use dictionaries in our plist to hold the drink details

Use an XML file to hold the drink details

Create multiple arrays in our plist

Which of these options is the best for DrinkMixer? Why?

you are here 4  183

dictionaries store key-value pairs

Which options below are possible ways to load the drink data?

since re drink information, butot do sto to se ba ta da a e us ve We could es the database, we’d ha nothing else in this appsuuspport added...let’s keep looking. some work to get DB

We already ha a plist of string a plist of dictve s—switchin io na us a data struct ries won’t be much work ang dover to ure that can hold gi the drink info.ves

Create a database with drink information

Use dictionaries in our plist to hold the drink details

Use an XML file to hold the drink details

Create multiple arrays in our plist

This would work too, but DB. We’re not parsing an has the same hurdle as using a to define the schema, thy XML right now, so we’d have en add parsing code.

the options— This is basically the worst of all ays line arr le ltip we’d have to make sure mu aight. str up to keep a single drink

Since we already have code written that uses plists, we can change our plist to have an array of dictionaries instead of an array of strings without a lot of effort. This way we don’t have to introduce SQL or XML into our project. However, we do lose out on the strong typing and data checking that both SQL and XML could give us. Since this is a smaller project, we’re going to go with dictionaries. Which of these options is the best for DrinkMixer? Why?

Dictionaries store information as key-value pairs Our current drink plist is just a single array of drink names. That worked great for populating the table view with just drink names, but doesn’t help us at all with drink details. For this plist, instead of an array of strings, we created an array of dictionaries. Within each dictionary are three keys: name, ingredients, and directions. Each of these have string values with the corresponding information. Since NSDictionary adopts the NSCoding protocol, it can be saved and loaded in plists just like our basic array from before.

184   Chapter 4

multiple views

Q: A:

You keep talking about NSCoding. What is that?

NSCoding is a protocol that provides an API for encoding and decoding objects. A lot of the basic container types like NSArray NSDictionary conform to this protocol; that’s why we can serialize them in and out of a plist. You can conform to this protocol on your own custom objects as well. For more information about NSCoding, see the Apple documentation.

Q:

Where did the back button in the detail view come from? We didn’t do that...

A:

It’s automatic functionality that comes with the Navigation Controller. When you added a title for the main view, the Navigation Controller kept track of that name as part of the view stack for navigation, and added a back button with the title in it. So yeah, you did do that!

Ready Bake Code Go back to http://www.headfirstlabs.com/books/hfiphonedev/

and download DrinksDirections.plist. It has a different name, so you’ll need to make a couple of quick modifications.

1

 pen up the new plist in Xcode (again, in the resources directory), and O look at what it comes with—all that data is ready to go!

2

 o into the code and change the references from DrinkArray to G DrinksDirections.

Test Drive Build and run the application, but you should pay close attention. (Umm, not that we think anything bad is going to happen, just, well, because...)

you are here 4  185

uh oh...

Test Drive

It crashed!

186   Chapter 4

multiple views

They'll also show in the corner of up the editor window .

Debugging—the dark side of iOS development Something has gone wrong, but honestly, this is a pretty normal part of the development process. There are lots of things that could cause our application to crash, so we need to figure out what the problem is.

Warnings can help find problems first

you'll see Here's whanting and an for a war error.

In general, if your application doesn’t build, Xcode won’t launch it—but that’s not true for warnings. Xcode will happily compile and run an application with warnings and your only indication will be a little yellow yield signs in the gutter of the Xcode editor. Two minutes spent investigating a warning can save hours of debugging time later.

They will appear in the gutter next to the offending code.

Geek Bits Some common warning culprits: Now that iOS 4.3 is out, code that uses deprecated 2.0 or 3.0 properties triggers warnings.  ending a message to an object that it doesn’t claim S to understand (from a typo or an autocompletion error) will trigger warnings. Your app will compile, but will likely end up in a runtime exception when that code is executed.

That’s not our problem, though: at this point our code is (at least it should be) warning and compile-error-free. The good news is that when an app crashes in the Simulator, it doesn’t go away completely (like it would on a real device). Xcode stops the app right before the OS would normally shut it down. Let’s use that to see what’s going on.

Time for some debugging... you are here 4  187

start with the console

First stop on your debugging adventure: the console We need to figure out why our app crashed, and thankfully, Xcode has a lot of strong debugging capabilities. For now we’re just going to look at the information it gave us about the crash, but later in the book, we’ll talk about some of the more advanced debugging features. Since you ran the program in the simulator, Xcode is going to bring up the debugging pane at the bottom of the editor. You'll probably want to resize it to make the log easier to review.

The console down becausetells us that our app w and what th of an uncaught exce as shut ption, at exception was.

ck trace of The console also gives us a sta re’s a much but where our application was, upthe second... a in better view of that coming 188   Chapter 4

The console ha what happened stthe information about to be shut down. hat caused our application happened, though. It doesn’t tell us why it

multiple views

Interact with your application while it’s running Xcode 4 is a very powerful debugging tool. Some of the best debugging techniques involve well-placed logging messages using NSLog(...). This information is printed into the console and can help you diagnose problems quickly. The console isn’t just read-only, though; it is your window into your running application. We’ll see log messages displayed in the console, and when your application hits a breakpoint, you’ll be placed at the console prompt. From there you can use debugging commands like print, continue, where, up, and down to inspect the state of your application.

ly the The console debugger is actual so t, mp pro gdb (GNU debugger) here. rk wo nds nearly all gdb comma

And when it’s about to stop running In this case, we’re dealing with a nearly dead application, but the idea is the same. Since DrinkMixer has crashed, Xcode provides you with the basic information of what went wrong. In our case, an “unrecognized selector” was sent to an object. Remember that a selector is basically a method call—it means that some code is trying to invoke methods on an object and those methods don’t exist.

To see this output, you'll need to select "Debugger Output" from this dropdown box.

The console interact wit prompt lets you the commandh your application at line.

But Xcode doesn’t stop at the command line. It has a full GUI debugger built right in. Let’s take a look... you are here 4  189

breaking it down

Xcode supports you after your app breaks, too So far we’ve used Xcode to write code and compile and launch our applications. Its usefulness doesn’t stop once we hit the “Build and Debug” button. First, we can set breakpoints in our code to let us keep an eye on what’s going on. Simply click in the gutter next to the line where you want to set a breakpoint. Xcode will put a small blue arrow next to the line and when your application gets to that line of code, it will stop and let you poke around using the console.

This switch indicates whether the breakpoints are on or not.

To set a breakpoint, just click here.

insert Step Once your app hits a breakpoint, Xcode will to let Into, Step Over, Continue, and Debugger buttons you walk through your code.

Slide the scrubber all the way to the right to see the full stack from the app... 190   Chapter 4

multiple views

The Xcode debugger shows you the state of your application The debugger shows your code and also adds a stack view and a window to inspect variables and memory. When you click on a stack frame, Xcode will show you the line of code associated with that frame and set up the corresponding local variables. There isn’t anything in the debugger window you couldn’t do with the console, but this provides a nice GUI on top of it.

Here’s the from your astack current breapp at the (or crash...). kpoint click on a fr If you Xcode will sh ame, the correspo ow you nding code.

d Here are the Step anlet to s on tt bu e Continu de. you walk through your co

Xcode shows you your app’s variables (local, global, etc.) in this pane.

Test Drive Since we know that we’re having a problem near the array, try setting a breakpoint on the line that creates the array. Then build and run the app again and see what happens.

you are here 4  191

loading the array isn’t the problem

Test Drive When you step over the breakpoint at the point where you load the array, everything is OK:

To set a breakpoint, you’ll need to click in the gutter, here.

But hit continue and...

192   Chapter 4

multiple views

Here’ excepts our uncaug unreco ion for the ht again... gnized sele ctor

What the heck is going on? Our application is crashing, and it’s not at the array loading code, so get back into Xcode. It will show you the line that’s causing the problem, can you see what’s wrong?

To be continued... you are here 4  193

multipleviews cross

MultipleViews cross

Untitled Puzzle

Header Info 1 Header Info 2 etc...

Take what you’ve learned about the Navigation Controller and multiple views to fill in the blanks.

1 2 3

4

5 6 7 8

9

10

Across

Down

3. The set of views that the nav controller deals with. 6. Dictionaries use __________ to organize data. 8. The screen that gives you output from the app. 9. A template that combines a table view and nav controls. 10. Has cells that need to be customized to work.

1. A more versatile way to manage data beyond an array. 2. DrinkMixer is this type of app. 4. To use a new class you need to ___________ it. 5. The @ symbol is shorthand for creating one of these. 7. A tool in Xcode to help fix broken code.

194   Chapter 4

Your iOS Toolbox You’ve got Chapter 4 under your belt and now you’ve added multiple views and the Navigation Controller to your toolbox.

Tables:

Are a collection of cells. Come with support for editing contents, scrolling, and moving rows. Can be customized so your cells look like more than one column.

Plists:

Files that can be created and edited in Xcode. Support Arrays and Dictionaries out of the box. Are good for handling data, but have some limitations—we’ll cover another option, Core Data, in a couple chapters coming up.

late: p m e T n o i t Naviga iew and

a table v Comes withcontrol built in. navigation ivity app. t c u d o r p a r l Is great fo hierarchica e g a n a m o t Is designed ultiple views. m data and o s built in t n io t a im n a Has cool en views. move betwe

: w e i V e l b a T I U by only

emory Controls m cells requested e creating th . Any other cells in the view yed if iOS needs are destro y for something the memor else.

Xcode:

Navigation Controller:

Maintains a view stack for movin g between view controllers. Has a navigation bar for button s and a title. Can support custom toolbars at the bottom of the view as neede d.

Has a built-in console with debugging and logging information. Gives you errors and warnings as you compile to identify problems. Has a built-in debugger that allows you to set breakpoints and step through the code to find the bug. you are here 4  195

CHAPTER 4

multiple views

multipleviews cross solution

MultipleViews cross Solution Untitled Puzzle

Take what you’ve learned about the navigation controller and Info 1 Header multiple views to fill in the blanks. Header Info 2

etc...

1 2

P

I 3

R 5 6

K

E

D

V

4

I

E

W S

T

A

C

N

O

N

T

S

D

S

I

S

U

T

O

Y

R

C

A

I

T

N

9

N G

A

V

I V I

G

A

T

8

I

O

C

O

N

I

N

K

7

S

O

T Y

E

T

E

A

B

R

U

Y

G

A 10

L

D

G A

B

L

E

V

I

E

W

E R

Across

Down

3. The set of views that the nav controller deals with. [VIEWSTACK] 6. Dictionaries use __________ to organize data. [KEYS] 8. The screen that gives you output from the app. [CONSOLE] 9. A template that combines a table view and nav controls. [NAVIGATION] 10. Has cells that need to be customized to work. [TABLEVIEW]

1. A more versatile way to manage data beyond an array. [DICTIONARY] 2. DrinkMixer is this type of app. [PRODUCTIVITY] 4. To use a new class you need to ___________ it. [INSTANTIATE] 5. The @ symbol is shorthand for creating one of these. [NSSRING] 7. A tool in Xcode to help fix broken code. [DEBUGGER]

196   Chapter 4

5 plists and modal views

Refining your app This soup would be even better with the perfect cocktail, maybe a Neon Geek...

So you have this almost-working app... That’s the story of every app! You get some functionality working, decide to add something else, need to do some refactoring, and respond to some feedback from the App Store. Developing an app isn’t always ever a linear process, but there’s a lot to be learned along the way.

this is a new chapter   197

debugging DrinkMixer

It all started with Sam... Sam wanted an app to make his bartending work easier. You got one up and rolling pretty quick, but hit a snag filling in the details for each drink because of a plist of dictionaries. drink ow a ton of I need to kn ere an th y night. Is recipes ever ? app for that

DrinkMixer has two views: a table view of the drinks and a detail view about each individual drink.

DrinkMixer

Sam, bartender at the HF Lounge

When we last left DrinkMixer, you were in the middle of debugging it... 198   Chapter 5





plists and modal views

Anatomy of a Crash DrinkMixer started and ran happily until it hit our breakpoint at line 19. The debugger stopped our application and displayed the debugging console. By setting a breakpoint in our code, what we discovered at the end of Chapter 4 is that before your app imported the file, there was no crash; so far so good. Let’s walk through loading our plist and make sure that works by typing next twice. The first “next” sets up the path to the plist, the second one actually loads the data.

Here’s where it stopped at the breakpoint. We told the debugger to let DrinkMixer execute the next two lines.

Here’s the Continue button.

It made it past loading the plist, so let’s let it continue running...

is being sent to an This exception tells you that an unknown selector (message) coming from? it is where ...so tring NSCFDictionary—specifically, isEqualToS Loading the plist worked fine; no problems there. The error must be coming after that. Let’s have the application continue running and see where it fails. Hit the Continue button (or type continue in the console)...and there’s our exception again. Where is this actually failing?

you are here 4  199

CSI iPhone

Use the debugger to investigate the crash We can reliably get DrinkMixer to crash, and it doesn’t seem to be our plist loading code. Xcode has suspended our application right before iOS shuts it down, so we can use the debugger to see exactly what it was trying to do before it crashed. Switch back to the debugger and take a look at the stack on the left. This is the call stack that led to the crash.

The stop button will terminate your application.

Here’s the stack at the time of the crash. The top 13 frames are framework code, but frame 6 is code we wrote...

Trying to continue now will just keep failing— DrinkMixer has been stopped by iOS.

And here’s the line that caused the problem. See what’s going on yet? 200   Chapter 5

plists and modal views

So the exception talks about NSCF Dictionary...maybe it has something to do with the new drinks directions we uploaded for the detail view?

You’re on to something there. Let’s take a closer look.

Using what you’ve learned so far, figure out what’s going on!

The exception talks about NSCF Dictionary. What dictionary is it talking about? Where is it coming from?

Who’s sending messages to the dictionary? Why did we get an unrecognized selector?

you are here 4  201

square peg, round hole

Using what you’ve learned so far, figure out what’s going on!

The exception talked about NSCF Dictionary. What dictionary is it talking about? Where is it coming from?

The dictionaries are coming from the plist! When we load the plist, we now have an array of dictionaries instead of an array of strings.

Who’s sending messages to the dictionary? Why did we get an unrecognized selector?

Messages are being sent to the dictionary when we try to set the cell’s label text. It’s actually the label sending it a message (see the next stack frame, it’s code in UILabel). It’s sending messages as though the cell label text were a string. But now we’re assigning a dictionary to the label text! /

}

// Configure the cell. cell.textLabel.text = [self.drinks objectAtIndex:indexPath.row]; return cell;

RootViewController.m

We’re trying to stuff a dictionary into a string Putting a dictionary into the text field of the label, which wants a string, isn’t going to work. Our previous array was an array of strings, so that code worked fine. Now that we have an array of dictionaries, we need to figure out how to get the drink name value (a string) out of the dictionary, and then assign that to the text label. If you take another look at the DrinksDirections.plist, you’ll see that we have an array of dictionaries— one for each drink. As we saw earlier, dictionaries store their values using keys; they’re just a collection of key-value pairs. To get a value out, you simply send the dictionary the objectForKey:@"key" message.

Instead of assigning the array value right to the text label, you’ll need to pull out the name value from the appropriate dictionary. somelabel.text

202   Chapter 5



Dictionary

name = Cupid’s Cocktail ingredients = Cherry liqueur, peach ... directions = Shake ingredients and strain into...

we use the For each drinkr, the name of key name fo gredients the drink, in s, and so on. for ingredient

plists and modal views

Update your code to handle a plist of dictionaries Armed with the knowledge of how the dictionaries are put together, we can use this information to populate the detail view, too. If you give the detail view controller the dictionary of the selected drink, it can populate the view’s fields before the view is shown to the user.

Detail View

Datasource

Each dictionary has everything we need for drink. We need to get tha dictionary to the dataso at urce for the detail view.

View Controller

Go ahead and make the changes below to your app. After this, it should know that you’re using an array of dictionaries, not strings—and the detail view should have a reference to the drink it should display.

1

Change the way a table cell is configured. In RootViewController.m, fix the cell’s textLabel.text property to use the name value from the appropriate dictionary.

Don’t forget about the NSDictionary documentation if you want to know more about dictionaries. 2

Add a reference to a drink dictionary in the detail view. In DrinkDetailViewController.h, add an NSDictionary* instance variable named drink_ and the corresponding property declaration.

3

Add drink to the DrinkDetailViewController.m file. Synthesize and dealloc the new dictionary reference.

detail view the We’ll update the the values in e controller to usin a minute... new dictionary you are here 4  203

updating for dictionaries

Go through the code and make sure that you’ve got everything right...

// Configure the cell.

cell.textLabel.text = [[self.drinks objectAtIndex:indexPath.row]



objectForKey:@”name”]; return cell;

the Use objectForKey to gearty. ion ct di name from the

RootViewController.m @private

UITextField *nameTextField_;



UITextView *directionsTextView_;

UITextView *ingredientsTextView_;



NSDictionary *drink_;



}

y* Declare the NSDictionaarproperty instance variable and mic, retain with the usual nonato attributes.

@property (nonatomic, retain) IBOutlet UITextField *nameTextField;

@property (nonatomic, retain) IBOutlet UITextView *ingredientsTextView; @property (nonatomic, retain) IBOutlet UITextView *directionsTextView;

@property (nonatomic, retain) NSDictionary *drink;

This shouldn’t be an outlet since we won’t be setting it through Interface Builder.

DrinkDetailViewController.h

Add drink to the synthesize line.

@synthesize drink=drink_, nameTextField=nameTextField_, ingredientsText View=ingredientsTextView_, directionsTextView=directionsTextView_;



- (void)dealloc {





}

[nameTextField_ release];

[ingredientsTextView_ release];

our dictionary reference here.

[directionsTextView_ release]; Release

[drink_ release]; [super dealloc];

@end DrinkDetailViewController.m

204   Chapter 5

plists and modal views

Test Drive Now that we’ve told DrinkMixer how to deal with dictionaries, go ahead and build and run. Make sure that the breakpoints are off!

It’s working again! Now that it’s not crashing, it’s time to fill in the details. you are here 4  205

filling in the drink details

The Detail View needs data Now that you’ve figured out how to deal with dictionaries, it’s time to fill in the drink details. But getting the details out of the array of dictionaries to give to the datasource requires another step.

Touch here Nav Controller

This is the information in DrinksDirections.plist.

Datasource

The datasource needs the dictionary information for the selected drink...

Detail View Controller

Remember this? This is the structure of the app.

How are we going to get the information from DrinksDirections.plist into the Detail View?

206   Chapter 5

plists and modal views

The other keys are key Right now we’re just pulling the name of each drink into the app using the name key. In order to populate the ingredients and directions, we need to use the other keys. You could just type those right into our code, but you’re a better developer than that, so let’s pull them up into constants. The only thing left is getting the proper dictionary to the detail view controller so it can pull the information it needs. Go ahead and start setting everything up!

The View Controller needs direct access to the datasource, and the easiest way to get to that data is going to mean some quick code refactoring. 1

Organize your dictionary constants to avoid bugs. Since we’re going to need the name, ingredients, and directions keys in the View Controller, we should clean up the code to start using real constants. Create a new file called DrinkConstants.h (right-click on the DrinkMixer folder, then choose New File, and then choose Other and an empty file). Add constants (#define’s) for NAME_KEY, INGREDIENTS_KEY and DIRECTIONS_KEY. Import DrinkConstants.h into DrinkDetailViewController.m and RootViewController.m. Finally, update where we use the @"name" key to the new constant, NAME_KEY.

2

Set the detail View Controller’s drink property. In the root View Controller, after you instantiate the detail View Controller when a cell is tapped, you need to set the drink property on the new controller to the selected drink.

3

Add code to the detail View Controller to populate values on the UI controls. Before the Detail View appears, the View Controller should use the drink dictionary to set the contents of the name, ingredients, and directions components. Don’t forget to use the constants you just set up!

you are here 4  207

cleaning up with constants

Here’s all the added code to make the Detail View work.

1

Create DrinkConstants.h.

DrinkDetailViewController.m and RootViewController.m both need #import "DrinkConstants.h". We’re Then add the constant to display the name: // Configure the

changing the dictionary keys to constants here...

cell.

cell.textLabel.text = [[self.drinks objectAtIndex:indexPath.row] objectForKey:NAME_KEY]; return cell;

Change this value from @“name”.

RootViewController.m

208   Chapter 5

plists and modal views

Set the detail view controller’s drink property.

2

#pragma mark -

#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndex Path *)indexPath { DrinkDetailViewController *detailViewController = [[DrinkDetailViewController alloc] initWithNibName:@”DrinkDetailViewContro ller” bundle:nil]; detailViewController.drink = [self.drinks objectAtIndex:indexPath. row];

[self.navigationController pushViewController:detailViewController animated:YES];

[detailViewController release];



}

Add this whole line to ab a dictionary from the argr ray.

RootViewController.m

Add a method to the detail View Controller to populate the fields.

3

This whole method

is new.

-(void) viewWillAppear:(BOOL)animated {

egate We need to make sure we delolle r ntr Co iew this up to the UIV else. ng thi any do superclass before we



[super viewWillAppear:animated];



self.nameTextField.text = [self.drink objectForKey:NAME_KEY];



// Set up our UI with the provided drink

self.ingredientsTextView.text = [self.drink objectForKey:INGREDIENTS_KEY]; self.directionsTextView.text = [self.drink objectForKey:DIRECTIONS_KEY];

And here are those constants you just created.

}

DrinkDetailViewController.m

you are here 4  209

so that’s what’s in a cupid’s cocktail!

Test Drive Compile and build and run again...

It works!

Q:

We re-create the Detail View every time someone taps on a drink. Couldn’t I just reuse that view?

A:

For DrinkMixer, it really won’t matter too much; since the view is pretty lightweight, we won’t suffer too much overhead recreating it when a drink is tapped. However, for best performance, you can refactor it to reuse the same detail View Controller and just change the drink it should be showing when a row is tapped.

Q:

Why did we have to pull out the dictionary key names into a separate file?

210   Chapter 5

A:

Having magic string values in your code is generally a bad idea—no matter what programming language or platform you’re using. By pulling them up into constants using #define, they are checked by the compiler. So a typo like @”nme” instead of @”name” would end up as a bug at runtime, while mistyping NME_KEY instead of NAME_KEY would prevent things from even compiling.

Q:

I looked at the NSDictionary documentation and there’s a valueForKey: and an objectForKey:. What’s the difference?

A:

Great question. valueForKey: is used for what’s called key value coding, which is a specific pattern typically used in Cocoa Binding. The subtle catch is that NSDictionary usually just turns a call to valueForKey: into a call to objectForKey, and it looks like either one will work. However, valueForKey actually checks the key you pass it and has different behavior depending on your key. That’s almost never what you want (unless you’re doing Cocoa binding stuff, of course). The correct method to use is objectForKey:.

plists and modal views

Is that app up on the App Store? Then I can just download it on my phone and start making even more tips!

Sam, ready for your app to make his (and your) wallet fatter...

Looks like there’s a market there! A quick submission to Apple and... you are here 4  211

a modern-day dear john letter

s Store From: iTune APPROVED T O N p p a r kMixe Subject: Drin ution on the ib tr is d r fo D e E T APPROV man Interfac u O H N ’s is S p p iO a r to You . The table not confor m w s e ie o v d le t I b . ta e r e f th App Sto mentation o . le p im r u o y tor elements a ic d in Guide in e r u s t using disclo ce Guide fa r te views are no n I n a m u r m to the H fo n o c lementation, t p o n im o r d u t o a y th g s in App ted. After fix u ib tr is d e b t may no approval. r fo p p a r u o resubmit y

Time to investigate the HIG...

Seriously, this can an if you don’t follow thde will happen happened to, um, a frienHIG. It d of the authors...twice. We’ll go through the approval process later. Later in the book, we’ll take you through the process of preparing an app for approval. For now, just worry about how to fix DrinkMixer!

212   Chapter 5

plists and modal views

We have a usability problem We know that the user needs to touch the name of the drink to see the details about each individual drink, but how is the user supposed to know that? The HIG has a number of recommendations for how to deal with drill-down, hierarchical data. We’re already on the right track using table views, but the HIG has a number of additional recommendations for helping the user understand how to navigate the app.

Table cells have a number of built-in usability items that help users understand how to use your app—even if it’s the first time they’ve run it. Touch here

Here is the root view that users see, the table view. We’re alrea Navigation Cdy using the back button ontroller’s user know ho to help the back to whe w to get re they cam from... e

When the user taps, the View Controller hands off control to the detailed view.

It’s time to dive into the HIG and figure out what went wrong.

Take a look at disclosure indicator elements—when should we be using these?

The HIG mentions detail disclosure buttons and disclosure indicators—which should we use? Why?

you are here 4  213

disclose your intentions

It’s time to dive into the HIG and figure out what went wrong.

Take a look at disclosure indicator elements—when should we be using these?

In the HIG, the “Table View” section, you can pretty quickly find out why you’re in violation over those disclosure indicators: “The disclosure indicator element...is necessary if you’re using the table to present hierarchical information.”

The HIG mentions detail disclosure buttons and disclosure indicators—which should we use? Why?

The disclosure indicator denotes that there is an additional level of information available about an item when you click it (like drink details); it selects that row and shows the additional data. The button can do something besides select the row—it can kick off an action as well. That’s more than we’ll need here, so we’ll just stick with the disclosure indicator..

Table Cells Up Close So, what exactly is the detail disclosure button, and where does it go? Let’s look a little deeper in the HIG.

w is This imageVoiew images used to sh with a cell. associated

bel is the The textLaarea in a cell. main text

Big Font Info

small detailed text

This is the detailTextLabel. Depending on what cell style you use, it can show up in different places, fonts, and colors.

Here’s an ype—common accessoryTisclosure ones are d detail indicator, button, and disclosure . checkmark

DrinkMixer uses default cells, but you can easily customize your cells for a different app, besides just adding detail disclosure buttons. There are a few other types of cells in the API: subtitle cell style, a value 1 style (where the first value of the cell is emphasized), or a value 2 cell (where the second value of the style is emphasized). Even though the table only supports one column, you can make it look like more by adding a thumbnail, for example. You can also adjust the font sizes to open up some room for each table cell if you need to. Most really polished apps use some kind of table cell customizing, so keep that in mind while you’re looking through the API. For now, we just need to add the disclosure button to our cells to indicate that there’s more information available if a user taps on them.

214   Chapter 5

plists and modal views

Use a disclosure button to show that there are more details available TableViewCells have a lot of built-in functionality—we’re just scratching the surface. Adding a detail disclosure indicator is simply a matter of telling the cell what type of accessory icon it should use. Take a look at the UITableViewCell documentation for some of the other options.

Here’s the constant you need.

There’s just one quick line of code to set the cell’s accessory type when we configure the cell: // Configure the cell.



Just set the accessory type to the detail disclosure indicator.

cell.textLabel.text = [[self.drinks objectAtIndex:indexPath.row] valueForKey:NAME_KEY];

cell.accessoryType = UITableViewCellAccessoryDetailDisclosureIndicator;

return cell; }

Test Drive

RootViewController.m

Go ahead and build and run...make sure it’s working! you are here 4  215

ready to resubmit to the App Store

Test Drive One little line of code fixed all of your App Store approval issues.

There are thoseators— disclosure indic ows now the user kn what to do!

216   Chapter 5

plists and modal views

This app is great! I’m going to use it every night.

After resubmitting to the App Store, DrinkMixer is up on iTunes!

er - Week 1 Sales report - DrinkMix oads Overall sales - 400 downl Price - $1.99 Overall revenue - $796

Wow, just for one week!

hat Apple Remember tpercentage will take a of this.

The reviews are coming in... you are here 4  217

meanwhile, back in the App Store

Sales were going strong But then bad reviews started coming in. What’s going on?

The reviews are bad...

They say things like “DrinkMixer sucks–I can’t add anything”

...and sales are tanking! Another review: “I need more than 40 drinks.” “I don’t like any of the drinks on the list.”

218   Chapter 5

“My bar has some custom drinks and I don’t want to keep a separate sheet of drinks around.” y run on m ’t n s e o d “It id Phone.” Andro

“I’m going to switch to iDrink— it’s more expensive, but it lets me add new drinks and customize my list.”

plists and modal views

Think about how you originally designed DrinkMixer, what’s not working based on the feedback, and figure out what you’ll do next.

1

What would address the users’ concerns?

2

Given the structure of DrinkMixer, how would you refactor the code to fix the problem?

3

Is there an easy way to fix the code? A hard way?

you are here 4  219

give the people what they want

Think about how you originally designed DrinkMixer, what’s not working based on the feedback, and figure out what you’ll do next.

1

What would address the users’ concerns?

The easiest way to fix the problem is to update the app so users can add more drinks to the list. 2

Given the structure of DrinkMixer, how would you refactor the code to fix the problem?

We could add a new view that lets users enter their drink information. It could look like the detail view, but allow them to type in the information they want. We’ll have to save that new information and update the table to show the new drink. 3

Is there an easy way to fix the code? A hard way?

There are lots of hard ways and probably a few good “easy” ways. In general, the easiest way for us to add this functionality is to reuse as much of what we’ve already done as possible. We can definitely take advantage of our Navigation Controller, and let’s see if we can’t do something useful with our DetailDrinkView too...

How would you go about implementing a view where users can add drinks to DrinkMixer?

220   Chapter 5

plists and modal views

App Layout Construction Here is the table view for DrinkMixer with two possible designs. Based on aesthetics, usability, and standard iOS App behavior, which one is better for showing the users where they should add a drink?

Some kind of button in the Navigation Controller to kick off a new view.

Add a new toolbar with some buttons below the Nav Controller.

You’d have room for an add button and others, when you need them.

Option #1

Option #2

Which interface is better? Why? (Be specific.)

Why not the other?

you are here 4  221

built-in buttons

App Layout Construction solution Here are two designs. Based on aesthetics, usability, and standard iOS App behavior, which one is better for showing the users where they should add a drink?

The Navigation Controller comes with built-in button support.

This type of interface is good when you have several new views to add, not just one.

The toolbar will cover up part of the table view, too.

Option #1

Which interface is better? Why? (Be specific.)

Option #2

Option #1.

Because by putting the icon in the Nav Controller, you don’t take space away from

the table view. There’s also built-in support for that button in the Nav Controller already. Why not the other?

Option #2 makes the interface a bit more cluttered and requires more code. If

there was lots of new functionality coming, it would be worth considering, though. 222   Chapter 5

plists and modal views

Use Navigation Controller buttons to add drinks

Users will be able to tap the + button to add a drink.

So far we’ve used the Navigation Controller to move between views. But if you’ve spent much time with other iOS apps, you know it’s capable of much more. Since a UITableView is almost always embedded in a Navigation Controller, table editing is usually done through buttons on the controller itself. Let’s start out by adding a + button to the Navigation Controller that will let the users add a drink when they tap it.

Using Xcode, add the button to the Nav Controller and the associated IBActions and IBOutlets.

1

Open RootViewController.xib in the GUI editor. Scroll through the library and drag a Bar Button Item to the Main Window (this will add it to the list after the Table View). It won’t show up on the Navigation Controller in the editor—we’ll need to add code so it shows up at runtime.

2

Add the instance variable and property declaration for addButton and IBAction for addButtonPressed. Just like any other button, we’ll have an IBAction for when it gets clicked and a reference to the button itself—all in RootViewController.h.

3

Add the synthesize, dealloc, and addButtonPressed method for addButton. Synthesize the property, release the reference, and implement the addButtonPressed to log a message when the button is clicked—all in RootViewController.m

4

Finish up in the links. Open up RootViewController.xib again and link the new Bar Button Item to the actions and outlets within the Main Window.

It won’t show up because the Navigation Controller in er Interface BuildD, is SIMULATE not real.

Finally, pull up the inspector in the Utilities pane for the Bar Button Item and change the Identifier to Add.

you are here 4  223

add the button

Using Xcode, add the button to the Nav Controller and the associated IBActions and IBOutlets.

1

Open RootViewController.xib in the GUI editor. Scroll through the library and drag a Bar Button Item to the Main Window (it will get added to the list).

2

Add the instance variable and property declaration for addButton and IBAction for addButtonPressed.

@interface RootViewController : UITableViewController {





}

NSMutableArray *drinks_;

UIBarButtonItem *addButton_;

@property (nonatomic, retain) NSMutableArray *drinks;

@property (nonatomic, retain) IBOutlet UIBarButtonItem *addButton; - (IBAction) addButtonPressed: (id) sender;

@end

3

Add the synthesize, dealloc, and addButtonPressed method for addButton.

RootViewController.h

@synthesize drinks=drinks_, addButton=addButton_;

#pragma mark-

#pragma mark Actions

- (IBAction) addButtonPressed: (id)sender {

}

NSLog(@”Add button pressed!”);

224   Chapter 5

RootViewController.m

We’ll start just by logging the button press so we can test it before implementing anything further...

plists and modal views

Put this inside of ViewDidLoad, it’s a tweak of the code that’s stubbed out.

self.navigationItem.rightBarButtonItem = self.addButton;

- (void)dealloc {

}

4

[drinks release];

[addButton_ release];

[super dealloc];

RootViewController.m

Finish up in the view. Open up RootViewController.xib again, and link the new Bar Button Item to the actions and outlets within the Main Window, right-clicking and using the menus that pop up. Finally, pull up the inspector for the Bar Button Item and change the Identifier to Add.

you are here 4  225

button success

Test Drive Go ahead; build and run the app...

The button works! Now you get an affirmative message in the console...

The button shows up in the view, but now what? 226   Chapter 5

plists and modal views

The button should create a new view Our new button works: the action gets called, but really doesn’t do anything useful yet. We need to give our user a place to enter the new drink information, and we can do that with a new view. Just like with the detailed view, we can let the Navigation Controller handle the transition.

What do we ne for the AddViewControed lle r’ Where does it go s UI? ?

needs to Pushing the “plus” buttneonw view where move the user into a information. they can enter drink Back and forth tab

AddDrink ViewController

List of drinks

?

Root ViewController

Back button

Touching still movesthe disclosure indic the detail the user into ator particular view about that drink.

Name: Name of the drik Ingredients: These are the ingredients. Like grenadine, vodka, etc.

DetailDrink ViewController

What do we need for the user to be able to enter a new drink? Exactly what fields do you need and how will you lay them out? How will the View Controller work?

Directions: These are the directions, mix, pour over ice, then layer the rest.

We pulled the view e view information out of th e controller and into throller nib. DetailDrinkViewCont

you are here 4  227

reuse your view

We need a view...but not necessarily a new view Our “new” drink view is really just an editable version of our detailed view. So instead of creating a whole new nib, let’s take advantage of the fact that the UI (the nib) is separate from our behavior (the UIView subclass in the .m file) and reuse the detail view. Up until now, we’ve had a one-to-one pairing between our nibs and our View Controllers. That’s definitely the norm, but our View Controllers are really just normal Objective-C classes. That means we can use object-oriented extension mechanisms like inheritance to add the behavior we want.

We need to support different behavior than the detail View Controller, though. We’ll need a new view controller.

Back button Name: Ingredients:

s The add drink view neeedsame to contain exactly thview—it fields as the detail ble. just needs to be edita

Directions:

When you click on these text fields, the keyboard will pop up and let you enter new information.

AddDrink ViewController DrinkDetailViewController.xib

Really, a new view controller but not a new nib? I thought they always go together.

Not necessarily. Remember that a xib is just the XML representation of a view. Using nibs is a lot easier than trying to lay out your view using code. And since the nib is just graphical information, you need to put the actual code somewhere else. That’s where the view controller comes in...

228   Chapter 5

plists and modal views

The View Controller defines the behavior for the view From the user’s perspective, we’ll have three views: the table view, the detailed view, and the new drink view. But, since we’re reusing the .xib to create the “new” view, all we need is a new view controller class that supports adding a drink. That means there isn’t any Interface Builder work to do at all!

e When we instantiate th ller, we tell ro nt Co iew DrinkDetailV cific nib. it to initialize with a spe

Back button

d view we neee w e n e h t ’s h t Here It will look to createw. e can reuse the nib. same, so

is We need th

now...

Name: Name of the drik Ingredients:

Back button Name: Ingredients:

These are the ingredients. Like grenadine, vodka, etc.

Directions:

Directions: These are the directions, mix, pour over ice, then layer the rest.

DrinkDetailViewController.xib DrinkDetailViewController.m

fines the The ViewController de in this case, behavior for the view— drink populating the fields with information.

I and The nib defines the GU the loo since both views will k same, we can reuse it.

AddDrinkViewController.m

Separating the UI from behavior helps you reuse your view.

Reusing both the .xib file and the detail view controller is also an option. But where could we run into problems with that approach?

you are here 4  229

keeping track of outlets and actions

A nib file contains the UI components and connections... One way we could reuse the nib is to create a new ViewController and pass it the DrinkDetailViewController.xib file when we initialize it. There are a few challenges with that, though. Remember, we don’t just use Interface Builder to lay out the interface; we use it to wire up the components to the class that will load the nib.

e fields troller has thed up to n o C w ie V e h T hat get wir and methods tts in the nib. the componen

d information eannib... ut o y la ew vi e h h T stored in t connections are

DetailDrinkViewController IBOutlet UITextField *nameTextField; ... - (void) viewWillAppear: (BOOL) animated; ...

...and information about the nib’s File’s Owner The nib doesn’t actually contain the ViewController class it’s setup to be wired to. Instead, it does this through the nib’s File’s Owner. When you pass the nib to the view controller, it will deserialize the nib and begin making connections to the outlet names stored in the nib file. This means that if we want to pass that nib into another, new view controller, we need to make sure we have the same outlets with the same names, the same actions, etc.



Reusing our nib gets us what we need for this app, but it’s not for every app out there.

Because of the way DrinkMixer is built, we can just subclass our detailed view to get what we need. That works great for this app, but be careful doing this in more complex apps, because your code can get difficult to maintain. Often, it’s better to just bite the bullet and build a new view...and sometimes you’ll realize they shouldn’t even look the same.

230   Chapter 5

plists and modal views

You can subclass and extend view controllers like any other class Instead of reusing just the nib and having to recreate all the outlets and actions, we can just subclass the DetailedViewController and add the behavior we need. Our AddDrinkViewController is the same as a DetailedViewController; it just has the ability to create and save an entirely new drink. Everything else— showing the name, showing the description, etc.—are all exactly the same as the DetailedViewController.

DetailDrinkViewController ive-C ct je b O in s ld fie By default, ed can get to IBOutlet UITextField *nameTextField; e w so , t ec t ... are pro bclass. them in our su - (void) viewWillAppear: (BOOL) animated;

er kViewControsll(yet) Our AddDrianny ld new fie won’t need ill inherit the because it wViewController’s DetailDrink fields...

...

Back button Name: Ingredients:

Directions:

AddDrinkViewController - (void) viewWillAppear: (BOOL) animated; ...

...but we will need to chan little behavior, so we’ll ne ge a override a couple of methed to ods.

When we create an Controller, AddDrinkDetailViewlass, the it will ask its superc roller, to load DrinkDetailViewContController.xib. the DrinkDetailView

First, we need to create the new View Controller. you are here 4  231

when to reuse

Q:

I still don’t get it about the new view controller without a new nib.

A:

There’s nothing in that nib that you couldn’t create in normal Objective-C by hand. As you’ve likely discovered with Interface Builder, nibs are generally a lot easier to work with than trying to lay out your view using code, so when you create a new view, you typically create a nib to go with it. But really, you could build an entire application without a single nib. In our case, we’re going to do something somewhere in the middle: we’re going to create a new view but reuse the UI information from another view.

Q:

So why the “Watch it” warning about reusing the nib? Is this a good idea or not?

232   Chapter 5

A:

Unfortunately, the answer is: it depends. For DrinkMixer, we can reuse our DetailDrinkView and its nib since we want the layouts to look the same and the DetailDrinkView doesn’t really do anything specific. However, in a more complex application, you might run into problems where you’re constantly fighting between the two view controllers or you have to expose so much information to the subclass that your code becomes unmaintainable. This isn’t a problem unique to iOS development; you always have to be careful when you start subclassing things.

For our app, subclassing works fine, and you’ll see it in some of Apple’s example applications, too (which is part of the reason we included it here). But it’s equally likely that in some other application you’ll want views to be similar, but not quite exactly the same. In those cases, create a new view controller and nib.

Q:

Do you usually have all your table cell initialization code in cellForRowAtIndexPath?

A:

It depends on how complex your table view and cells are. If your cell configuration is simple, sure. As you get more complicated table views (in particular, grouped table views with different kinds of cells), it gets unwieldy. Instead, consider creating helper methods that can configure a particular kind of cell.

Another really important thing to keep straight is resetting a cell back to defaults. Your cellForRowAtIndexPath should always reset a cell back to default values before using it—you just don’t know where it’s been. Obviously, you can factor that code out into a helper method too, but don’t forget to do it!

plists and modal views

Use Xcode to create a View Controller without a nib What we’ll do is create a new ViewController in Xcode that doesn’t have its own nib, and then tweak it to inherit from the DrinkDetailViewController. This new view will get all of the fields, behavior (which we’ll change), and the nib we need.

Get into Xcode and create the AddDrinkViewController files.

Create a new UIViewController subclass named AddDrinkViewController without a nib using the New File dialog box.

Watch the options in the new file creation...You don’t want a xib with the view.

Open up the new AddDrinkViewController.h file and change it to inherit from DrinkDetailViewController instead of the UIViewController. Don’t forget to import the DrinkDetailViewController.h file.

new behavior We’ll add the minute... we need in a

you are here 4  233

inherit the drink detail view controller

Get into Xcode and create the AddDrinkViewController files.

In the New File dialog box, you need to create new UIViewController subclass files. Be sure to uncheck the With XIB for user interface box, since we don’t need that .xib file.

In order to use the DrinkDetailViewController, we need to import the header so the compiler knows what we’re talking about.

#import

#import “DrinkDetailViewController.h”

@interface AddDrinkViewController : DrinkDetailViewController{ }

@end

By default, our View Controller inherited from UIViewController. Change that to DrinkDetailViewController here.

The AddDrinkViewController.m file can stay exactly as it is generated by Xcode.

Q:

Wait, why aren’t we just passing the nib into the AddDrinkViewController? Why all this subclassing stuff?

A:

We could do that, but the problem is we’re not just dealing with GUI layout. We have text fields and labels in there that need to get populated. Our DetailedDrinkViewController already has outlets for all the fields we need, plus it has the functionality to populate them with a drink before it’s shown. We’d have to reimplement that in our new view controller if we didn’t subclass.

234   Chapter 5

Q:

Is this some kind of contrived Head First example, or should I really be paying attention?

A:

You should be paying attention. This pattern shows up pretty often and a lot of Apple’s example applications use it. It’s very common, particularly in table-driven applications, to have one view that just displays the data and another to edit it when the user puts the table in editing mode (we’ll talk about that more later). Sometimes you should use totally different views; sometimes you can reuse one you have.

AddDrinkViewController.h

Q:

You mentioned that fields are protected by default. What if I wanted private fields in my class?

A:

It’s easy—just put @private (or @public for public fields) in your interface definition before you declare the fields. If you don’t put an access specifier there, Objective-C defaults to protected for fields.

plists and modal views

Jim: Now we have an AddDrinkViewController class, so all we have to do is push it on the stack like we did with the detail view, right?

Joe

Frank

Joe: That makes sense—we used the Navigation Controller to drill down into the data just by pushing a detailed view on the stack... Frank: Adding a new drink to our list is a little different, though. Jim: Why? Frank: Well, adding a new drink is really a sub-task. Joe: Huh? Frank: The users are stepping out of the usual browsing drinks workflow to create a new drink. Joe: Oh, that’s true. Now they’re typing, not reading and mixing a drink.

Jim

Frank: Right, so for times like this, it’s important to communicate to the users that they have to complete the task. Either by finishing the steps or— Joe: —or by cancelling. Frank: So, what kind of view is that?

Which of these views better communicates what the user needs to do? Is one more ambiguous than the other?

you are here 4  235

modal views are animated

Modal views focus the user on the task at hand... When users navigate through your app, they are used to seeing views pushed and popped as they move through the data. However, some tasks are different than the normal drilldown navigation and we really need to call the user’s attention to what’s going on. iOS does this through modal views. These are normal views from your (the developer’s) perspective, but feel different to the user in a few ways:

The modal view going to cover upis navigation contro the l... When you pus using pushViewh a view onto the stack navigation con Controller:animated:, the from the side troller slides the view in it) and creates(if you said to animate button in the a Back navigation nav bar.

he viewthe full t , w e i v modal m and covers. Users a y a l p o ou dism the bott vigation bavriew before y n e h W in fro he na odal ls ides —including t the new m application. screento deal withue with the have can contin they

... like adding or editing items We’re going to use a modal view when users want to add a new drink to DrinkMixer. They have to either save the added drink or discard (cancel) it before they can return to the main DrinkMixer app. So we want a view that encourages the user to make that choice.

236   Chapter 5

Modal views ha either by savingve to be dismissed, cancelling out of the changes or the view.

plists and modal views

Any view can present a modal view Up until now, we’ve presented new views using our Navigation Controller. Things are a little different for modal views: any UIViewController can show a modal view, then dismiss it when necessary. To display a modal view on top of the current view, simply send the current view the presentModalView Controller:animated: message. Since our RootViewController is the View Controller that needs to show the modal view, we can just send this message to ourselves, using self, like this: [self presentModalViewController:addViewController animated:YES];

rd self is the Objective-C keywotly ren cur is t tha for the object to executing the method. It’s similar “this” in Java or C++.

ller This is the View Controa modal as you want displayed new view, in our case, the ller. AddDrinkViewContro

n If you say NO to animated, the ing say By the view just appears. YES, we get the smooth slide in from the bottom.

Update the RootViewController.m file to display our AddDrinkViewController when the + button is tapped.

Import the AddDrinkViewController.h so the RootViewController knows what class you’re talking about.

Change the addButtonPressed:sender: method to create an AddDrinkViewController, and present it as a modal view. Be careful about your memory management—don’t leak references to the controllers.

you are here 4  237

create your modal view

Update the RootViewController.m file to display our AddDrinkViewController in a UINavigationController when the + button is tapped.

#import “RootViewController.h”

y here—just sa o t h c u m t No ile. import the f

#import “DrinkConstants.h”

#import “DrinkDetailViewController.h”

#import “AddDrinkViewController.h”

RootViewController.m

#pragma mark -

#pragma mark Actions

- (IBAction) addButtonPressed: (id) sender



NSLog(@”Add button pressed!”);

e Controller just like th iew kV rin dD Ad e th lass. e at su Alloc ller—remember, it’s a bc ro nt Co iew kV rin dD ile ta { De It uses the same nib, too.

AddDrinkViewController *addViewController = [[AddDrinkViewController alloc] initWithNibName:@”DrinkDetailViewController” bundle:nil];





}

[self presentModalViewController:addViewController animated:YES]; [addViewController release];

Now we just need to show the mo dal view—since RootViewController is a ViewCont just call presentModalViewContr roller, we oller and iOS handles the rest.

The RootViewController will retain a reference View Controller when we present it. Don’t forg to the new et to release the reference to the View Controller!

238   Chapter 5

RootViewController.m

plists and modal views

Test Drive

Now that the add view is fully implemented, build and run the project. Make sure you try out all the functionality: scrolling, drilling down to details, and finally adding a drink. You’ll see there’s still a little work left to be done...

Try clicking around, between fields.



If your keyboard isn’t working, your fields might still not be editable.

Back in Chapter 4, we had you make the fields uneditable in the utilities panel. If your keyboard isn’t appearing, try going back into Interface Builder and checking that the fields are now editable.

Touch in the title to bring up the keyboard and make sure it works.

But what about after you finish typing? you are here 4  239

sam can’t add his drink

That’s great, but after I type in the drink, nothing happens! I can’t get the view to go away, and I can’t add the drink.

That’s a problem. Actually, it’s two problems that are related. Earlier, we decided that the add drink detail view needs to go away one of two ways: either the user cancels out or saves the drink. We need to get those buttons in there.

240   Chapter 5

plists and modal views

App Layout Construction How should we lay out the Save and Cancel buttons? Is there anything unique about this view that we have to deal with?

Save Cancel

Your app will look like this after you’ve entered your drink (and you can’t do that now...)

you are here 4  241

we need a navigation bar

Our modal view doesn’t have a navigation bar To be consistent with the rest of DrinkMixer, we really should put the save and cancel buttons at the top of the view in a navigation bar. The problem is, we don’t have one in our modal version of the detail view. Drink detail view

A modal view covers the navigation control.

Add drink detail view

The detail view is pushed on top of the table view, preserving the Nav Controller.

We could add one by hand, but remember we’re sharing the detail drink view nib, which gets its navigation bar from the navigation controller. Since we’re showing the add drink view as a modal view, we cover up the navigation bar. Instead of trying to solve this from within the detail drink view nib, we can embed our add drink view in a Navigation Controller of its own, like this:

Instead of presenting our add wController, we can present an addNavControlVie ler View Controller.

This will add a Nav Controller to wrap the add drink view.

UINavigationController *addNavController = [[UINavigationController alloc] initWithRootViewController:addViewController];

242   Chapter 5

plists and modal views

Do this! Allocate the UINavigationController - (IBAction) addButtonPressed: (id) sender { and pass in our addViewController as its root view controller. It will retain NSLog(@”Add button pressed!”); the controller, since it needs to display it. AddDrinkViewController *addViewController = [[AddDrinkViewController alloc] initWithNibName: @”DrinkDetailViewController” bundle:nil];



UINavigationController *addNavController = [[UINavigationController alloc] initWithRootViewController:addViewController];

[self presentModalViewController:addNavController animated:YES];



[addNavController release];



}

[addViewController release];

Now we just need to show the mo dal view— since RootViewController is a Vie wC we just call presentModalViewCo ontroller, ntroller, and iOS handles the rest.

Don’t forget to release ref nce s to the AddDrinkViewController anderethe NavigationController.

RootViewController.m

v There’s our na

controller.

Now we just need to create those buttons... you are here 4  243

creating ui controls in code

Create the Save and Cancel buttons Since both the Save and Cancel buttons need to dismiss the modal view, let’s start by wiring them up to do that. We’ll need some actions, and the buttons themselves. We’ve covered how to do that in Interface Builder—and you can do that now if you want, but this time, let’s write them in code so see how that approach works.

- (IBAction) save: (id) sender;

- (IBAction) cancel: (id) sender;

These go just before the @end.

Since we’re using the navigation bar, we get built-in support for left and right-hand buttons. We just need to create those buttons and assign them to our leftBarButtonItem and rightBarButtonItem to place them where we want them.

the Add a viewDidLoad toller.m file. ro nt Co AddDrinkView

an add button, we’re Just like when we madeation Controller’s left going to use the Navigr Save and Cancel. and right buttons fo

AddDrinkViewController.h

Here are our o buttons in codtw e.

- (void)viewDidLoad {



[super viewDidLoad];

self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)] autorelease];

self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save:)] autorelease];

Notice our “autorelease” her nor mally, we alloc a class, assign it to to go, then release our referee— nce to it. By autoreleasing when we cre where it needs Objective-C to handle releasing ate it, we ask handling it ourselves, but a littleit for us later. Not quite as efficient as explicitly cleaner looking in the code. 244   Chapter 5

AddDrinkViewController.m

plists and modal views

Write the Save and Cancel actions When the user clicks either Save or Cancel, we need to exit the modal view by asking the View Controller that presented the view to dismiss it. However, to make things easier, we can send the modal view the dismiss message, and it will automatically forward the message to its parent View Controller. Since the AddDrinkViewController is the modal view and gets the button call back, we can just send ourselves the dismiss message and the controller stack will handle it correctly. We need to send ourselves the dismissModalViewControllerAnimated: message, like this: [self dismissModalViewControllerAnimated:YES];

Cancel methods Implement the Save ancadlled, then clear the to log which one was le actually saving a modal view. We’ll tackworks. new drink once this

Start this at the bottom of the file.

#pragma mark -

#pragma mark Save and Cancel



}



}

- (IBAction) save: (id) sender { NSLog(@”Save pressed!”);

[self dismissModalViewControllerAnimated:YES]; - (IBAction) cancel: (id) sender { NSLog(@”Cancel pressed!”);

Since we are in the modal view, this dismiss message will be delegated up to our parent View Controller, which will actually make the view go away.

[self dismissModalViewControllerAnimated:YES];

AddDrinkViewController.m

Now, to see if those buttons work... you are here 4  245

your modal view works

Test Drive The modal view can be dismissed now, and the keyboard works, too!

Just like that, the buttons are in the detail view.



Congratulations, the modal view is working!

You did a lot of view manipulation in this chapter and have a solid foundation of an application to show for it. You can transition to and from detail views and jump into modal views when you need the user to complete a specific task. Next, we just need to tackle saving the new drink.

246   Chapter 5

plists and modal views

Q:

Why don’t we need an outlet for the Save/Cancel button? And what about Interface Builder?

A:

We don’t need to do anything with the Save and Cancel buttons after we’ve created them (like disabling one, or swapping one out for something else briefly), so we create them and immediately hand them off to our navigationItem to put in the upper left and upper right. If we needed to do anything with them later, we’d probably want to hang onto a reference, and we could use an instance variable for that.

As for Interface Builder, we created the buttons in code this time. Depending on what you’re trying to do with a control, sometimes it’s easier to just create them (or set sizes or configure controls, etc.) in code. For many developers, it’s just personal preference. In our case, we only needed to create simple buttons, so we did that in code. We didn’t need any IBActions because when creating the buttons programmatically, you can pass in a selector (using the @selector(...) syntax) that should be called. That’s how we hooked the buttons up to our Save and Cancel methods.

So can I add some new drinks yet? I just learned how to make this cool new one from another bartender and want to put it in my app.

To be continued...

you are here 4  247

iOSdev cross

iOSDev cross

Untitled Puzzle

Header Info 1 Header Info 2 etc...

Using all the stuff you’ve learned about how to work with different plists and views, fill in the puzzle.

1

2 3

5

4

6

7 8

9 10

Across

Down

1. User _______________ on itunes stick with the app even after a new version is released. 3. The navigation controller has support for ___________ buttons to fix stuff. 5. A ____________ view has to be dealt with by the user before doing anything else. 6. Use these to organize names of things. 7. You can create ________________ bars in IB or in code. 8. A nib file has UI ___________________. 9. The HIG requires some kind of _____________ element in a cell if there is more information availible. 10. ______________ is easier when the UI is separated from the behavior.

2. Views can be ____________ and extended like any other class. 4. An _______________ specifies what a button should look like.

248   Chapter 5

plists and modal views

You’ve got Chapter 5 under your belt and now you’ve added plists and modal views to your toolbox. For a complete list of tooltips in the book, go to http://www .headfirstlabs.com/books/hfiphonedev.

Debugging

If you know where your problem is likely to be, set the breakpoint there. You can use the debugger to step through the problem area. If your application does crash, pay close attention to the call stack. Your code frames are in black; framework code (code without source) is in grey.

s Dictionarieto expand the

ways Are useful f a plist contents o s of data ir a p e lu a v Store key eys rings for k t S S N e s u Typically require it. but don’t

AppStore Basics

1. Submitting your app to store means it HAS TO COthe NFORM TO THE HIG. 2. Approvals can take ti and get it right with thme, so try e first submission. 3. Once your app is up fo the reviews stay with it r sale, , even with updates.

Views

Are typically used in a view stack with a Navigation Controller or presented modally. Can be subclassed and extended like any other class. Modal views help communicate to a user that they have to complete a distinct task or abandon it entirely.

you are here 4  249

CHAPTER 5

Your iOS Toolbox

iOSdev cross solution

iOSDev cross Solution Untitled Puzzle

1

R

E

V

I

E

Header Info 1 Header Info 2 etc...

2

W S 3

U

E

D

B M O

D

A

L

O M P

O

N

E

N

T 9

C

O

N

S

T

A

N

N

A

V

I

G

A

T

I

S

F

E

I

D

I

T

S

O

N

E

U

N

G

S

E

T 7

S C

T

E 6

A 8

I D

C 5

4

I

S

C

L

O

S

U

R

E 10

R

Across 1. User _______________ on itunes stick with the app even after a new version is released. [REVIEWS] 3. The navigation controller has support for ___________ buttons to fix stuff. [EDITING] 5. A ____________ view has to be dealt with by the user before doing anything else. [MODAL] 6. Use these to organize names of things. [CONSTANTS] 7. You can create ________________ bars in IB or in code. [NAVIGATION] 8. A nib file has UI ___________________. [COMPONENTS] 9. The HIG requires some kind of _____________ element in a cell if there is more information availible. [DISCLOSURE] 10. ______________ is easier when the UI is separated from the behavior. [REUSE] 250   Chapter 5

Down 2. Views can be ____________ and extended like any other class. [SUBCLASSED] 4. An _______________ specifies what a button should look like. [IDENTIFIER]

6 saving, editing, and sorting data

Everyone’s an editor... If these records were on an iPhone and I could edit them, life would be grand!

Displaying data is nice, but adding and editing information is what makes an app really hum. DrinkMixer is great—it uses some cell customization, and works with plist dictionaries to display data. It’s a handy reference application, and you’ve got a good start on adding new drinks. Now it’s time to give the user the ability to modify the data—saving, editing, and sorting—to make it more useful for everyone. In this chapter, we’ll take a look at editing patterns in iOS apps and how to guide users with the Nav Controller.

this is a new chapter   251

sam’s new drink

Sam is ready to add a A new drink at the Lounge Red-Headed School Girl... Sam went to try DrinkMixer with the new add view and ran into problems right away.

Sam was clicking around, ready to add his new drink. The directions field is hidden under the keyboard.

You can’t see the directions at all, and part of the ingredients information is covered up.

We have a problem with our view, since we can’t get to some of the fields.

252   Chapter 6

Sam, the bartender

saving, editing, and sorting data

...but the keyboard is in the way We’re back to the keyboard problem we saw earlier with InstaEmail. When Sam taps on a control, it gets focus (becomes the first responder) and asks iOS to show the keyboard. Generally, that’s a good thing. However...

When S Drink naam taps in the keyboard me field, the supposed appears like it to—that ’s ’s good.

And th completee keyboard Directio ly covers the ns field!

We had a similar problem in InstaEmail where the user couldn’t get to the controls under the keyboard. p ry to tafield t n e v e He can e Ingredients he into th pe in some of t ns and ty nts...but he ru ingredie he keyboard. under t

How did we deal with the keyboard last time? Will that work this time? What do you want the view to do when the keyboard appears?

you are here 4  253

scroll view up close

How did we deal with the keyboard last time? Will that work this time? What do you want the view to do when the keyboard appears?

Resigning first responder worked last time. In DrinkMixer, it would be fine for the name field, but what about the directions and the ingredients fields? As soon as the keyboard comes up, they’re covered. The user has a smaller screen to work with once the keyboard shows up—we need to set up the view to scroll things in when the user needs them. We can do this with a UIScrollView.

UIScrollView Up Close UIScrollView is just like the basic UIView we’ve been using except that it can handle having items (like buttons, text fields, etc.) be off the screen and then scrolling them into view. The scroll view draws and manages a scroll bar, panning and zooming, and controlling which part of the content view is displayed. It does all of this by knowing how big the area it needs to show is (called the contentSize) and how much space it has to show it in (the frame). UIScrollView can figure out everything else from there.

Content view

The conte to be just nbtu doesn’t have text fields; ttons and work well witUIScrollViews h images, too .

Controls (buttons, etc.) a The scroll view acts like econtent th on magnifying lens ion is view, so that only a port . er visible to the us

UIScrollView ha support for zooms built-in panning around t ing and view—you just ne he content how big the conted to tell it ent is. Remember, in CocoaTouch, components are subclasses of UIView. All a scroll view needs to care about are the subviews it has to manage. It doesn’t matter if it’s one huge UIImageView that shows a big image you can pan around, or if it’s lots of text fields, buttons, and labels. To get a scrollable view, we need to move our components into a UIScrollView instead of a UIView. Time to get back into Interface Builder...

254   Chapter 6

saving, editing, and sorting data

Wrap your content in a scroll view We want the user to be able to scroll through our controls when the keyboard covers some of them up. In order to do that, we need to add a UIScrollView to our view and then tell it about the controls (the content view) we want it to handle.

The scroll view needs to hold these components now.

All these components need to be children of the scroll view.

e The scroll view will be wth vie e size of the entir l). (minus the nav contro This is really annoying. You mean we have to pull all those components off and then lay out the view again? Isn’t there an easier way?

You’ve got a point. Remember when we said sometimes Interface Builder makes things (a lot) easier? This is one of those times...

you are here 4  255

scroll view construction

Easy GUI REConstruction Apparently we aren’t the first people to realize after we’ve built a view that it needs to be scrollable. Interface Builder has built-in support for taking an existing view and wrapping it in a UIScrollView. Highlight all the widgets (as shown here) in the detail view, then go to the Editor→Embed→Scroll View menu option. Interface Builder will automatically create a new scrolled view and stick all the widgets in the same location on the scrolled view.

Interface Builder will create a UIScrollView just big enough to hold all our components. Since we want the whole view to scroll, grab the corners of the new UIScrollView and drag them out to the corners of the screen, right up to the edge of the navigation bar (we don’t want that to scroll).

Now you have the same listing of widgets as before, but they are under a scroll view.

How will this new scroll view know how much content needs to be scrolled?

256   Chapter 6

saving, editing, and sorting data

The scroll view is the same size as the screen Interface Builder created the UIScrollView, but there are a few finishing touches necessary to make this work the way we want. You need to tell the UIScrollView how big its content area is so it knows what it will need to scroll—you do that by setting its contentSize property. Then you’ll need to add an outlet and property for the UIScrollView, then wire it up in Interface Builder so we can get to it. So how do we figure out how big the contentSize should be? When the UIScrollView is the same size as our screen, we don’t have anything outside of the visible area that it needs to worry about. Since the scroll view is the same size as our UIView that it’s sitting in, we can grab the size from there, like this: scrollView.contentSize = self.view.frame.size;

Once you’ve added that line, you’ll have a scroll view that takes up all of the available space, and it thinks its content view is the same size.

Once you re UIScrollViewsiza e it, the are the same nd its contentSize tell that to tsize. We just need to he scroll view.

Update DrinkDetailViewController.h and DrinkDetailViewController.m to handle our new UIScrollView.

1

 dd an attribute named scrollView to DrinkDetailViewController to hold a reference A to the UIScrollView. You’ll need the field declaration and IBOutlet property, then you will synthesize it in the .m and release it in dealloc.

2

 ire up the new property to the UIScrollView in Interface Builder by adding a new W Referencing Outlet to the UIScrollView connected to your scrollView property.

3

 et the initial contentSize for the scrollView in viewDidLoad:. Remember, we’re S telling the scrollView that its content is the same size as the view it’s sitting in.

you are here 4  257

start scrolling

Update your DrinkDetailViewController.h and DrinkDetailViewController.m to handle our new UIScrollView.

 dd an attribute named scrollView to DrinkDetailViewController to hold a reference A to the UIScrollView. You’ll need the field declaration, an IBOutlet property, to synthesize it in the .m and release it in dealloc.

1

@interface DrinkDetailViewController : UIViewController { @private

UITextField *nameTextField_;



UITextView *directionsTextView_;



UITextView *ingredientsTextView_;



Add a f the new siecld and a property rollView. for

NSDictionary *drink_;

UIScrollView *scrollView_;



}

@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;

ty, Synthesize the proper e tS en then set the cont iz in viewDidLoad.

DrinkDetailViewController.h

Clean up our reference in dealloc.

@synthesize scrollView=scrollView_;

-

(void)viewDidLoad {

Set the initial contentSize.

[super viewDidLoad]; self.scrollView.contentSize = self. view.frame.size;

}

- (void)dealloc {

[scrollView_ release]; [nameTextField_ release]; [ingredientsTextView_ release];

...

DrinkDetailViewController.m

258   Chapter 6

saving, editing, and sorting data

2

Wire up the new property to the UIScrollView in Interface Builder.

Test Drive Tap in the text field and the keyboard appears...but nothing’s scrolling!

Why isn’t it working yet? Think about all the things that you have going into this view—the scroll view, the main view, and the keyboard...

you are here 4  259

keyboard means changes

The keyboard changes the visible area The problem is that the keyboard changes the visible area but the scroll view has no idea that just happened. The scroll view still thinks it has the whole screen to display its content, and from its perspective, that’s plenty of room. We need to tell the scroll view that the visible area is smaller now that the keyboard is there.

Content view

ent In DrinkMixer, the coasntour view is the same size e, which scroll view’s initial siz is the whole screen...

Scroll view

appears ...but then the keyboaanrdd covers up over the scroll view visible area. a large part of the scroll view it We need to tell the with. has less space to work

iOS tells you about the keyboard, but doesn’t tinker with your views. Just because iOS knows that the keyboard is there, it doesn’t know how your app wants to handle it. That’s up to you!

260   Chapter 6

saving, editing, and sorting data

Wouldn't it be dreamy if iOS could just tell the app when the keyboard appears? But I know it's just a fantasy…

you are here 4  261

iOS notifications

iOS notifies you about the keyboard Interacting with the keyboard and the scroll view brings us to a part of the iOS we haven’t talked about yet called Notifications. Just like component events being passed around our application, there are system-level events, called Notifications, that are being passed by iOS. The secret to knowing what’s going on with the keyboard is knowing about these events. 1

 am taps in the Drink S name field and the field becomes the first responder. Now the iOS needs to show the keyboard.

Event

UIKeyboardDidShowNotification

 he iOS posts a notification to the T default NSNotificationCenter named UIKeyboardDidShowNotification.

2

Object

Selector

DetailDrinkViewController

keyboardDidShow

NSNotificationCenter

4

 he NSNotificationCenter T invokes the target selector and passes it information about the object that triggered the event, along with event-specific details. [registeredObject keyboardDidShow:eventInfo];

262   Chapter 6

3

 SNotificationCenter looks up the N event to see if anyone is registered to be told when that event happens. Objects are registered by providing a selector (method) to call if the event is triggered.

saving, editing, and sorting data

Register with the default notification center for events iOS supports more than one NSNotificationCenter, but unless you have specific needs for your own, you can just use the default system-level one. You can get a reference to the default one by calling this: [NSNotificationCenter defaultCenter];

With the notification center, you can register for events by passing the object you want the notification center to call back to (usually yourself), the method to call, an event you are interested in (or nil for any event), and, optionally, the sender you want to listen to (or nil for all senders).

ested in system Since we’re intwere’ll use the default notifications, nter. notification ce

We want the notification center to call us (the DetailDrinkViewController), so we pass self in as the observer.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@ selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];

or Create the seledctname from a metho actions. just like with

Don’t forget the colon here, because you’re gothinge to get details about gument. notification as an ar

Since we will only regis events when our windowter for keyboard care who sends the even is visible, we don’t t.

Then unregister when you’re done Just like memory management, we need to clean up our registrations from the notification center when we don’t need them any longer. We’ll register for events in viewWillAppear: and unregister in viewWillDisappear:. Unregistering for an event is easy—just ask the notification center to removeObserver for the object you registered. [[NSNotificationCenter defaultCenter] removeObserver:self];

Make sure you unregister from the same notifica center you registered wition th.

er to notification cent he t k ed as y pl m re si e ve W ng we’ gister hi yt er ev om fr remove us ly want to stop receiving for. If you onications, you can specify the certain notif well. notification as you are here 4  263

notification know-it-all

The notification center exposed This week’s interview:

Why do you talk so much? Head First: Um, this is embarrassing, but I’m not entirely sure I have the right Notification Center here...

passing. The sender wants to tell somebody that something happened, you call a method on that somebody...what’s different?

Notification Center: Well, unless you need something weird, it’s probably me. I’m the guy everybody goes to by default. Heads up! An app’s shuttin’ down. Be with you in a second.

Notification Center: It’s similar to message passing, but there are some differences. First, the senders don’t need to know who to tell. They just tell me that something happened and I’ll figure out if anyone cares. Second, there might be lots of people interested in what’s going on. In normal message passing, the senders would have to tell each one individually. With notifications, they just tell me once and I’ll make sure everyone knows. Third, unlike the delegate pattern—where the message gets sent to only one object—I can send messages to any number of objects. Finally, the receiver of the notification doesn’t need to care who’s sending the message. If some object wants to know that the application is shutting down, it doesn’t care who’s responsible for saying the app’s quitting, the object just trusts me to make sure they’ll know when it happens.

Head First: Wow—so you know about every app that starts and stops? Notification Center: Yup. I’m the default center; all the system events go through me. Now, not everybody is interested in what’s going on, but if they want to know, I’m the guy to see. Head First: So when someone wants to know what’s going on, they tell you what they’re interested in, right? Notification Center: Exactly. If somebody wants to know about somethin’ in the system, they register with me. They tell me the notification they want me to watch for, who I should tell when it happens, and, if they’re really picky, who should have sent it. Head First: So then you tell them when that notification happens? Notification Center: Right—they tell me what message to send them when I see the notification they were interested in. I package up the notification information into a nice object for them and then call their method. Doesn’t take me long at all; the sender almost always waits for me to finish telling everyone what happened before it does anything else. Interviewer: Almost always? Notification Center: Well, the sender could use a notification queue to have me send out the notifications later, when the sender isn’t busy, but that’s not typically how it’s done. Head First: Hmm, this sounds a lot like message 264   Chapter 6

Head First: So can anyone send notifications? Notification Center: Sure. Anybody can ask me to post a notification and if anyone’s registered to get it, I’ll let them know. Head First: How do they know which notifications to send? Notification Center: Ah, well that’s up to the sender. Different frameworks have their own messages they pass around; you’ll have to check with the framework to see what they’ll send out. If you’re going to be posting your own notifications, you almost certainly don’t want to go blasting out someone else’s notifications; you should come up with your own. They’re just strings—and a dictionary if you want to include some extra info— nothing fancy. Head First: I see. Well, this has been great, Notification Center. Thanks for stopping by!

saving, editing, and sorting data

So registering for the keyboard event sounds pretty easy, but there’s more to do after that, right?

Exactly. We need to tell the scroll view what to do once the keyboard does pop up (and when it goes away). .

Fill in the blanks and get a plan for the next step!

We need to and

We’ll add two

for the events in

that will be called by the

.

when the notifications are posted.

We’ll adjust the size of the

We need to

when the keyboard appears and disappears.

for events in

Bonus Question: What file will you put all these changes in?

you are here 4  265

sharpen solution

Now you have a plan for what to do next.

We need to and

register

for the

UIKeyboardDidHideNotification

We’ll add two

methods

events in

that will be called by the

UIKeyboardDidShowNotification viewWillAppear

.

notification center

when the notifications are posted.

We’ll adjust the size of the We need to

scroll view

unregister

when the keyboard appears and disappears. for events in

viewWillDisappear

.

This code needs to go into AddDrinkViewController, since that’s the only view where we’re going to be using the keyboard.

Q:

I can’t find the list of notifications that are sent by the iOS. Where are they listed?

A:

There isn’t a central list of all the notifications that could be sent. Different classes and frameworks have different notifications they use. For example, the UIDevice class offers a set of notifications to tell you about when the battery is being charged or what’s happening with the

266   Chapter 6

proximity sensor. Apple’s documentation is usually pretty clear about what notifications are available and what they mean. The keyboard notifications are described in the UIWindow class documentation.

Q:

Why would I want to create my own notifications?

A:

It depends on your application. Remember, notifications let you decouple the sender from the receiver. You could use

this in your application to let multiple distinct views know that something happened in your application. For example, let’s say you had a view that let you add or remove items from your application and your app has several different ways to view those things. Notifications could give you a nice way to announce to all the other views that something has changed without your add/ remove view needing to have a reference to each of them.

saving, editing, and sorting data

Go ahead and make the changes to your code to register for the keyboard events. We’ll implement the code to handle the scroll view shortly.

1

Add keyboardDidShow and keyboardDidHide methods to the AddDrinkViewController. For now, just have them print out an NSLog when they are called. We’ll add the meat in a second. Both methods should take an NSNotification*, as they’ll be called by the notification center and will be given notification information.

2

Register for the UIKeyboardDidShowNotification and UIKeyboardDidHideNotification in viewWillAppear(...). You should use the default NSNotificationCenter and register to receive both events regardless of who sends them out.

3

Unregister for all events in viewWillDisappear(...). Create this method and use it to add the code to unregister for events.

4

Add a BOOL to AddDrinkViewController that keeps track of whether the keyboard is visible or not. We’ll talk more about this in a minute, but you’re going to need a flag to keep track of whether the keyboard is already visible. Set it to NO in your viewWillAppear(...) for now.

you are here 4  267

exercise solution

Go ahead and make the changes to your code to register for the keyboard events. We’ll implement the code to handle the scroll view shortly.

- (void)viewWillAppear: (BOOL)animated { [super viewWillAppear:animated];

These are both new methods for the keyboard notifications in the implementation file. We’ll get to those in a minute.



NSLog(@”Registering for keyboard events”); [[NSNotificationCenter defaultCenter] addObserver:self selector:@ selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@ selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil]; // Initially the keyboard is hidden, so reset our variable keyboardVisible_ = NO; If you don’t g } ive it need to keep track of whether the keyboard is

We showing or not. More on this in a minute.

a notification unregister fro m , it w ill remove you to from anything you’ve register ed for.

- (void)viewWillDisappear:(BOOL)animated { NSLog(@”Unregistering for keyboard events”); [[NSNotificationCenter defaultCenter] removeObserver:self]; }

#pragma mark -

#pragma mark Keyboard handlers

- (void)keyboardDidShow:(NSNotification *)notif { NSLog(@”Received UIKeyboardDidShowNotification.”); } - (void)keyboardDidHide:(NSNotification *)notif { NSLog(@”Received UIKeyboardDidHideNotification.”); } AddDrinkViewController.m

@interface AddDrinkViewController : DrinkDetailViewController { BOOL keyboardVisible_; } - (void)keyboardDidShow: (NSNotification *) notif; - (void)keyboardDidHide: (NSNotification *) notif;

AddDrinkViewController.h

268   Chapter 6

saving, editing, and sorting data

Keyboard events tell you the keyboard state and size The whole point of knowing when the keyboard appears or disappears is to tell the scroll view that the visible area has changed size. But, how do we know the new size? Thankfully, the keyboard notification events (UIKeyboardDidShowNotification and UIKeyboardDidHideNotification) include all the information we need.

n Each notificatio comes with a ject. notification ob

NSNotification object

name = UIKeyboardDidShowNotification

object = relevant object or nil

userInfo =

how We need to know is so of the big the keyboardscroll We get the size the userInfo we can tell thevisible keyboard from object. view the new area.



The notific the name ofattion object contains the object it he notification and there’s no relapertains to (or nil if ted object).

Notification objects are dicuserInfo with notificat tionaries information in ion-specific them.

The keyboard size is in the NSNotification object.

Getting the notification is easy, but we get told every time the keyboard is shown, even if it’s already there.

That’s why we need the BOOL to keep track of whether or not the keyboard is currently displayed. If the keyboard isn’t visible when we get the notification, then we need to tell our scroll view its visible size is smaller. If the keyboard is hidden, we set the scroll view back to full size.

you are here 4  269

keyboard magnets

Keyboard Code Magnets Part I Below are the code magnets you’ll need to implement the keyboardDidShow method. Use the comments in the code on the right to help you figure out what goes where.

NSDictionary *info = [notif userInfo];

CGRect viewFrame = self.view.bounds; viewFrame.size.height = keyboardTop - self.view.bounds.o rigin.y;

NSValue *aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];

Frame; w.frame = view self.scrollVie e_ = YES; keyboardVisibl

if (keyboardV isible_) { NSLog(@”%@”, @”Keyboard is notification already visi .”); ble. return; }

Ignoring

CGRect keyboardRect = [aValue CGRectValue]; keyboardRect = [self.view convertRect:keyboardRect fromVi ew:nil]; CGFloat keyboardTop = keyboardRect.origin.y;

NSLog(@”Resizing smaller for keyboard”);

270   Chapter 6

saving, editing, and sorting data

- (void)keyboardDidShow:(NSNotification *)notif {



// The keyboard wasn’t visible before



// Get the origin of the keyboard when it finishes animating.



// Get the top of the keyboard in view’s coordinate system.



//Resize the scroll view to make room for the keyboard



// We need to set the bottom of the scroll view to line up with it

}

AddDrinkViewController.m

you are here 4  271

keyboard magnets

Keyboard Code Magnets Part II Below are the code magnets you’ll need to implement the keyboardDidHide method. Use the comments in the code on the right to help you figure out what goes where.

self.scrollVie w.frame = self .view.bounds; keyboardVisibl e_ = NO;

{ rdVisible_) y hidden. if (!keyboa oard alread yb Ke @” ”, %@ @” g( NSLo cation.”); fi ti no return;

Ignoring

}

NSLog(@”%@”,@”Resizing bigger with no keyboard”);

272   Chapter 6

saving, editing, and sorting data

- (void)keyboardDidHide:(NSNotification *)notif {



// The keyboard was visible



// Resize the scroll view back to the full size of our view

}

AddDrinkViewController.m

you are here 4  273

keyboard magnets solution

Keyboard Code Magnets Part I Solution Below are the code magnets to SHOW the keyboard.

- (void)keyboardDidShow:(NSNotification *)notif {



if (keyboardVisible_) { NSLog(@”%@”,@”Keyboard is already visible. Ignoring We will get this notification whenever the user switches notification.”); text fields, even if the keyboard is already showing. So return; we keep track of it and bail if it’s a repeat. } // The keyboard wasn’t visible before

NSLog(@”Resizing smaller for keyboard”);

// Get the origin of the keyboard when it finishes animating.

NSDictionary *info = [notif userInfo];

We get the keyboard size from

the dictionary...

NSNotification contains a dictionary with the event details; we pull that out here.

NSValue *aValue = [info objectForKey:UIKeyboardFr ameEndUserInfoKey]; // Get the top of the keyboard in view’s coordinate system. // We need to set the bottom of the scroll view to line up with it

CGRect keyboardRect = [aValue CGRectValue]; keyboardRect = [self.view convertRect:keyboardRect fromVi ew:nil]; CGFloat keyboardTop = keyboardRect.origin.y;

//Resize the scroll view to make room for the keyboard

CGRect viewFrame = self.view.bounds; viewFrame.size.height = keyboardTop - self.view.bounds.o rigin.y; self.scrollView.frame = viewFrame; keyboardVisible_ = YES; }

Finally, update the scroll view with the new size and mark that the keyboard is visible.

274   Chapter 6

...then figure out how big the scroll view really is now (basically how big our view is, minus the size of the keyboard).

AddDrinkViewController.m

saving, editing, and sorting data

Keyboard Code Magnets Part II Solution

Below are the code magnets to HIDE the keyboard. Handling the UIKeyboardDidHideNotification works almost exactly the same way, except this time the scroll view needs to be expanded by the size of the (now missing) keyboard.

- (void)keyboardDidHide:(NSNotification *)notif { if (!keyboardVisible_) { NSLog(@”%@”,@”Keyboard already hidden. notification.”); return;



}

Ignoring

ification if we e. Ignore this notoa rd isn’t visibl know the keyb

// The keyboard was visible

NSLog(@”%@”,@”Resizing bigger with no keyboard”);

// Resize the scroll view back to the full size of our view f.view.bounds; self.scrollView.frame = sel keyboardVisible_ = NO;

scroll view to Then resize the ea. the new visible ar

}

AddDrinkViewController.m

you are here 4  275

scrolling works now

Test Drive Go ahead and build and run. Once you get into the detail view, you should be able to scroll the view to all the fields, and the messages in the console help you keep track of what’s going on.

Q:

Manipulating that scroll view size is kind of tricky—how would I have figured that out without magnets?

A:

A great reference for the code samples and information for programming apps in general is the Text, Web and Editing Programming Guide for iOS that is available on the Apple developer website. That has sample code for common problems like managing the keyboard and text views.

Q:

Tell me again why we need to keep track of whether the keyboard is already visible? Isn’t iOS doing that?

A:

The iOS knows the state of the keyboard, but it sends keyboard events out when different controls get focus. So, when the user taps in the first field, you’ll get a UIKeyboardWillShowNotification followed

276   Chapter 6

by a UIKeyboardDidShowNotification. When the user taps into another field, you’ll get another UIKeyboardDidShowNotification so you know the keyboard focus has changed, but you won’t get the keyboard hide event, since it never actually went away. You need to keep track of whether you already knew it was visible so you don’t resize the scroll view to the wrong size.

Q:

The scroll view works, but depending on what the users pick, they still have to scroll to the widget?

A:

Yes—and that’s not ideal. You can ask the scroll view to scroll to a particular spot on the content view if you keep track of which control has the focus. The Text, Web and Editing Programming Guide for iOS has good sample code for that.

Q:

Don’t we know the dimensions of the keyboard that pops up? Why do we

have to figure out the boundaries with all that code? Isn’t it always the same?

A:

It’s not always the same! If your application is landscape, your keyboard is wider than it is tall. If your app is portrait, then it’s taller than it is wide. And the iPad is a completely different size. Apple also makes it clear that they may change the size of the keyboard if necessary and you should never assume you know how big it is. Always get size information directly from the keyboard notifications.

Q:

What are the animation and curve properties in the notification about?

A:

The Keyboard notification includes information on how quickly the keyboard is animating in. You can use this information to animate the size change of your content so the keyboard comes in as your content view shrinks.

saving, editing, and sorting data

Everything scrolls OK, and I can put a drink in, but as soon as I get back to the list, the drink I added isn’t there!

Sam’s drink is missing. As soon as he leaves the drink detail view, the new drink no longer shows up in the main list. We need to figure out how to keep it around longer...

Work through the following questions to think about what this means for our app.

What happens to new drinks when the user hits Save?

Where do we need to add code?

How are we going to save the new drink?

you are here 4  277

create a MutableDictionary

Work through the following questions to think about what this means for our app.

What happens to new drinks when the user hits save?

We dismiss the view and the drink information

is lost. Where do we need to add code?

We need to add some code that actually stores the values

that the user entered. Since we already store our drinks in dictionaries, we can create a new dictionary with the information and add it to the drink array.

How are we going to save the new drink?

We can create a new dictionary by allocing it, but we’re going to need to get a reference to the array from somewhere. Could the RootViewController help with that?

That’s not a bad idea. Creating a new NSMutableDictionary is easy enough, we can do that by allocing and initializing it. We can set the drink interaction on the dictionary using setObjectForKey:. What’s going to take a little more work is adding it to the drink array. We need to give the AddDrinkViewController a reference to the whole drink array. Let’s start by having the RootViewController pass the new drink in after we’ve created it.

278   Chapter 6

saving, editing, and sorting data

Go back and update the RootViewController and AddDrinkViewController to support saving new drinks.

1

Give the AddDrinkController a reference to the master drink array. You’re going to need to add a drinkArray field to the class, a corresponding property, and then synthesize it and release the reference in dealloc. Finally, you need to make sure that the RootViewController passes on a reference to the drink array when it’s setting up the AddDrinkController.

2

Create and add a new dictionary to the array. You need to update the save: method to get the drink details from the controls and store them in a new dictionary. After that, add the dictionary to the master drink array using addObject:.

you are here 4  279

exercise solution

Go back and update the RootViewController and AddDrinkViewController to support saving new drinks. 1

Give the AddDrinkController a reference to the master drink array.

@ interface AddDrinkViewController : DrinkDetailViewController{

}

nce to the We need a refenreadd a new array so we ca drink later.

BOOL keyboardVisible_;

NSMutableArray *drinkArray_;

@property (nonatomic, retain) NSMutableArray *drinkArray;

AddDrinkViewController.h - (IBAction) addButtonPressed: (id) sender {

NSLog(@”%@”, @”Add button pressed.”);

ller Give our newly created AddDrinkViewControwhen a reference to the master drink array for the user adds a new drink.

AddDrinkViewController *addViewController = [[AddDrinkViewController alloc] ini tWithNibName:@”DrinkDetailViewController” bundle:nil];



addViewController.drinkArray = self.drinks;

RootViewController.m

#import “DrinkConstants.h”

We need the constant key names so we can populate the new dictionary.

@implementation AddDrinkViewController

@synthesize drinkArray=drinkArray_;

We need to synthesize the new property.

AddDrinkViewController.m

280   Chapter 6

saving, editing, and sorting data

Create and add a new dictionary to the array.

2

- (IBAction) save: (id) sender { NSLog(@”%@”, @”Save pressed!”);





Since we want to add keys and objects, we to create a mutable dictionary. What problemsneed coul d run into later if you created an immutable version?you

// Create a new drink dictionary for the new values NSMutableDictionary *newDrink = [[NSMutableDictionary alloc] init]; [newDrink setValue:self.nameTextField.text forKey:NAME_KEY]; [newDrink setValue:self.ingredientsTextView.text forKey:INGREDIENTS_ KEY]; [newDrink setValue:self.directionsTextView.text forKey:DIRECTIONS_KEY]; // Add it to the master drink array and release our reference [drinkArray_ addObject:newDrink]; tants to Use the key cons [newDrink release]; ed ne in rink formation, , we d it he ed t c’ d lo al ad e w Since ference. pend it to the

to release our re

// Pop the modal view and go back to the list [self dismissModalViewControllerAnimated:YES];

then ap drink array.

}

- (void)dealloc { }



[drinkArray_ release]; [super dealloc];

r reference And release ourr ay when to the drinkA we clean up.

AddDrinkViewController.m

you are here 4  281

five-minute mystery

Nicole, ready to pamper her VIP guests.

The Case of the Missing Reservations

Five-Minute Mystery

Nicole has been a maitre d’ at Chez Platypus since it opened nearly 10 years ago. This upscale restaurant has a number of distinguished customers who like their dining experience to be just perfect. The VIP guest list hasn’t changed in years and Nicole knows everyone’s face. She takes them right to their favorite table when they show up and makes sure everything is just right. She’s extremely efficient and the restaurant couldn’t do without her... that is, until her recent, tragic, mistake. Earlier this month, Chez Platypus got a new investor. A prominent, if eccentric, Nobel Prize-winning scientist who is known for his particular tastes. Restaurant management dug up the dusty VIP list and added the scientist’s name at the bottom, along with all the detailed instructions for making sure everything was “just so” when he arrived. They trusted that Nicole would take good care of him and didn’t give it another thought. Last night, their new investor arrived a few minutes before some of the other VIP guests. Nicole didn’t even notice him. She continued to move the regular VIPs to their seats and, for all she knew, their new investor did not even exist.

Why would Nicole ignore such an important new guest?

to be continued... 282   Chapter 6

saving, editing, and sorting data

Test Drive That was a lot of code! Run the app and make sure everything is working. Here’s a drink to add to the list (it’s the new house drink in the Head First Lounge).

Red-Headed School Girl Add this to your app.

Canadian whiskey Cream soda Add the whiskey, then the cream

shot glass and drink.

soda to a

you are here 4  283

test drive

Test Drive To properly test the app now, click the Add button and enter the data for the new drink in the detail view. When you’re finished, click Save. Now, what happens back in the list view?

Save whenne! you’re do

But your drink still isn’t’ there! 284   Chapter 6

saving, editing, and sorting data

Debugging

Something’s wrong. We implemented the Save method, created a new drink, added it to the array...and we’re pretty sure all that code works. Before we move on, let’s use the debugger and do a quick sanity check. Uncomment the viewWillAppear in RootViewController.m and set a breakpoint. If the breakpoints are on, it will launch the debugger when you run.

After looking at the initial values, go ahead and continue the application and add a new drink. Check out the values when the RootViewController stops again.

Set a breakpoint at viewWillAppear in the RootViewController.

ation stops When the apploiicnt , expand at the breakp w many drinks self to see ho nk array. are in the dri

What did you find?

What’s going on?

you are here 4  285

exercise solution

Debugging Now we’re going to use the debugger to help us figure out what’s going on.

Notice this time our callstack is different we’re coming from the AddDrinkViewController’s save call. We have 41 objects in the drink array now! That’s our new drink, so we’re adding it to the drink array correctly.

*If you want to see the name in each dictionary, you can use this command in the console: name"] ey:@" p (char*)[[[self.drinks objectAtIndex:0] objectForK

UTF8String]

The array initially has 40 dictionaries in it; after adding our new drink, it has one more. If we use that console command, we can step through them and see that it’s right. What’s going on? The tableview isn’t picking up the new drink. We’ve added it to the drink array, but it’s not getting added to the actual view. It’s like the table view doesn’t know it’s there... What did you find?

286   Chapter 6

saving, editing, and sorting data

The Case of the Missing Reservations Solved Why would Nicole ignore such an important new guest? Nicole hasn’t needed to look at the VIP list in years. She was so concerned that their important customers feel welcome that she didn’t want to have to do something as crass as go back and read a list every time someone arrived. She made a point of memorizing that list so when they came to the restaurant she could recognize and seat them immediately. As far as Nicole knew, there were 10 VIPs on that list and she knew them all.

Five-Minute Mystery Solved

The problem was that the list was changed and no one told her. All it would have taken was a simple “heads up” to Nicole that there was a change to the list and the restaurant’s newest investor wouldn’t have disappeared...along with his money.

you are here 4  287

table view data changes

The table view doesn’t know its data has changed The table view does a number of things to improve performance as much as possible. As a result, if you just change values in the datasource without telling it, it won’t know that something has changed. In our case, we added a new value to the array used by our datasource but didn’t let the table view know about it.

Datasource

The Save button controller modified the drink array used by the datasource...

...but nothing ever told the table view it happened.

View Controller

You need to ask the table view to reload its data Since we’re modifying the underlying data used by the datasource, the easiest way to refresh the table is to ask it to reload its data. You do this by sending it the reloadData message. This tells the tableview to reconstruct everything—how many sections it thinks it has, the headers and footers of those sections, its data rows, etc.

- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated];



}

Ask the table view to reload its data right before the RootViewController appears.

[self.tableView reloadData];

RootViewController.m

288   Chapter 6

saving, editing, and sorting data

Test Drive Update your RootViewController.m to tell the table view to refresh its data before the tableview is shown, and let’s try adding a new drink again.

Red-Headed School Girl Add this to your app.

Canadian whiskey Cream soda Add the whiskey, then the cream

shot glass and drink.

soda to a

you are here 4  289

saving works!

Test Drive To properly test the app now, click the Add button and enter the data for the new drink in the detail view. When you’re finished, click Save. Now, what happens back in the list view?

Save whenne! you’re do

There it is...

290   Chapter 6

saving, editing, and sorting data

Q:

Telling the table to reload all its data seems pretty drastic. Is that really how I should do it?

A:

It’s the simplest way to refresh the table data, but not necessarily the most efficient. It depends on what you’re doing to the table. If you’re modifying the table while it’s visible, you can call beginUpdates and endUpdates to tell it you’re about to make a number of changes and it will animate those changes for you and let you avoid a reloadData call. There are also versions that only reload the specified rows or for a given section. Which you use depends on your application, how much you know about what changed in your data, and how big your dataset is.

Q:

We didn’t add any code to the cancel button. Don’t we have to do something there?

A:

Nope—the cancel button is coded to just dismiss the AddDrinkViewController. This will clean up any memory associated with the controller and throw away any data the user entered in the fields. As long as we don’t manipulate the drink array, we’ve properly canceled any action the user started.

Q:

Did we really need to use the debugger back there? Couldn’t I have just printed out how many items were in the array using NSLog?

Q:

A: Q:

A:

A:

Why can’t I see the drink information in the debugger when I expand the drinks array and dictionaries? This is one of the disadvantages of using a generic class like NSMutable Dictionary for storing our drinks. The debugger knows the class is a dictionary, but that’s about all it can tell us, since all the keys and values are dynamic. You can get to them through the debugging console, but that’s not as convenient as seeing real attributes on classes when you debug something.

Sure, but then you wouldn’t have been able to practice debugging again... :-) Why can’t we use the po command for the GDB debugger to see the names in the dictionary?

You can use po [[self.drinks objectAt Index:0] objectForKey:@”name”] and simplify the command we used earlier. The catch is that sometimes Xcode has trouble figuring out which object you want, so use the command from earlier if that happens.

Uhh—that drink is at the end of the list, not in with the Rs.

Look back at your debugging work. Why is the drink showing up at the bottom of the table? What do we need to do?

you are here 4  291

sorting arrays

The array is out of order, too Our table view gets its information directly from our drink array. In fact, we just map the row number into an index in our array in cellForRowAtIndexPath:.

o an index We map the row number right41intis going to value for the array. So, row 41st spot in the be whatever we have in the array—namely, our new drink.

// Configure the cell. cell.textLabel.text = [[self.drinks objectAtIndex:indexPath.row] objectForKey:NAME_KEY];

RootViewController.m

Sort your array using NSSortDescriptor In order to get the table view properly sorted, we need to sort our data array. NSSortDescriptors can do exactly that. You tell descriptors what to compare by specifying a property, how to compare them with an optional selector, and then which order to display the information in. In our case, we’re looking for alphabetical sorting by the name of the drink.

We want the NSSortDescriptor to sort based on drink names.

// Sort the array since we just added a new drink

NSSortDescriptor *nameSorter = [[NSSortDescriptor alloc] initWithKey:NAME_KEY ascending:YES selector:@selector(caseInsensitiveCom pare:)]; [drinkArray_ sortUsingDescriptors:[NSArray arrayWithObject:nameSorter]]; [nameSorter release];

To do the sort, simply ask the array to sort itself with our selector.

If we didn’t provide a selector, it does a case-sensitive comparison, but we want a case-insensitive one.

Add this in the save method after you add the data to the array but before the view gets popped off the stack. 292   Chapter 6

AddDrinkViewController.m

saving, editing, and sorting data

Test Drive Add the sorting code to AddDrinkViewController, then run the app. Let’s add another drink; this one should end up in the right place.

tail

Guesswork Cock

dry sherry, passion Peach schnapps, gin, e e juice and lime juic fruit juice, pineappl in into Shake together, stra serve.

a cocktail glass and

Great, that new drink is there, but what about the RedHeaded School Girl from before? Don’t we need to deal with saving more permanently?

All our data is lost when we quit... We’re positive we’re updating the array with our new drink, but obviously that new array doesn’t survive quitting and restarting our app. What do we need to do? When should it happen?

you are here 4  293

when to save

Jim: OK, so we should save the array after each new drink is added, right? That will make sure we always have the right data.

Frank

Joe

Frank: Not so fast. Keep in mind the whole speed/memory management thing. Joe: What’s the problem? It’s just a little array. Frank: But that means you could be saving every time you add a drink. Jim: Oh, I see, that means we’ll have to go through reading in the array and saving it back out multiple times. That does seem like a waste. Joe: Well then, when are we supposed to do it? Frank: When we background the app! With multitasking, apps don’t really close anymore, but when it goes to background, we can save it. Jim: How do we do that? How can we tell when the user backgrounds the app? Frank: Hmm...what about that applicationDidEnterBackground method on our app delegate?

Jim

Joe: But the app delegate doesn’t know anything about our drink list or where to save it... Frank: Good point. The UIApplicationDelegate says there’s a notification that goes out, too. I bet we could use that...

Q:

What notification tells us the application is going into the background?

A:

The iOS will send out an UIApplicationDidEnterBackground when your app is put in the background. Since iOS 4, iPhone supports multitasking, so they can do limited tasks in the background, but in general, they suspend shortly after entering the background state.

Q:

Do I need to register to receive it?

294   Chapter 6

A: Q:

Yup—just like any other notification.

What if the user hits the home button or the phone rings or...?

A:

In general, if the user answers the phone, views a text message, or does something else to trigger the iPhone or iPad to switch applications, you’ll get the applicationDidEnterBackgournd. There’s really only one case where you won’t...

Q: A:

What happens if my app crashes?

Then you’re not going to get the notification. The data would be lost in this case. You need to balance how critical it is to make sure no data is lost with the performance impact of saving more frequently. In our case, we’re just going to save on background. In general, Apple advises you to save as close to the user interaction as possible—we’ll build an app that behaves that way later in the book.

saving, editing, and sorting data

Use Jim, Frank, and Joe’s discussion and your skills at working with the API to figure out what to implement to save the array. Update RootViewController.m and RootViewController.h to handle saving.

1

Add the code to save out the new plist of dictionaries. Implement the method that will be called when the UIApplicationDidEnterBackground Notification is sent to save the plist. We’re going to give you a little code snippet to use. This code will only work on the simulator, but we’ll revisit this issue in a later chapter. NSString *path = [[NSBundle mainBundle] pathForResource:@”DrinksDirections” ofType:@”plist”];

[self.drinks writeToFile:path atomically:YES]; 2



Register for the UIApplicationDidEnterBackgroundNotification. We know that the applicationDidEnterBackground: method will be called on the AppDelegate when the application is sent to the background, but our RootViewController really owns all of the data. Have the RootViewController register for the UIApplication DidEnterBackgroundNotifcation just like the AddDrinkViewController did, except add the registration and unregistration code to viewDidLoad and viewDidUnload, respectively.

This code will only work in the simulator!

The code used to save the plist will work fine on the simulator, but fail miserably on a real device. The problem is with file permissions and where apps are allowed to store data. We’ll talk a lot more about this in Chapter 8, but for now, go ahead with this version. This is a perfect example of things working on the simulator but behaving differently on a real device.

you are here 4  295

sharpen solution

Use Jim, Frank, and Joe’s discussion and your skills at working with the API to figure out what to implement to save the array. Update RootViewController.m and RootViewController.h to handle saving.

Add this to viewDidLoad. RootViewController.m

// Register for application backgrounding so we can save data

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];

Implement this in RootViewController.m.

- (void)applicationDidEnterBackground:(NSNotification *)notification { NSString *path = [[NSBundle mainBundle] pathForResource:@”DrinksDirections” ofType:@”plist”]; [self.drinks writeToFile:path atomically:YES]; }

Add this to viewDidUnload.

// Unregister for notifications

lems on This is the code that’s going to give us probfix it) in (and n agai a real device. We’ll run into this ... now. for us the next chapter—bear with

[[NSNotificationCenter defaultCenter] removeObserver:self];

296   Chapter 6

saving, editing, and sorting data

Test Drive Purple C r

ayon

Raspberry li

queur, vod

Here it is!

ka, and pin

eapple juic e

Pour the li qu with pinea eur and vodka over pple juice and garnis ice and then fill h with a g rape. Make sure when you run DrinkMixer the second time you tap on the icon in the simulator; don’t hit Build and Debug again! Author’s note: we thought about showing the same screenshot twice, but figured that wouldn’t prove that it saves after hittingstill the home key and coming back in.



Stopping and hitting “Build and Debug” in Xcode is NOT the same as pushing the Home key and relaunching the app in the simulator!

When you stop the app using Xcode’s Stop button, you are killing the app right then and there. No termination notifications are sent, no saving is done—it’s just stopped. Likewise, when you click Build and Debug, Xcode will reinstall the application on your device before launching it. To test our load and save code, make sure you restart the app by tapping the icon in the simulator.

you are here 4  297

no dumb questions

Q:

So arrays know how to save themselves...Can I just put any object in there and have it save to a plist?

A:

No—not just any old object. Arrays load and save using a Cocoa technique called NSCoding. Any objects you want to load and save must conform to the NSCoding protocol, which includes initWithCoder and encodeWithCoder method—basically, load and save. You’d need to conform to the NSCoding protocol and provide those methods to so that objects can be archived and distributed. However, NSDictionaries do conform to NSCoding (as do the strings inside of them), and that’s why we can load and save so easily.

Q:

What is the deal with giving us code that won’t work on the device? What happens?

A:

Well, to find out what happens, we encourage you to run it on a real device. Then think about why it isn’t working the way you’d expect. We’ll talk a lot more about this in the next app. To give you a hint, it has to with where we’re trying to save the data. This is also a real-world example of something working just fine in the simulator only to behave differently on a real device. You always need to test on both.

Q:

Instead of registering for that backgrounding notification, couldn’t

298   Chapter 6

we have just updated the AppDelegate to get the drink array from the RootViewController and save it in the delegate?

A:

Yes, you could. It’s more of a style and design question than anything else. Right now, the AppDelegate doesn’t know anything about our plist, our drink array, or even the RootViewController, for that matter (other than making it visible). You could argue we’d be breaking encapsulation if we exposed what needs to be loaded and saved for each view up to the AppDelegate. Since we only need to save a single array, it’s not a big deal either way, but if you have a number of views that need to save information or complex persistence code, it’s often cleaner to leave it with the class that needs to know about it rather than lumping it all into the AppDelegate. Technically speaking, though, either one would work.

Q:

Why did we register and unregister in the viewDidLoad and viewDidUnload methods instead of the *Appear methods?

A:

The problem is when and how often those methods are called. viewWillAppear is called whenever the view is about to be shown. That starts out OK—we’ll get that call before the table view shows up and we can register. However, the viewWillDisappear will be called right before we show the detail or add drink view controllers (since our RootViewController is about to be hidden).

If we unregister there, we won’t get the backgrounding notification if the user decides to quit while looking at the details for a drink. For example, say the user adds a new drink, goes back to the RootViewController, then taps on his drink to make sure he entered it correctly. We show the detailed view, he’s happy, then he quits the app. Our RootViewController has unregistered for the backgrounding notification and the drink is lost. Instead, we use the load and unload methods, which are called when the view is loaded from the nib or unloaded. Since that view is in use throughout the application, those won’t be called except at startup and shutdown.

Q:

What’s the deal with hitting “Build and Run” versus tapping on the icon to start DrinkMixer the second time?

A:

It’s because of how we’re saving the data. We’ll talk more about it in the next chapter, but the problem is when you hit “Build and Debug,” Xcode compiles and installs the application onto the simulator. This means it’s replacing the modified drink plist with the one that we ship with the application and you lose your drink. Which, everyone can agree, is very, very sad.

saving, editing, and sorting data

That’s great! Now I can add the extra drinks I need. But there are a couple of other things that I need to really make this app work for me. Think you can help?

r ideas:

DrinkMixe

se t I don’t u a h t s k in r Delete d list smaller. e to keep th ks ts for drin n ie d e r g in Edit the he list. I like to t that are in nd change things a experiment up!

How can we implement these things? Where in the app do we need to handle this stuff?

you are here 4  299

table views support editing

Table views have built-in support for editing and deleting Good news! The table view comes complete with almost everything we need for deleting data. This is behavior that acts a bit like implementing a Save or Cancel button, and a lot of it comes preloaded. Editing mode adds an Edit button to the Navigation Control in the main view, and when it’s pressed, indicators appear to the left of the table cell that can be selected and deleted like this:

ad “edit” This button will re will it , and when pushed icon te display the dele tton to and change the bu “done.”

View

Datasource

the The edit button erin how to view tells the us e. enter editing mod

The drinks array will be modified as needed after the drinks are deleted.

Delegate

The delegate (our view controller) will handle which mode the table is in and handle deleting drinks. 300   Chapter 6

saving, editing, and sorting data

Editing view construction Using the view below, write what each part of the editing view does.

you are here 4  301

editing view construction solution

Editing view construction solution Using the view below, write what each part of the editing view does.

The Done button turns off editing mode and puts the table back to normal.

The delete icons let the user delete a row from the table.

302   Chapter 6

The + button is unchanged: it lets us add a new drink.

When tapping on a row in edit mode, we should be able to edit a drink instead of just displaying it.

saving, editing, and sorting data

The Xcode template we chose for this app comes with a good bit of the code we’ll need, and at this point, you’re pretty familiar with the RootViewController and the table view. We’ll give you some hints on what to implement next, but let you take it from here.

1

Add the Edit button to the root view. We need an Edit button in the upper left of the navigation bar. The templated code for the UITableViewController comes with everything we need built-in; it’s just a matter of uncommenting the line in viewDidLoad.

2

Implement tableView:commitEditingStyle:forRowAtIndexPath. Once the table view is in editing mode, we’ll get a call when the user tries to delete a row either by swiping across the row or tapping the delete indicator. Most of this method is stubbed out for us too, but you’ll need to add code to update the datasource with the change. Remember, we’ve been mapping rows to indexes in our array. Finally, you don’t need to call reloadData after this change because we ask the tableView to explicitly remove the row.

3

Update the didSelectRowAtIndexPath to add a drink. Our AddDrinkViewController has nearly everything we need to be able to edit an existing drink. Update didSelectRowAtIndexPath to invoke the AddDrinkViewController instead of the DrinkDetailViewController if we’re in editing mode. to create a Navigation

Controller You’ll need ntroller, Co iew rinkV for this display of AdtedDd modally. since it’s being presen

4

Make sure Interface Builder knows it’s editable. Verify that “Allow Selection While Editing” is checked for the Drinks table view.

5

Add the ability to edit a drink in our AddDrinkViewController. You’ll need to tell the app that it must edit a drink instead of creating a new one, then have it populate the controls with the existing information, and finally update the drink on save.

you are here 4  303

exercise solution

The Xcode template comes with a good bit of the code we’ll need, and at this point you’re pretty familiar with the RootViewController and the table view. We’ll give you some hints on what to implement next, but let you take it from here. 1

Add the Edit button to the root view. We need an Edit button in the upper left of the navigation bar. The templated code for the UITableViewController comes with everything we need built-in; it’s just a matter of uncommenting the line in viewDidLoad.

In viewDidLoad

self.navigationItem.leftBarButtonItem = self.editButtonItem;

The UITableViewController comes with built-in support for an edit button. All we need to do is add it to the nav bar.

RootViewController.m

2

Implement the tableView:commitEditingStyle:forRowAtIndexPath. Once the table view is in editing mode, we’ll get a call when the user tries to delete a row either by swiping across the row or tapping the delete indicator. Most of this method is stubbed out for us too, but you’ll need to add code to update the datasource with the change. Remember, we’ve been mapping rows to indexes in our array. Finally, you don’t need to call reloadData after this change because we ask the tableView to explicitly remove the row.

// Override to support editing the table view. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEdi tingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { Use removeObjectAtIndex to // Delete the row from the data source. clean up our datasource.

[self.drinks removeObjectAtIndex:indexPath.row];



}

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

} else if (editingStyle == UITableViewCellEditingStyleInsert) { }

RootViewController.m

304   Chapter 6

saving, editing, and sorting data

3

Update the didSelectRowAtIndexPath to add a drink. Our AddDrinkViewController has nearly everything we need to be able to edit an existing drink. Update didSelectRowAtIndexPath to invoke the AddDrinkViewController instead of the DrinkDetailViewController if we’re in editing mode.

// Override to support row selection in the table view. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath {

if (!self.editing) {

DrinkDetailViewController *drinkDetailViewController = [[DrinkDetailViewController alloc] initWithNibName:@”DrinkDetailViewController” bundle:nil]; drinkDetailViewController.drink = [self.drinks objectAtIndex:indexPath.row]; [self.navigationController pushViewController:drinkDetailViewController animated:YES]; First, we need to check to see if [drinkDetailViewController release];

in editing mode. If not, just

we’re } display the normal detail view. else { AddDrinkViewController *editingDrinkVC = [[AddDrinkViewController alloc] initWithNibName:@”DrinkDetailViewController” bundle:nil]; editingDrinkVC.drink = [self.drinks objectAtIndex:indexPath.row]; editingDrinkVC.drinkArray = self.drinks;

UINavigationController *editingNavCon = [[UINavigationController alloc] initWithRootViewController:editingDrinkVC]; [self.navigationController presentModalViewController:editingNavCon animated:YES]; If we are in editing mode, create an [editingDrinkVC release]; nkViewController and set the drink to AddDri [editingNavCon release]; in addition to our drink array. We’ll fix edit } nkViewController in a minute...

}

up the AddDri

RootViewController.m

Just the AddDrink ViewController left... you are here 4  305

exercise solution

The Xcode template comes with a good bit of the code we’ll need, and at this point you’re pretty familiar with the RootViewController and the table view. We’ll give you some hints on what to implement next, but let you take it from here. 4

Make sure Interface Builder knows it’s editable. Verify that “Allow Selection While Editing” is checked for the Drinks table view.

306   Chapter 6

saving, editing, and sorting data

Add the ability to edit a drink in our AddDrinkViewController. You’ll need to tell it that it must edit a drink instead of creating a new one, then have it populate the controls with the existing information, and finally update the drink on save.

5

- (IBAction) save: (id) sender {

NSLog(@”Save pressed!”);



if (self.drink != nil) {



If there’s a drink set, then we need to update it. We can either update the existing object or replace it. Since we need to re-sort the whole array anyway (in case the drink name changed), we just remove the old one and readd it.



// We’re working with an existing drink, so let’s remove



[drinkArray_ removeObject:self.drink];



// it from the array to get ready for a new one

self.drink = nil; // This will release our reference too



}



// Now create a new drink dictionary for the new values



NSMutableDictionary* newDrink = [[NSMutableDictionary alloc] init];

AddDrinkViewController.m

you are here 4  307

it’s all in there

Test Drive Try removing or editing drinks now. You should be able to remove drinks and fine-tune them all you want. Remember to restart your app by tapping on the icon, though; otherwise, you’ll lose your changes.

Resubmit your app to the store and... 308   Chapter 6

saving, editing, and sorting data

Here’s DrinkMixer at #1! Congratulations!

you are here 4  309

navigationcontroller cross

NavigationController cross

Untitled Puzzle Let’s check your scroll view, nav control, and table view buzz words!

1

2

Header Info 1 Header Info 2 etc...

3

4 5

6

7

8

9

Across

Down

1. A field that the user can change is _____________. 2. Arrays load and save using ___________. 5. System-level events that can be passed are called _____________. 6. Sort data using the _________________. 7. All the sytem events go through the _____________ center. 8. The scroll view won't work without setting the _____________. 9. viewWillAppear and ____________ are called at different times.

1. Table views have built-in support for ___________. 3. Keyboard events tell you about the ___________ and size of the keyboard. 4. The ______________ handles the scroll bar, panning, zooming, and what content is displayed in the view.

310   Chapter 6

saving, editing, and sorting data

Q:

I like the automatic editing support in the table view, but how do I do those cool “Add New Address” rows that the iPhone has when you edit a contact?

A:

It’s a lot easier than you think. Basically, when you’re in editing mode, you tell the table view you have one more row than you actually have in your data. Then, in cellForRowAtIndexPath, check to see if the row the table view is asking for is one past the end. If it is, return a cell that says “Add New Address” or whatever. Finally, in your didSelectRowAtIndexPath, check to see if the selected row is one past your data, and if so, you know it was the selected row.

Q:

We haven’t talked about moving rows around, but I’ve seen tables do that. Is it hard?

A:

No, the table view part is really easy; it’s the datasource part that can be tricky. If you support moving rows around, simply implement the method tableView:move RowAtIndexPath:toIndexPath: (the tableview checks to see if you provide this method before allowing the user to rearrange cells). The users will see a row handle on the side of the cells when they’re in editing mode. When they move a row, you’ll get a call to your new method that provides the IndexPath the row started at and the IndexPath for the new position. It’s your job to update your datasource to make sure they stay that way. You can also implement tableview:canMoveRowAtIndexPath to only allow the users to move certain rows. There are even finer-grained controls in

the delegate if you’re interested, such as preventing the users from moving a cell to a certain section.

Q:

What if I don’t want the users to be able to delete a row? Can I still support editing for some of the rows?

A:

Absolutely. Just implement tableview: canEditRowAtIndexPath: and return NO for the rows you don’t want to be editable.

Q:

When we edit a drink, we replace the object in the array. What if we had some other view that had a reference to the original?

A:

Great question. The short answer is you’re going to have a problem, no matter how you handle it. If some other view has a reference to the object we removed, that’s not tragic since the retain count should still be at least 1; the object won’t get deallocated when we remove it. However, the other views obviously won’t see any of the changes the user made since we’re putting them in a new dictionary. Even if they had the old dictionary, they wouldn’t have any way of knowing the values changed. There are a few ways you could handle this. One option is you could change our code to leave the original object in the array and modify it in place, then make sure that any other view you have refreshes itself on viewWillAppear or something along those lines. Another option is you could send out a custom notification that the drink array changed or that a particular drink was modified. Interested views can register to receive that notification.

Q:

Aren’t we supposed to be concerned about efficiency? Isn’t removing the drink and reading it inefficient?

A:

It’s not the most efficient way since it requires finding the object in the array and removing it before reinserting it, but for the sake of code clarity, we decided it was simpler to show. We’d have to re-sort the array regardless of which approach we took, however, since the name of the drink (and its place alphabetically) could change with the edit.

Q:

We added the edit button on the left-hand side of the detail view, but what about a back button? Isn’t that where they usually go?

A:

That’s true. When you get into having an add button, an edit button, and a back button, you run into a real estate problem. The way we solved it was fine, but you’ll need to make sure that your app flows the way you need it to when your Navigation Controller starts to get crowded.

Q:

We still have some oddities like the drink being editable in the detail view. Should we worry about that?

A:

Yes. So much of what has made iOS and the app store successful is the polish that Apple and iOS developers have put on their applications. It’s the difference between a pretty good app and a chart-topping, wowinducing success purchased by hundreds of thousands of users.

you are here 4  311

navigationcontroller cross solution

NavigationController cross Solution Untitled Puzzle

Header Info 1

Let’s check your scroll view, nav control, and table view Header Info 2 buzz words! etc...

1

E

D

I

T

A

B

L

2

E

N

D 5

T

N

O

T

I

F

I

I N

S

C

O

D

I

I

O

N

S

D

L

O

A

N

G

T 4

I

6

3

S C

A A

R S

S

O

R

T

D

E

S

C

R

I

P

T

G

O

T E

R

L 7

D

E

F

A

U

L

T

V 8

C

O

N

T

E

N

T

S

I

Z

E

W D

I

E 9

V

I

E

D

Across

Down

1. A field that the user can change is _____________. [EDITABLE] 2. Arrays load and save using ___________. [NSCODING] 5. System-level events that can be passed are called _____________. [NOTIFICATIONS] 6. Sort data using the _________________. [NSSORTDESCRIPTOR] 7. All the sytem events go through the _____________ center. [DEFAULT] 8. The scroll view won't work without setting the _____________. [CONTENTSIZE] 9. viewWillAppear and ____________ are called at different times. [VIEWDIDLOAD]

1. Table views have built-in support for ___________. [EDITING] 3. Keyboard events tell you about the ___________ and size of the keyboard. [STATE] 4. The ______________ handles the scroll bar, panning, zooming, and what content is displayed in the view. [SCROLLVIEW]

312   Chapter 6

saving, editing, and sorting data

CHAPTER 6

Your iOS Development Toolbox You’ve got Chapter 6 under your belt and now you’ve added saving, editing, and sorting data to your toolbox.

Scroll View

Acts like a lens to show only the part of the view you need and scrolls the rest off the screen. Needs to be given a contentSize to work properly. Can be easily constructed in Interface Builder

Notifications

Are system-level events that you. can monitor and use in your app The default notification center handles most notifications. Different frameworks use different notifications, or you can create your own.

Sorting

Arrays can be sorted NSSortDescriptors. using

Table View

Editing

There’s bu editing a ilt-in support for table view . The edit b u t t lots of fu on comes with methods t nctionality, includin table view o delete rows from g . t

he

you are here 4  313

Sam’s new idea

Sam has another project in mind...

Congrats on the app. So, we all just got iPads as a bonus. I bet DrinkMixer would look awesome on mine!

314   Chapter 6

7 migrating to iPad

We need more room I need a device that’s more like a book. Does Apple have one of those?

iPhones are great, but a bigger screen can be better.  When the iPad first launched, some panned it by saying that it was “just a big iPhone” (but uh, without the phone). In many ways it is, but that screen opens up many opportunities for better user interaction. More screen real estate means that reading is comfortable, web pages are easily viewed, and the device can act more like a book. Or a calendar. Or many other things that you already know how to use, like a menu...

this is a new chapter   315

drinking big

DrinkMixer on the iPad It happens all the time: a new device comes out and now your clients want to use it. iPad and iPhone apps have lots of overlap. iOS runs both devices, which are touch screen–based, and only one app is allowed to be visible at a time to users. The big difference? The screen size!

ed in any iPad is meant to be usor landscape. orientation, portrait

the People tend to usertrait iPhone most in po mode, like this.

The iPhone screen is only 3.5” diagonally.

The iPad screen is 9.7” diagonally.

Lots of iPads are Wi-Fi only.

316   Chapter 7

The iPad screen resolution is 768 x 1024.

iPhones have data access most of the time. The iPhone 4 (with Retina display) has a resolution of 640 x 960.

migrating to iPad Let’s just run DrinkMixer on the iPad. iPhone apps are supposed to work too, right?

Yes, they do. Every iPhone app on the App Store will run on iPads right out of the box, but there’s a catch.

Do this!

Click here and select iPad Simulator.

Open up DrinkMixer in Xcode and run it in the iPad simulator. Then you can see what we’re dealing with.

en on To see what will happ need to the iPad, you just or for switch the simulat Xcode.

Build and run... you are here 4   317

big, but ugly

The iPad simulator The simulator will launch the iPad with the DrinkMixer iPhone app, as is. You didn’t have to change a line of code, and it does work. Of course, the simulator is enough to show you that just running the iPhone app on the iPad isn’t ideal. First off, it’s the size of an iPhone app, but on an iPad. Ugh. You can change the size of the view to fill the entire screen by “doubling” the pixels, but that doesn’t change the resolution of the view, so the graphics don’t look as good. Play with it in the simulator, too, and we’ll bet it doesn’t really feel right, either...

When using pixel doublyling, the app looks not on pixelated, but wrong! It looks totally out of proportion.

Click here to double the pixels.

Wow. I don’t know how you fix it, but that is not what I was thinking an iPad version of DrinkMixer would look like...

318   Chapter 7

migrating to iPad

The HIG covers iPads, too Since the device has changed, let’s go back to the HIG. If you work your way through the iOS Human Interface Guidelines, you’ll find a section called “For iPad: Restrain Your Information Hierarchy.” For DrinkMixer, that means we need to use the extra real estate that comes from the big screen to reduce the number of screen transitions. For example, you can easily display a detail view next to a list view using a Split View Controller.

The view shows two levels of data, called a Split View Controller.

This is a popover. It’s used in portrait mode.

This is the segmented control. It’s another way to consolidate information into a single view rather than introduce a hierarchy.

What are some apps that you’ve used for both iPad and iPhone? Are there any elements that are iPad-specific?

you are here 4   319

interview with iPad

iPad Exposed This week’s interview:

What makes you different?

Head First: Hi, iPad! It really exciting to be interviewing a famous device like you.

usually put a lot on one screen. They call that “restraining the hierarchy.”

iPad: Thank you! I’m happy to be here. There are lots of other tablets out there, but once you know me, you know the best.

Head First: Interesting. Do you have any special views?

Head First: That’s a great lead-in for me. How do you answer the critics that say you’re just a big iPhone? iPad: Would that really be such a bad thing? People who say that just don’t understand us. We do have some things in common, like the touch screen, iOS, accelerometers, and awesome apps, but the apps are usually different. Head First: Well, your apps are bigger, right? iPad: Yes, but you’re missing the point. My screen is bigger, but because of that, you interact with me very differently. Head First: How so? People still use the same gestures? iPad: When you pick me up, I’m more like a book than iPhone. iPhone is more about getting things done quickly and moving on. Me, you want to sit down and spend some time with. I might even write my own book: iPhones Are From Mars, iPads Are From Venus. Don’t you agree?

iPad: I do. To reduce the number of views, there is a Split View Controller just for my apps. In landscape, it shows a table view on the left side of the screen and the detail view on the other side. So as soon as you pick something, you can see the details without hiding the list. Head First: Is that what you use for mail? iPad: Yup, it’s perfect for that. And if you shift that Split View Controller to portrait, it just shows the detail view, until you click on a navigation button and see the popover. Head First: The what? iPad: The popover. It’s another one of my own special controls. It’s like a dialog box that appears on the screen without covering the whole thing up. Head First: Like a speech bubble? iPad: Exactly. Great for little bits of information, color settings, stuff like that. Head First: What don’t you have?

Head First: Ummm, that’s probably true. I’ve noticed that you move around more, too.

iPad: I’m like an iPod Touch. Limited GPS, sometimes no camera. Those are the biggies.

iPad: My Apps need to support all four orientations, so no matter how you pick me up, it just works. And there should be less bouncing around.

Head First: Anything you’d like to add?

Head First: What? iPad: When you’re using iPhone, the screen is really small (but pretty, I wish I had that awesome display). Anyway, because the screen is small, you’re going between screens a lot on iPhone. With me, designers

320   Chapter 7

iPad: Well, people should really take advantage of my size. Use visual clues from the real world to help people use your app. You have lots of space to work with and room for lots of fingers touching things. Think about how real books, calendars, switches, dials, and real physical controls look and feel. Take advantage of that. Head First: Thanks, iPad. Can’t wait to get started!

migrating to iPad

Sketch up the UI for the new DrinkMixer iPad app. You’ve got more room, so be sure to use it well! Consult the HIG and make sure you know what information is going in which element of the view.

Remember, we want to convert this whole app into an iPad app, with the right iPad controls.

you are here 4   321

exercise solution

Now we have an idea of the UI for the new iPad app. Having this all put together first is going to help keep everything going in the right place as you code.

Navigation control

Drinks List

l view Detai here

he iPadWe’ll use Stplit View specific er. Then we Controll the drink can have he left and list on t edients on the the ingr right.

Navigation control

Drinks List

l view Detai here

This is the popover that comes with the Split View Controller.

322   Chapter 7

a The Split View Controller is han s pretty powerful control. It itdlelooks restraining the hierarchy and de with good too, even in portrait mo a popover!

migrating to iPad

Q:

Is building an app for iPad really that different than building for iPhone?

A:

From a coding and development perspective, no, they’re nearly identical. But from a design and UI perspective, yes, they’re very different. It’s important to spend time with an iPad to make sure you “get it.” When designing iPad apps, there will be a point where an app will just start to feel like an iPad app. It’s lots of little design elements like the use of space, textures on controls and UI components, interaction patterns, etc. iPhone apps are much more about ease of use with your thumb, quick access to data, etc. iPad apps are “bigger” than that. People sit on couches and really soak in iPad apps. Give them that kind of depth.

Q:

Does “restraining the hierarchy” just mean using a split-view control?

A:

No. That’s one way to help get there, but it’s definitely not the only way. We’ll

Behind the Scenes

use a split view control for DrinkMixer, but there are lots of other things to consider. For example, let’s say you have summary information about chapters in a book. In an iPhone app, you might have a table view listing the chapters, and tapping on a chapter will show that summary information. In an iPad app, you might want to have a fancy table of contents only showing three or four summaries at a time but indicate to the user that they can turn the page to see the next set. Hook that up with a swipe gesture and a nice page curl animation and you have a much more natural way of flipping through the same material without needing to slide views in and out as the user moves through data.

Q:

Are we going to have the same hardware issues with iPad as we did with iPhone? Specifically different capabilities and features?

Wi-Fi–only ones obviously don’t have 3G connectivity or a true GPS. The iPad 2 has two cameras while the original iPad doesn’t have any. iPad 2’s graphics and processing capabilities are substantially better, too. The good news is that you should handle it just like iPhone and iPod Touch differences— simply check for device capabilities and code around not having them.

Q:

Does the iPad run a different version of iOS or anything?

A:

No; well, no more so than iPod Touch vs. iPhone. There are some controls that are only available on the iPad (and you need to check for them if you build a universal app— more on that later), but the basic OS is the same and you can have a single build that runs on all iOS devices. Speaking of running on lots of devices...

A:

Absolutely. You already had that to some extent with just the first iPad— there are 3G iPads with a GPS while the

ne t for iPho c e j o r p a Building Universal App + iPad =

Creating an App for iPhone and iPad

Now that you’re ready to build this thing, what exactly is it called? When Apple designed the iPad and iPhone to share one OS, Apple introduced the concept of a Universal app, an app that is built for both devices. That still means different views for iPhone and iPad, but only one code base that gets submitted to the App Store. Users get both a native iPhone app and native iPad app when they buy your application instead of needing to purchase two separate apps. It’s a big factor in distribution, actually...

you are here 4   323

fireside chat

Tonight’s talk: Universal App Distribution or not?

Universal:

Two Apps:

Ha! Two apps. That’s really inefficient. It really just makes more sense to support iPhones and iPads everywhere, like I do. You do support everyone, that’s true, but that makes you kinda hefty, right? You have to check for like, everything! True, but if I’m written right, most of my code is shared between the devices. It’s really not that hard to just use the right view controllers on the right device and everything else goes from there. See, my apps don’t have that much to worry about. If it’s iPhone, that’s it. Well, except for touches...but I don’t need to deal with everything in one.. I’d be happy to trade worrying about a couple more devices for better sales and reviews. Wait a sec. I’m really the moneymaker. If you build two apps, then you can sell twice per user. Every person out there with an iPhone and an iPad has to pay twice to get all of me. The reviews I see have a lot of people complaining about needing to purchase the same app again just to use it on another device. I’m usually a little more expensive, but users love getting more value for their money. Hmm. I don’t buy it. I cost less but only work on a specific kind of device.

324   Chapter 7

migrating to iPad

Universal:

Two Apps:

Let’s talk maintenance. I think we both agree that you don’t want two completely separate code bases, right? Oh, absolutely. Even if you are going to make two different apps, you should be sharing a code base. You definitely don’t want to be in a situation where you’re fixing the same bug in multiple projects or trying to keep them in sync.

Totally agree. So I guess really we’re arguing about a packaging issue. Do the users want to pay a little more and get iPhone and iPad support, or do they want to pay a little less when they only want support for one device, but end up paying more if they want both? You know, I bet there isn’t a simple answer to that. There are lots of users who have lots of different opinions. It probably depends on the app and how the developer wants to interact with the users. I guess there’s room for both of us...

Sam doesn’t want to deal with multiple applications. He wants the iPad version of DrinkMixer to be tied in with the original iPhone app. What kind of app distribution are we going to have to use?

you are here 4   325

building universal

Use Xcode to build your Universal app Since we’re looking to support Sam and keep things easier, it makes more sense to build a universal app that creates one software bundle. This isn’t too hard to implement, because when the code is cleanly separated in the MVC pattern, we just need to talk to another view.

Do this!

Upgrade your app Highlight the project in the Navigator window and you’ll see the basic project settings. Make sure that Targets is highlighted. Under devices, the drop-down box will let you select iPhone, iPad, or Universal. Select Universal.

Selec in yourt the projec window Navigatio t n .

n convert Xcode cpa for iPad. your ap es! Click Y

Xcode generated a new view for the app.

326   Chapter 7

migrating to iPad

Wait—what’s a target? What does it have to do with iPads?

Targets are used in the build process. Xcode completes the build process based on the targets that you identify. A target keeps track of which files and the instructions of what to do with them for a build. We upgraded our target to be a universal build, so now it has everything it needs to build an app that runs on iPhones, iPod Touches, and iPads. As part of the upgrade process, Xcode introduced the new .xib for us and added it to the target.

Geek Bits Since Xcode is used for Mac development too, there are lots of reasons to create several targets, such as frameworks or libraries. Targets are frequently used to build unit tests or application tests as well. The test code is only included in the test targets and won’t be in the release builds. Xcode only builds the active target, so you can build just one piece at a time. We only have one target for DrinkMixer, so it’s always active.

you are here 4   327

test drive

Test Drive Make sure that the iPad simulator is still selected for the build and build and run the app. It should look much more iPad-specific now...

ch a great Because we did nsucode, the job writing cleaeasily to the app can port actions all iPad and the work.

bling badness, We got rid of the pixel-dourgr n iPhone but it still feels like an ove ow app...

328   Chapter 7

migrating to iPad The detail view looks really bad, and it’s just a regular table view—we never told it to use the Split View Controller.

You’re right. Since we’re working with iPhone and a new view, Xcode just ported what we had over to the iPad—in this case, a table view. To put the new Split View Controller into play, we need to fix that.

Split-view Magnets

l

Navigation contro

Drinks List

il Deta e her

view

We’ll walk you through th you just get them in ordeer!coding,

Adding the split view isn’t really that hard if you think about it a bit. Use these magnets to order the steps we need to work through.

Delete the Navi gation Controll er Change the Table View to the Root View

the Declare and add instance r le ol tr on UISplitViewC IBOutlet to the variable and its egate files DrinkMixerAppDel ixer Detail View Add in the DrinkM

Wire up the Split View Con troller reference

Add a Navigation Controller to the Detail Vi ew

brary the li m o r f r trolle ew Con i V t i Spl Add a

Open up MainWindow-iPad.xib in Xcode

you are here 4   329

magnets solution

Split-view Magnets Solution

Adding the split view isn’t really that hard if you think about it a bit. Use these magnets to order the steps we need to work through.

Open up MainWindow-iPad.xib in Xcode

This is the initial file listing that was created by Xcode when the new target was created.

Delete the Navi gation Controll er

ng This Navigation Controller isViegoiw it Spl a h to be replaced wit Controller. So, delete this now.

Add a Split View Controller from the library

330   Chapter 7

migrating to iPad Controller Add a Navigation ew to the Detail Vi

For the Split View Controller to work, you need to have two children. By default, they are a Navigation Controller with an embedded Table View and a standard view controller. We want a Navigation Control on top of the View Controller for the righthand pane. Using a real navigation controller for the detail view gets us access to a navigation bar and the usual edit buttons, like we had for the iPhone version of the app.

We want a Navigation Controller here.

Child no. 1, with table view Navigation Controller

The easiest way to swap out the right view controller is to drag and drop a Navigation Controller into the right pane. Interface Builder will update the right view controller to be a Navigation Controller for us and drop the navigation bar right where we want it.

This view is child no. 2.

ation Drop a Navigright Controller ight View into the r . It should Controller look like this... change to

This is what it’llyou’re look like when done...

you are here 4   331

more magnet solution

Split-view Magnets Solution (cont.) ixer Detail View Add in the DrinkM

ller is the This view controthe landscape detail view on plit View view of the S needs to be Controller. It . our detail view

To change this, highlight the View Controller, and change the view controller class to “DrinkDetailViewController” from the drop-down list in the inspector. able ler instance vari SplitViewControl UI e th s d le ad fi d e an at Declare AppDeleg to the DrinkMixer and its IBOutlet

@interface DrinkMixerAppDelegate : NSObject { UIWindow *window;

UINavigationController *navigationController;

}

UISplitViewController *splitViewController_;

@property (nonatomic, retain) IBOutlet UISplitViewController *splitViewController;

332   Chapter 7

DrinkMixerAppDelegate.h

migrating to iPad

@synthesize splitViewController=splitViewController_;

- (void)dealloc {



[splitViewController_ release];



[window release];



}

[navigationController release]; [super dealloc];

@end

DrinkMixerAppDelegate.m

Wire up the Split View con troller reference

Right-click on the App Delegate and connect the splitViewController outlet to the Split View Controller. Change the table view to the root view

Just like we did with the detail view, change the class type for the table view controller to “Root View Controller.”

But the App Delegate is for the iPhone and the iPad, right? Don’t we need to tell it which view to use?

you are here 4   333

who are you?

Check your devices This is where the code paths for the iPhone and the iPad are going to intersect—in the App Delegate. Because we’re migrating an existing iPhone application, we already have an AppDelegate and it’s set up to add our RootViewController to the window when the application launches. Now that we’ve added iPad-specific views, we need to update our AppDelegate to add the correct one to the window depending on the device. iOS makes it easy to determine which device you’re on through a macro named UI_USER_INTERFACE_IDIOM(). This returns a constant that tells you the type of device your application is running on; we can use this to figure out which view controller to show in the window.

Here we check the UI SER_INTERFACE_IDIO and if it’s an iPad, we_U M() ad view to the window. Othe d the splitViewController’s navigationController’s vie rwise, we add the w.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions :(NSDictionary *)launchOptions { // Override point for customization after application launch. // Add the navigation controller’s view to the window and display.

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {



}



else {



}

}

[self.window addSubview:self.splitViewController.view];

[self.window addSubview:navigationController.view];

[self.window makeKeyAndVisible]; return YES;

DrinkMixerAppDelegate.m

Now it’s ready to run… 334   Chapter 7

migrating to iPad

Test Drive Save everything and then build and run. You may need to switch Xcode back to the iPad setting for the Simulator...

iew that we This is the inv portrait. Just should see view. the detail

he popoversway t e v a h ’t n o We d so there’s no working yete, full drink list. to show th

To see the split view, go up to the Hardware→Rotate Right menu option. you are here 4   335

test drive

Test Drive Rotating DrinkMixer should expose the Split View Controller that we’ve been working on. But there’s a problem...

That isn’t working!

336   Chapter 7

migrating to iPad

Rotation is key with iPad An important part of coding for the iPad is that is has to support all orientations, since Apple is big on there being no wrong way to use an iPad. Users will expect to be able to pick up their iPad any way and have it work. To start supporting all orientations, we need each of our controllers to know that we want to do that.

This method is in all your view implementation files, just commented out. /*

// Override to allow orientations other than the default portrait orientation.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {

// Return YES for supported orientations.



return YES;

}

*/

return (interfaceOrientation == UIInterfaceOrientationPortrait);

Remove this line and replace it with YES to support all orientations.

RootViewController.m DrinkDetailViewController.m

you are here 4   337

test drive

Test Drive Now you’re supporting all the orientations, everything is linked, and devices are checked. The split view should be working now...

Now, push the home key and launch it again. 338   Chapter 7

migrating to iPad

Wait. The app looked OK once it got started, but something weird happened at startup.

We’re not fully supporting rotations yet. The code is all set up to handle a rotation when you’re in the app. At startup, iPad apps should show a launch image first, while they’re loading, like a splash screen. Apple’s HIG recommends that the image should be your actual initial user interface (minus specific data). Depending on the application, though, some people use actual splash screens. Once the images are set up, iOS will pick the image that goes with the current orientation and avoid that awkward rotation of the interface that you saw without appropriate launch images.

We have two images you can use for launch images, you just need to download them and drop them in your project.

Go to http://www.headfirstlabs.com/books/hfiphonedev and download the launch images for this chapter. Select the project in Xcode and scroll down to iPad Deployment Info. Enable all four support device orientations. Drop the portrait launch image into the Portrait Launch image box and the landscape image into the Landscape Launch Image box. Let Xcode copy both into your project.

you are here 4   339

sharpen solution

We’re just about ready to fully support the launch in any orientation. Here’s what your final resources directory should look like.

In the iPad D you should haveepalloyment Info, orientations sele l launch images dropped incted and the to Launch Images.

Xcode will have to your project, added the images it changed the nabut notice that ~ipa mes to Defaultnotation is stand d.png. This ~ipad be used to providard in iOS and can e device specific resources.

340   Chapter 7

migrating to iPad

Test Drive Now we’re supporting all the different orientations, right from the beginning. Try stopping it and relaunching in the simulator from landscape. Everything works!

you are here 4   341

test drive

Test Drive

.. Continued.

What if you tap on one of those rows in the horizontal split-view orientation?

t right! That’s no

342   Chapter 7

migrating to iPad

BE the Detail View Controller

Your job is to play Detail View Controller and figure out why you’re not displaying the right thing. Look at the code below and see if you can figure out what’s going wrong...

When does this code actually get called? - (void)viewWillAppear:(BOOL)animated {

[super viewWillAppear:animated];



// Setup our UI with the provided drink



DrinkDetailViewController.m

self.nameTextField.text = [self.drink objectForKey:NAME_KEY];

self.ingredientsTextView.text = [self.drink objectForKey:INGREDIENTS_KEY];

self.directionsTextView.text = [self.drink objectForKey:DIRECTIONS_ KEY];

}

When should it get called?

What classes are you going to need to update?

What’s the difference here between the iPad and the iPhone?

you are here 4   343

be the solution

BE the Detail View Controller

Your job is to play Detail View Controller and figure out why you’re not displaying the right thing. Look at the code below and see if you can figure out what’s going wrong...

viewWillAppear gets called only when the view gets presented to the user. Not as the user interacts with the view.

When does this actually get called? - (void)viewWillAppear:(BOOL)animated {

[super viewWillAppear:animated];



// Setup our UI with the provided drink



DrinkDetailViewController.m

self.nameTextField.text = [self.drink objectForKey:NAME_KEY];

self.ingredientsTextView.text = [self.drink objectForKey:INGREDIENTS_KEY];

self.directionsTextView.text = [self.drink objectForKey:DIRECTIONS_ KEY];

}

When should it get called?

What classes are you going to need to update?

The detail view needs to be updated each time the user selects a row.

DrinkDetailViewController, RootViewController

What’s the difference here between the iPad and the iPhone?

That restrained hierarchy thing. The iPhone has to show two different views for the detail view and the root view. Since the views are together for the Split View Controller on the iPad, the view isn’t being presented, but it still has to change. 344   Chapter 7

migrating to iPad

A persistent view problem We built things for the iPhone to populate the detail view when it’s about to be displayed. The problem is that with the iPad version, it’s always displayed. What worked well on the iPhone (repopulating the data as the view was presented) doesn’t work well on the iPad, since it will only get one viewWillAppear message—right after the app launches. So what should we do? The wrong solution would be to duplicate our viewWillAppear code. We’re better developers than that, so we’re going to refactor the code into a new method in the DrinkDetailViewController called refreshView that will repopulate the view when the drink changes (via a drinkChanged method).

it. Duplicating Yeah, we saidould be bad. Not that code w good idea” or “maybe not a can...blah blah”. “for now we BAD. Your code It would be nity, too. needs its dig

Add this! add a call tod u o y re su e k a M View metho DrinkDetailViewController.m [super viewWillAppear:animated]; our new refresh ar. in viewWillAppe [self refreshView];

(void)viewWillAppear:(BOOL) animated {

}

- (void)refreshView {

This is code that has been pulled from viewWillAppear



// Setup our UI with the provided drink



self.ingredientsTextView.text = [self.drink objectForKey:INGREDIENTS_KEY];



}

self.nameTextField.text =

[self.drink objectForKey:NAME_KEY];

self.directionsTextView.text =

[self.drink objectForKey:DIRECTIONS_KEY];

method, drinkChangedre showing, ew n ur o s e’ Her the drink we’ which updateses to our new method to then delegatview. update the

- (void)drinkChanged:(NSDictionary *)newDrink {

self.drink = newDrink;

}

[self refreshView];

-(void) refreshView;

-(void) drinkChanged:(NSDictionary *)newDrink;

DrinkDetailViewController.h

@end

you are here 4   345

tableview fixing

Don’t forget the tableview

n the iPad and

Instead of swapping out the table view when a row is selected, the detail view needs to change and the table view should stay the same—but only for the iPad, not the iPhone. To fix that problem, we need to split the code, just like we did in our AppDelegate.

ence here betwee What’s the differ the iPhone?

s g. The iPhone ha in th y ch ar er hi detail view That restrained t views for the en er ff di o er tw to show e views are togeth th e nc Si . ew vi ad, the view and the root ntroller on the iP ange. co ew vi lit sp e for th has to ch ted, but it still isn’t being presen

Add this! @class DrinkDetailViewController;

@interface RootViewController : UITableViewController {

NSMutableArray *drinks_;



DrinkDetailViewController *splitViewDetailView_;

UIBarButtonItem *addButton_;



}

@property (nonatomic, retain) IBOutlet DrinkDetailViewController *splitViewDetailView;

RootViewController.h @synthesize drinks=drinks_, addButton=addButton_, splitViewDetailView=spli

tViewDetailView_;



}

-

(void)dealloc

{

[splitViewDetailView_ release]; [drinks_ release];

[addButton_ release]; [super dealloc];

346   Chapter 7

RootViewController.h

migrating to iPad

d if so, use the new an an , ad iP an on ’re we see if not on We need to check tood on our detailViewController. If we’ree be fore. th lik r me ntrolle co w vie drinkChanged il ta de w ne a push iPad, just create and

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPa th *)indexPath {

if (!self.editing) {

{

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)



}

[self.splitViewDetailView drinkChanged:[self.drinks objectAtIndex:indexPath.row]];

else {

DrinkDetailViewController *detailViewController = [[DrinkDetailViewController alloc] initWithNibName:@”DrinkDetailViewControll er” bundle:nil]; detailViewController.drink = [self.drinks objectAtIndex:indexPath.row];

[self.navigationController pushViewController:detailViewC ontroller animated:YES];

}

}

[detailViewController release];

else {

AddDrinkViewController *editingDrinkVC = [[AddDrinkViewController

RootViewController.h

Test Drive Before you build and run, go into Interface Builder and link up the detail view in the split pane to our new split ViewDetailView property on the RootViewController. Now you’re pushing the detail view onto the right-hand side of the pane, while the left-hand side is still showing the table view.

you are here 4   347

test drive

Test Drive Build and run the application. Now you’re pushing the detail view onto the right-hand side of the pane, while the left-hand side is still showing the table view.

Everything’s working! In landscape at least… 348   Chapter 7

migrating to iPad But if you rotate the thing, it still doesn’t look right.

Right. To fully implement the Split View Controller, we need to have the popover working in the portrait view. If it doesn’t, the user will be stuck in the detail view unless they rotate back to landscape.

Match each control in the landscape view (table, detail view, nav control) to its equivalent in the portrait view. Then you’ll have an idea of what needs to go where!

Landscape

Portrait

Navigation control

Drinks List

l view Detai here

This is the popover that comes with the Split View Controller.

you are here 4   349

who does what solution

SOlUTion Match each control in the landscape view (table, detail view, nav control) to its equivalent in the portrait view. Then you’ll have an idea of what needs to go where!

Landscape

Portrait

This button needs to be created, since it doesn’t map to anything specific in landscape. It will present the popover.

Navigation control

Drinks List

Present the popover! The UISplitViewController, while awesome, doesn’t do anything except manage the two views inside it. When the iPad is rotated to portrait, the views that we’re working with are the same, just like you saw in the exercise. While DrinkMixer supports the detail view in portrait, we need to enable the other hidden view, the table view. We’ll do this with the popover. The popover is an iPad exclusive control that is used to present a table view temporarily, just to allow the user to select another detail view and keep working with the data. To manage moving the views around, we need to conform to the UISplitViewController delegate protocol and present the popover. We also need to set up a button to allow the user to access the popover view in the navigation control of the detail view in portrait.

350   Chapter 7

l view Detai here

migrating to iPad

Go dive into the documentation and find out about the UISplitViewController delegate protocol. Use that to figure out how to implement the items below.

1

Add the UIPopoverController to DrinkDetailViewController.h. Create an instance variable named popOver_ and a corresponding property that’s an IBOutlet.

2

Synthesize and dealloc popOver_ in DrinkDetailViewController.m.

3

Implement UISplitViewDelegate methods in DrinkDetailView Controller.m. Get started based on what you can find in the documentation. We’re going to implement the button in code. If you get stuck, it’s on the next page.

4

Use Xcode to edit the view so that the SplitViewController delegate outlet is connected to our DrinkDetailViewController. You’ll have to open up the iPad main window and expand the Split View Controller to make the connection.

troller acting asthat’s on C w ie lV ai et D ’s the view the Drink Note that it’sw Controller’s delegate. Thatthe view that has the Split Vie e go to portrait, so that’s visible when we popover. to handle th

you are here 4   351

long exercise solution

Once you’re finished implementing the Split View Controller delegate, it can handle all the information properly in landscape and portrait. 1

Add the UIPopoverController to DrinkDetailViewController.h. Create an instance variable named popOver_ and a corresponding property that’s an IBOutlet.

@interface DrinkDetailViewController : UIViewController

{ @private

UITextField *nameTextField_;



UITextView *directionsTextView_;



}

UITextView *ingredientsTextView_; UIScrollView *scrollView_; NSDictionary *drink_;

UIPopoverController *popOver_;

@property (nonatomic, retain) UIPopoverController *popOver;

2

Synthesize and dealloc popOver_ in DrinkDetailViewController.m.

@synthesize drink=drink_, nameTextField=nameTextField_, ing redientsTextView=ingredientsTextVi ew_, directionsTextView=directions TextView_, scrollView=scrollView_,

popOver=popOver_;

- (void)dealloc {

[nameTextField_ release];



[directionsTextView_ release];





DrinkDetailViewController.m

352   Chapter 7

DrinkDetailViewController.h

}

[ingredientsTextView_ release]; [scrollView_ release]; [drink_ release];

[popOver_ release]; [super dealloc];

migrating to iPad

3

Implement UISplitViewDelegate methods in DrinkDetailViewController.m. Get started based on what you can find in the documentation. We’re going to implement the button in code. If you get stuck, it’s on the next page.

#pragma mark - UISplitViewDelegate methods - (void)splitViewController:(UISplitViewController *)svc willHideViewContr oller:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonIt em *)barButtonItem forPopoverController:(UIPopoverController *)pc { barButtonItem.title = @”Drinks”;

[self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES]; }

self.popOver = pc;

troller when This method will be called by the splitViewCon method This r. rolle cont it has to hide the left-hand view use to can we that on butt gives us a popover controller and y. ssar show that hidden view when nece

- (void) splitViewController:(UISplitViewController *)svc willShowViewContr oller:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarB uttonItem *)barButtonItem { [self.navigationItem setLeftBarButtonItem:nil animated:YES]; }

self.popOver = nil;

troller This method gets called when the splitViewCon just We . back r rolle can put the left-hand view cont ditch the button.

DrinkDetailViewController.m

you are here 4   353

long exercise solution

Use Xcode to edit the view so that the SplitViewController delegate outlet is talking to the DrinkDetailViewController. You’ll have to open up the iPad main window and expand the Split View Controller to make the connection.

4

- (void)drinkChanged:(NSDictionary *)newDrink {

Do this!



One last thing—the drinkChanged method needs a quick update. }

self.drink = newDrink; [self refreshView];

if (popOver_ != nil) { }

[popOver_ dismissPopoverAnimated:YES];

our popover Here we make sure to dismiss nk. dri if the user selects a new 354   Chapter 7

DrinkDetailViewController.m

migrating to iPad

Test Drive

Everything should be working now! Try using the button and rotating the simulator.

It works!

you are here 4   355

no dumb questions

Q:

The detail view still doesn’t look all that great. Shouldn’t we fix that?

A:

If we were going up on the App Store, yes. In our next iPad app, later in the book, we’re going to focus a lot more on look and feel. For now, we wanted you to get the controls figured out.

Q:

When we enabled various launch orientations in Xcode, what did that actually do?

A:

If you take a look at your Info.plist in your project, you’ll see that Xcode quietly added an array of enumerations that list the launch orientations you support. The GUI option we used is just a convenience (and new in Xcode 4) for setting those values. iOS looks at your app’s Info.plist to figure out what launch orientations it can use.

Q:

You mentioned the ~ipad thing was standard. Standard for what?

A:

Before the iPad, launch screens were simply named Default.png, then DefaultPortrait.png and Default-Landscape.png. Once the iPad entered the scene, Apple added the concept of ~ to filenames. iOS will pick the most appropriate file based on device type. It does something similar with the @2x notation for high-resolution (iPhone 4 Retina display).

356   Chapter 7

Q:

Are popovers only used with Split View Controllers?

A:

Most definitely not! Popovers are used pretty often in iPad applications. They’re very straightforward to use—they simply wrap a view and you can tell them which control they should appear next to. See the documentation for UIPopoverController for more information.

Q:

We really didn’t do much to support the various screen orientations. Is that normal?

A:

It really depends on your application. When you edit the size information of a control in Xcode, you can set its Autoresizing properties. With those properties, you can anchor a control to the top, bottom, or sides and control whether it stretches when the view changes size (which is typically due to a rotation). If you’re using roughly the same layout for both landscape and portrait (which we are, minus the table view), you can use Autosizing to get you what you want.

For more complicated views, you might hide or show entire controls or resize and relayout controls depending on the orientation. There are a number of view controller callbacks that will get called while the view is rotating to its new orientation, and in there you can update the size, position, and visibility of your controls if necessary.

Typically, you’ll use view animations here to make sure things transition smoothly.

Q:

What happens if I try to use a popover on the iPhone?

A:

Very, very bad things. There are controls and features that only exist on a particular device (and within a particular iOS version). When the iPad first came out, you couldn’t even rely on there being a class name UIPopoverController on the iPhone. Now that’s gotten a little simpler, but you must always check that you’re on a particular device or that the device has the feature you are about to use before trying to do it. Depending on what versions of iOS you support, you will also need to check to make sure certain classes exist before doing anything with them. For example, older code will often have the popover reference we added in the detail view controller declared as type “id” since you couldn’t assume the UIPopoverController was a valid type on iPhones. If you support old versions of iOS, you’ll need to do the same. Apple has excellent documentation on writing backward compatible code that you should look into if you’re going to support older versions of iOS.

migrating to iPad

iPad Cross Let’s get the right brain working. Here are some vocab words from your firstPuzzle iPad chapter. Untitled

Header Info 1 Header Info 2 etc...

1 2 3 4

5

6 7

8

9 10

Across

Down

3. ___________ is what you're doing when you implement code that differs by device 7. This control is iPad specific and controls other views. 9. These are not just big iphones 10. iPads need to support all ________.

1. This control is used only on iPad 2. Apps compiled for both iPhone and iPad are 4. The Split View Controller keeps track of ________ views 5. To implement the popover, you need to add a ________ to the portrait view. 6. This covers UI for iPhone and iPad 8. The images display when the app is starting up

you are here 4   357

iPad cross solution

iPad Cross Solution Let’s get the right brain working. Here are some vocab words from your firstPuzzle iPad chapter. Untitled

Header Info 1 Header Info 2 etc...

1 2

U

3

D

E

V

I

C

E

C

H

E

C

K

I

N

O G

P 4

I 5

6

B 7

U

S

P

T O N

L

I

T

V

A

T 10

8

H I

E

W C

I

E

N

N

T

G T

A

T

I

C

O

V

H

V

E

I

E

R

O

L

S 9

U R

O

O

N

S

I

P

P

A

L

E

R

D D

S

L

C H

Across

Down

3. ___________ is what you're doing when you implement code that differs by device [DEVICECHECKING] 7. This control is iPad specific and controls other views. [SPLITVIEWCONTROLLER] 9. These are not just big iphones [IPADS] 10. iPads need to support all ________. [ORIENTATIONS]

1. This control is used only on iPad [POPOVER] 2. Apps compiled for both iPhone and iPad are [UNIVERSAL] 4. The Split View Controller keeps track of ________ views [CHILD] 5. To implement the popover, you need to add a ________ to the portrait view. [BUTTON] 6. This covers UI for iPhone and iPad [HIG] 8. The images display when the app is starting up [LAUNCH]

358   Chapter 7

migrating to iPad

CHAPTER 7

Your iOS Development Toolbox You’ve got Chapter 7 under your belt and now you’ve added a bunch of iPad controls to your toolbox.

iPad HIG

There are iPad-specific controls, and some rules differ between iPhone and iPad. The Split View Controller and popovers are iPad-specific controls.

Device Checking

Once you build a Universal app, you’ll need to check for different devices so your app can behave differently as needed.

Universal Apps

Depending on how you want to distribute your app, you can build two apps or a Universal app. Universal apps are only sold once, but they contain code for both the iPhone and iPad, which makes maintenance easier and the customers happy!

Split View Controller

This controller’s job is to ke track of two child views th ep displayed differently in portat are and landscape. Once you set rait up properly, you can have a it number of views that displa small y lots of different ways.

you are here 4   359

8 tab bars and Core Data

Enterprise

Bounty hunter apps

Here’s what I ‘ve found: we just can’t be competitive anymore without an iPhone app!

Enterprise apps mean managing more data in different ways. Companies large and small are a significant market for iPhone and iPad apps. A small handheld device with a custom app can be huge for companies that have staff on the go. Most of these apps are going to manage lots of data, and since iOS 3.0, there has been built-in Core Data support. Working with that and another new controller—the tab bar controller—we’re going to build an app for justice!

this is a new chapter   361

bob’s on the go

HF bounty hunting With my business, I’m out of the office a lot. The courts will let me submit evidence from my iPhone now, so I need an app for that to get paid. I picked up an iPad while I was at it, since I figured it would help me do more detailed research during boring stakeouts. Can you help me with some apps for both?

Bob needs some help. Bounty hunting is not a desk job; Bob needs lots of information to pick up fugitives on the go. His iPhone is ideal to take along while he’s chasing bad guys, while his iPad will great for more detailed background work. Here’s what Bob needs in his apps:

Bob the bounty hunter 1

iPhone App 1

2

 ob needs a list of fugitives. He B he’s has to keep track of everyone he’s ple peo h looking for, along wit captured. He wants to be able to quickly ed display a list of just the captur fugitives.

3

362   Chapter 8

 e also needs a display of the H h detailed information about eac ted wan y’re the at wh like fugitive, and for, where they were last seen, is. nty bou ir the how much

2

3

iPad App

Details on past w hereabouts. They’ll have a lo cation and details about what the fu gitive was doing there. For research, Bo b needs the full dossier on each fugitive. Picture and details shou ld all be in the same view.  ll the informat A ion he uses in th e iPhone app, too.

tab bars and core data

Jim: OK, so he wants the entire package, iPhone and iPad. Where do we start? Frank: Well, we’re going to want to create another universal app. Joe: Right, then we can keep the logic and everything together, just like we did last time, but with different views.

Jim

Jim: OK, so what do we do first? Joe: What if we start with the iPad, write that, and then do the iPhone, since it’s smaller? Frank: Two things. First, we need to design everything. Jim: Why?

Frank

Frank: Because we want to figure out what the two views will have in common and make sure it’s set up right. Jim: Oh. Frank: Second, since the customer gets paid based on using the iPhone app, pretty sure he’s going to want that first.

Joe

Joe: But it’s smaller! I think we should do the hardest one first. Frank: Listen, we work for Bob and this is what he wants. Joe: OK, so the plan is... Jim: We’ll start with designing both views, then code up the iPhone and the backend together. Frank: Right, then we can lay the iPad view on top. Jim: OK, let’s get started.

you are here 4   363

universal benefits

pps Universal A If you’re planning on supporting both devices from the beginning, it’s best to start with an app set up to do just that. Remember, a universal app comes with all this stuff built right in: One binary file to maintain. The files will compile together and support both devices. That means that changes only need to be tracked once. UIs will be separate. Universal apps have two separate view controllers and Interface Builder files, one each for the iPhone view and the iPad view. Shared code needs device checking. Since all of the logic code will be shared (if we do our job right), there’s going to need to be more device checking than before. Instead of just 4 generations of iPhones and iPod Touches, you’ll also need to watch out for iPad-specific stuff. Universal apps are sold once, used twice. The way the App Store is set up, universal apps are sold as an app that will run on both devices. Users buy the app, put it in their iTunes libraries, and then those who have iPhones and iPads can install it on both as a native app. It may look and act differently, like iBountyHunter will, but you only get paid once.

It’s something to consider. The iPad app store is full of XD and XL versions of apps that you can charge for again.

Now for some UI work... 364   Chapter 8

tab bars and core data

Portrait Orientation

Now, what is this iPad app going to look like? Since iPad apps MUST support both landscape and portrait, you need to think about both.

Remember—the HIG wants you to have different-looking views for the different orientations...

on

Landscape Orientati

you are here 4   365

sharpen solution

t

Here’s what we came up with for this iPad app—don’t worry if yours is a bit different! Since iPad apps are expected to support both orientations differently, we’re using the Split View Controller that we used for DrinkMixer.

To show the list of fugitives, we’ll have a popover that can appear in this corner with this full list. Navigation control Fugitive Name

Fugitive ID#

Image of the fugitive

The map will have pushpins for each location known for the fugitive.

366   Chapter 8

Map of previously known locations

Bounty: This area is for notes and details about the fugitive.

When Bob selects an entry, a UIWebView will display HTMLformatted text describing the sightings for the fugitive.

We’ll add some fun UI touches, too, like a pushpin holding the description.

tab bars and core data

More UI goodness: cool page background that looks like a corkboard

For the landscape view, we’ll have the full list of fugitives displayed, since we have extra space.

Fugitive Name

Navigation control

Fugitive ID#

Bounty: Image of the fugitive

List of fugitive Names Map of previously known locations

The map will have pushpins for each location known for the fugitive.

This area is for notes and details about the fugitive.

When Bob selects an entry, a UIWebView will display HTML-formatted text describing the sightings for the fugitive.

We‘ll have the background for the text look like notebook paper.

you are here 4   367

tab bar up close

A new iPhone control Now that we’ve designed the larger interface, we need to get into the iPhone views. In some ways, designing an iPhone view after an iPad is more difficult. Smaller screen size and shorter interaction times are the key things to consider in creating this view, especially when there’s plenty of data to display.

iPhone App 1

To leverage the smaller screen, we’re going to use a new controller: the tab bar.

2

 ob needs a list of fugitives. He B he’s has to keep track of everyone he’s ple peo with g alon for, ing look captured. He wants to be able to quickly d display a list of just the capture fugitives.

3

 e also needs a display of the H h detailed information about eac ted wan ’re they fugitive, like what , for, where they were last seen is. nty bou r thei ch mu how and

Tab Bar Up Close

The Tab Bar Controller is another common iPhone interface. Unlike the Navigation Controller, there isn’t really a stack. All the views are created up front and easily accessed by clicking the tab, with each tab being tied to a specific view. Tab bars are better suited to tasks or data that are related, but not necessarily hierarchical. The UITabBarController keeps track of all of the views and swaps between them based on user input. Standard iPhone apps that have tab bar controllers include the phone app and the iPod.

The tabs themselves can contain tex or an image.t and/

368   Chapter 8

The tab bar can contain any view you need.

tab bars and core data

Take a second to go back to Bob’s requirements and your iPad UI—think about how many views you’ll need for the iPhone version and then sketch up what you think you’ll need to build.

you are here 4   369

tab bars easily access multiple views

For the iPhone app, we’re going to need three views. Using Bob’s parameters, here’s what we came up with.

1

 ob needs a list of fugitives. He keeps B track of everyone he’s looking for or has captured.

2

We’ll keep track of the e. fugitive data sorted by nam

For each list, we’ll use a table view, like we did with DrinkMixer.

 ob wants to be able to quickly display a B list of just the captured fugitives.

The quickest way to switch between different lists is with a tab bar controller.

Fugitives

Captured

list of names

list of names

Fugitives

Captured

Fugitives

Captured

With the tab bar controller, the user can click on the tab at the bottom of the screen to jump between views.

370   Chapter 8

tab bars and core data

3

 ob wants a separate display with B the detailed information about each fugitive.

For managing these data, we’re going to use a technology introduced with the 3.0 iOS SDK, Core Data. It can manage a lot of different data types for your app.

The detail view for each fugitive will be available by clicking on any name. This area is for notes and details about the fugitive. The tab bar controller will still be visible.

Q:

Mail app on an iPad. There are a lot of little touches aimed at realism.

A:

How do I keep track of integrating the iPad and the iPhone UIs together?

Why are there so many UI touches in the iPad version?

The iPad is all about eye candy, so we’re going to show you how to add some! To get an idea of the importance of realistic UI to Apple, go check out the iPad HIG. Another good way to get an idea of what they’re focused on is to go play with the

Q:

A:

Good question. Now that we have the UI worked out for the iPad, it’s a good idea to sit down and make up a list of the fields that we’ve included. Since the iPhone app is smaller, we should take the iPad list of fields and pick and choose the ones we need.

Fugitive Name

Fugitive ID#

Bounty: Fugitives

Captured

Q:

Can I embed a Navigation Control inside of a tab bar controller?

A:

Yes, you can, but NOT the other way around. If you have too much information to fit within one tab, but the contents are related, this may be the way to go.

you are here 4   371

which template?

Choose a template to start iBountyHunter This time around, we have a lot going on in our app. A universal app with a Navigation Controller, a tab bar, and Core Data, too. As we saw earlier, the only template that supports universal apps right now is the window-based app; so we’ll start there and add the tab bar and the Navigation Controller with Interface Builder and a little bit of code.

d After you pick the window-blogasebox. dia s thi get application, you

Name the product “iBou.ntyHunter”.

Select “universal” as the Device Family.

Make sure the U Core Data box isse checked.

372   Chapter 8

tab bars and core data

I just checked out the new file folders. They’re completely different than what we had earlier when we upgraded our target to be a universal app for DrinkMixer.

Yes! This app is being designed with two front ends in mind from the start. That means that we need to be smart about how we split up the functionality, with some of the logic shared for both UIs and some for each device. Let’s take a closer look...

you are here 4   373

sharing is iOS caring

There’s a different structure for universal apps Since we’re starting out building an app that will build for both devices, there’s a different structure from the beginning. Some logic will be shared between the two devices, like data management, while the UI logic will be separated for each device.

Shared logic; mostly data management for us. All the files here are for iPhone, including the view.

All the files here are for iPad.

Shared resources

Now that we have the files in place, let’s focus on the iPhone stuff first... 374   Chapter 8

tab bars and core data

Jim: OK, what do we do now? All we have is an empty view. Joe: Well, we need to add two table views, the tab bar navigation controller to switch between those views, and the detail view.

Jim

Joe

Frank: So do we need a bunch of new nib files to handle all these views and controls? Jim: Ugh. This basic template gave us nothing! Joe: It’s not so bad. I like to think of it as a blank slate. Let’s see, we can start with the tab bar and tab bar controller... Frank: Right, that will switch between the two table views for Fugitive and Captured. Those views will each need nav controllers as well, to get in and out of the detailed view. Joe: So do we need separate nibs for the tab bar and those two views? It seems like maybe we could have all those controls in just one nib, for the tab bar and the two views, since they’re basically the same. Jim: Yeah, but we’d still need view controllers, headers, and .m files for each of those views.

Frank

Joe: Yup, they’re the views that need the tables in them. We’d also need a detail view with it’s own nib and view controller, with the .h and .m files, right? Frank: That sounds about right. We can use Interface Builder to create the tab bar and navigation controllers. Joe: What do we do about the rest of the stuff ? Add new files in Xcode? Frank: That’ll work—like before, we just need to specify that the nib files are created at the same time, and we should be good to go. Jim: I think that all makes sense—it’s a lot to keep track of. Joe: Well, we’re combining like three different things now, so it’s definitely going to get more complicated! Maybe it would help to diagram how this will all fit together?

you are here 4   375

iBountyHunter bird’s-eye view

Drawing how iBountyHunter iPhone works... The main tab controller is going to be responsible for presenting a few different views to the user, and our data will be stored in a SQLite database. Putting all this info together is definitely easier if you can see how it all works.

We’ll store our data in a SQLite database.

Core Data fugitive data source

The tab bar cont us all the functio roller gives need right out ofnality we we don’t need to the box, so subclass it.

AppDelegate_ iPhone

TabBar Controller

Fugitives

ive List it g u F e h Since t ptured List arete and Ca iews, we’ll crea r table v ViewControlle UITableses. But they subclas eed their own won’t n s. nib file

Captured

Fugitives

Captured

list of names

list of names

Fugitives

Captured

Fugitives

Captured

Fugitive Name

Fugitive ID#

View Controller

Nav Controller

We’ll use a Navigation Controller to transition and from the detail view.to 376   Chapter 8

tab bar We’ll have a singt’sle responsible controller tha tween our for flipping berollers. two view cont

Bounty: Fugitives

Captured

Each of our views will have a view controller that’s responsible for fetching the appropriate data for that view.

tab bars and core data

...and how it fits with the universal app The AppDelegate_Shared class works for controlling views and data within the entire app structure. Once you get into either the iPhone or the iPad app, each one has its own AppDelegate to control the views and data within each individual app.

Contains all the Core Data setup code for hooking up to our fugitive data.

AppDelegate_ Shared

We’ll work on this fi en we get into the iPadleUIwh .

AppDelegate_ iPhone

comes Works with the datateth_Sathared from the AppDelega iPhone view. and passes it to the

Both of these files subclass the AppDelegate_Sha red

AppDelegate_ iPad

.

Now that you know how the views all fit together... you are here 4   377

cubicle conversation cont.

Jim

Joe

Joe: That helps a lot. So we only need two nibs: one to handle the controls for the tab bar switching between Fugitive and Captured views and another to handle the detail view. Frank: I get it. We need to put the table view components somewhere, and we can either create new nibs for each view and have the tab controller load them... Jim: ...or we can just include it all in one nib. Easy! Frank: Exactly. Since we don’t plan to reuse those table views anywhere else and they’re not too complicated, we can keep everything a bit simpler with just one nib. Jim: And we need view controllers for the two table views, along with the detail view. They’ll handle getting the right data, depending on which view the user is in. Frank: Plus a Navigation Controller for the table views to transition to and from the detail view. Joe: I think we’re ready to start building!

Frank

Jim: Call me picky, but I’d still like to list it all out...

o Do List iBountyHunter T s (both .h and .m er ll ro nt co ew vi ews. 1. Create and Captured vi e iv it ug F he t r files) fo view and add the ar b ab t he t e 2. Creat it, along with a to er ll ro nt co tab bar the app delegate. reference from the Controllers for 3. Add the Nav tured views. Fugitive and Cap ews for the Fugitive le vi 4. Build the tab ews. and Captured vi ew with a nib and a ail vi 5. Create a det .h and .m files. h it w er ll ro nt view co

378   Chapter 8

tab bars and core data

Q:

view?

Why are we using a tab bar controller and a table

A:

Our Fugitive data is hierarchical and lends itself well to a table view. The problem is, we have two table views: the Fugitive list and the Captured list. To support two top-level lists, we chose a tab bar.

Q:

Couldn’t you have done something similar with a toggle switch, like a UISegmentControl?

A:

Yes, we could have. It’s really a UI design choice. The two lists are really different lists, not just different ways of sorting or organizing the same data. It’s subjective, though.

Q:

A:

Well, there is a lot going on in this app, and we could have done this a different way. We could create two more nibs, each with a Nav Controller and a table view in it. Then we’d tell the Tab Bar Controller to load the first one as the Fugitive List and the second one as the Captured List. Rather than do that, since the views look the same, we just put all those controls for the list in the same nib as the tab bar. Remember, the nib is just the UI controls, not the behavior.

Q: A:

Can you use a tab bar controller for the iPad, too?

Yup, but you might not need them. With the Split View Controller (which we’ll be using), you can display both a list and some details on the same screen, so you may be able to handle your data filtering with other elements.

OK, I’m still a bit confused about the business with using just one nib for the tab controller and the two table views.

Create a new class for the Fugitive view controller in Xcode, and then add your tab bar controller in Interface Builder.

1

Create one new class with .m and .h. files. These will be the view controllers for the Fugitive List and the Captured List. We’ll start with FugitiveListViewController.h and .m for now, which needs to be a subclass of UITableViewController, so select “UITableViewController subclass” from the drop-down in the Utilities pane.

2

Add the tab bar controller. In Interface Builder, open the MainWindow_iPhone.xib to get started, and drop the tab bar controller in the view.

We’ll helpe it you tak here! from t

you are here 4   379

exercise solution

Create a new class for the Fugitive view controller in Xcode, and then add your tab bar controller in Interface Builder.

1

Create one new class with .m and .h files.

When you create these, make sure that they are UIViewController subclasses.

This needs to be aoller. UITableViewContr

...and that the “With XIB for user interface” box is NOT checked.

Here’s what your file listing should look like once you’re done.

380   Chapter 8

tab bars and core data

2

Add the tab bar controller. The window template doesn’t give us a whole lot out of the box. We’re going to use Xcode to assemble our views and view controllers the way we want them.

The template comes with an empty UIWindow. It’s the window that our app delegate will display when it starts.

While this label is cute, you should delete it.

Drag the tab bar controller from the Library into your main window listing. This will create your TabController view.

The tab bar controller comes with a tab bar and two built-in view controllers, but we’re going to change those shortly...

you are here 4   381

building the fugitive view

Build the fugitive list view We’re going to focus on the Fugitive List first, but the same steps will apply to the Captured List when we get to it. 1

Delete those two view controllers and replace them with Navigation Controllers. Since we want all the functionality that comes with a Nav Controller, delete those View Controllers and drag two new Nav Controllers in their place from the Library. Make sure they’re listed underneath the tab bar controller.

Nothing’s changed in the view—the main window listing just reflects what you’ve updated

2

Change the view controller to the FugitiveListView controller. Highlight the view controller under the first Navigation Controller and use the inspector in the utility panel to change the Class to FugitiveListViewController.

The navigation controller comes with a default UIViewContr r. We don’t want the default; olle we want it to use our Fugitive Lis t view controller.

382   Chapter 8

tab bars and core data

3

Set the names in the tabbar and navbar. To change the title for the Fugitive List view controller, double-click on the title in the nav bar and type “Fugitives”. For the tab, in the Utilities pane, change the Bar Item Title to “Fugitives”.

Updated Nav Controller title is changed with the badge item.

Use this to update the tab bar item title

em and The navigation titview the fugitive lisles both controller tit anged in the need to be ch as well. utilities panel

What’s next?

We see the warnings. Yes, there are a couple in there. Don’t worry, we’re going to fix them soon. If you want to figure out what they are, it wouldn’t hurt! you are here 4   383

checking things off your list

Next up: the Captured view You’ve just gone through and created the classes for your two table views and dropped in a tab controller to switch between the two. You’ve already checked quite a few things off your list!

Remember this from the conversation earlier?

Just do the same thing we did earlier with the Fugitives view for these two items.

384   Chapter 8

o Do List iBountyHunter T .m ers (both .h and ll ro nt co ew vi e ews. 1. Creat and Captured vi e iv it ug F he t r files) fo view and add the ar b ab t he t e 2. Creat it, along with a to er ll ro nt co tab bar the app delegate. ugitive reference from llers for the F ro nt co v na he t 3. Add ews. itive and Captured vi ews for the Fug vi le ab t he t ld 4. Bui ews. and Captured vi ew with a nib and a ail vi 5. Create a det .h and .m files. h it w er ll ro nt view co

We haven’t done this yet. That’s going to mean some code and IB work; we’ll come back to it in a minute.

tab bars and core data

BE the Developer

Your job is to be the developer and finish up the work in Xcode and Interface Builder to get the Fugitive and Captured views working with the tab bar controller. Use the To Do list from Jim, Frank, and Joe to figure out what’s left.

It’s up to you to create the captured view and then connect the views up with the tab bar controller... you are here 4   385

be the solution

BE the Developer Solution

Your job is to be the developer and finish up the work in Xcode and Interface Builder to get the Fugitive and Captured views working.

You should end up with a list that looks like this.

Create your Captured view. Follow the same steps from earlier for creating the Fugitive view.

Then wire up the tab bar controller. To do this, we need to go back to IBountyHunterAppDelegate_iPhone.h. Right now, there isn’t an outlet to connect the tab bar controller to anything, so it won’t work. You should be pretty familiar with how to do this by now. Here’s the outlet you need for a tab controller:

#import

#import “iBountyHunterAppDelegate.h”

Since the tab bar do apply to the iPad esn’t

iBountyHunterAppDelegate_iPhone.h

@interface iBountyHunterAppDelegate_iPhone : iBountyHunterAppDelegate {

UITabBarController *tabBarController_;

}

@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;

@end

Almost there... 386   Chapter 8

tab bars and core data

Here, we’ll need to wire up the App Delegate to the Tab Bar Controller.

@implementation iBountyHunterAppDelegate_iPhone

@synthesize tabController=tabBarController_;

-



(void)

dealloc {

[tabBarController_release]; [super dealloc]; }

iBountyHunterAppDelegate_iPhone.m

Clear up the warnings. The UITableViewController in XCode 4 comes with the numberOfSectionsInTableView: and numberOfRowsInSection: methods declared with #warnings so you don’t forget to set them up.

Make sure that you delete the #warning lines. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections.

}

return 1;

need to These two values specified.

be

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section.

}

return 0;

and FugitiveListViewController.m

CapturedListViewController.m

you are here 4   387

test drive

Test Drive You’ve just done a lot of work on your app—new view controllers, new Nav Controllers, table views—all from scratch. Build and run to make sure that everything’s working.

have Check and make sure that you cted sele e the iPhone Simulator schem launch sn’t in Xcode, so the iPad doe instead.

Ugh! Nothing! Why isn’t the tab bar controller (or anything else) being displayed? 388   Chapter 8

tab bars and core data

Figure out why all you see is an empty view. Look at what we did earlier in Interface Builder (shown below) and see if you can figure out what’s missing. Write what you think on the lines below. Add the tab bar controller. The window template doesn’t give us a whole lot out of the box. We’re going to use Xcode to assemble our views and view controllers the way we want them.

While this label is cute, you should delete it.

Drag the tab bar controller from the Library into your main window listing. This will create your TabController view:

you are here 4   389

no dumb questions

Q:

We have a lot jammed in our main window nib. It still seems kinda strange to me.

A:

The nib for iBountyHunter contains five controllers (the tab bar, two Nav Controllers, and our FugitiveListViewController and CapturedListViewController) and their associated components. If you’re still having trouble with the idea, it might help to open the MainWindow.xib file in Interface Builder and view it in tree mode. Expanding the hierarchy shows the structure of our app. We have a single nib with a tab bar controller, which internally has two Nav Controllers nested underneath it that are instances of FugitiveListViewController and CapturedListViewController, respectively.

Q: A:

Can I add icons to the tab bar tabs?

Absolutely. The easiest way is to pick a standard icon using Interface Builder. To do that, click on the question mark icon on

the tab you want to change, then change the Identifier in the Inspector. If you want to use a custom image, set the Identifier to custom, then select your image in the Image field (you’ll need to add it to your project, just like we did with the application icon earlier). There are a couple of peculiarities with Tab Bar icons, though: they should be 30x30, and the alpha values in the icon are used to actually create the image. You can’t specify colors or anything like that.

Q:

How many views can I have in a tabbar?

A:

As many as you want. If you add more views than can fit across the tab bar at the bottom, the UITabBarController will automatically add a “More” item and show the rest in a table view. By default, the UITabBarController also includes an Edit button that lets the user edit which tabs are on the bottom bar.

Yes, there’s a UITabBarDelegate protocol you can conform to and set as the tab bar delegate. You’ll be notified when the users are customizing the bottom bar and when they change tabs.

Q:

Why did we add a reference to the tab bar controller in the App Delegate?

A:

We’ve added the tab bar controller to the nib, but there’s a little more tweaking we’re going to have to do to get everything displaying properly. Go ahead and give it a Test Drive to see what’s going on...

Q:

Is there any way of knowing when a user switches tabs?

ƒƒ As apps get more complex, building the UI becomes more difficult. ƒƒ The hierarchy view for the xib files helps visualize how the components go together.

390   Chapter 8

A:

ƒƒ Separating the iPhone and iPad UIs is important, but the logic needs to remain consistent.

tab bars and core data

Figure out why all you see is an empty view. Look at what we did earlier in Interface Builder (shown below) and see if you can figure out what’s missing. Write what you think on the lines below.

The window template doesn’t give us a whole lot out of the box. We’re going to use Xcode to assemble our views and view controllers the way we want them.

While this label is cute, you should delete it.

Drag the tab bar controller from the Library into your main window listing. This will create your TabController view.

We need to embed the tab bar controller into the UIWindow, then it will have the subviews that it needs and will display them correctly.

The problem is that the tab bar is a top-level element in the nib. The AppDelegate has the UIWindow as its window, so the delegate will display that window. But the UIWindow doesn’t contain anything—it has no subviews.

you are here 4   391

a view within a view

A view’s contents are actually subviews All the UI components we’ve used are subclasses of UIView. By dropping them into a view, we’ve made them subviews of some bigger container view. We need to do the same thing with our tab bar. We just need to go back into the GUI and add one more link between the main view and the Tab Bar Controller.

Test Drive Link up the rootViewController outlet to the Tab Bar Controller. Then everything should be working! Interface Builder knows how to work with tableviews and Nav Controllers, so the datasource and delegate will be automatically handled.

392   Chapter 8

tab bars and core data

Test Drive It’s time to see everything working. Build and run and you can see both tab views working with tables.

Looks good! So now we need to get some data in those tables, right?

you are here 4   393

time for some data

After a quick meeting with Bob...

Here’s my list of fugitives to track. While the court is in the process of going digital, I still only have this on paper.

Managing Bob’s data is the next step. Now that the iPhone app is up and running, you need to fill in the blanks. The list is pretty simple right now, so we can make the data into any form we want and then import it.

Name: J

ID # 9

im Smile

8225

y

Descrip ti theft. on: Serial devic e Bounty: $25,00 0

394   Chapter 8

tab bars and core data

Frank: I was thinking—I’m not sure a plist is such a good idea this time. Jim: Why not? We used it for DrinkMixer, and it worked fine.

Joe

Frank: Well, this list could get pretty big—remember, the list of fugitives is going to be ongoing: the ones that Bob is trying to catch and those that he already has. Joe: So? Frank: So...a big list means lots of memory. Joe: Oh, that’s right—and the plist loaded everything every time.

Jim

Frank: Exactly. Jim: What about that Core Data thing, that’s supposed to handle large amounts of data, right? Frank: That was a new data framework introduced in iOS 3.0. That would probably work. Jim: Why use that and not just a database? Doesn’t iPhone have SQLite support?

Frank

Frank: It does, but I’m not a SQL expert, and Core Data can support all kinds of data, including SQL, but you don’t have to talk SQL directly. Joe: I thought you said we weren’t using SQLite? Frank: We are, but we’ll use Core Data to access it. Joe: How does that work? Frank: Core Data handles all the dirty work for us—we just need to tell it what data we want to load and save...

What are some other limitations with how we stored data in plists and dictionaries with DrinkMixer?

you are here 4   395

building a data factory

Core Data lets you focus on your app Loading and saving data, particularly lots of data, is a major part (often a painful one) of most applications. We’ve already spent a lot of time working with plists and moving objects in and out of arrays. But what if you wanted to sort that data in a bunch of different ways, or only see fugitives worth more than $1,000,000, or handle 100,000 fugitives? Writing code to handle that kind of persistence gets really old, really quickly. Enter Core Data...

Fugitive DB Core Data works with You define what your claobjects. (called Entities) look lik sses e.

Core Data handles th loading and saving codee...necessary

But wait, there’s more!

...and can store data in of different formats, lika number database or simple binar e in a y file.

Core Data makes loading and saving your data a snap, but it doesn’t stop there. It’s a mature framework that Apple brought over from Mac OS X to iOS in version 3.0 and gives you: The ability to load and save your objects Core Data automatically loads and saves your objects based on Entity descriptions. It can even handle relationships between objects, migrating between versions of your data, required and optional fields, and field validation. Different ways to store your data Core Data hides how your data is actually stored from your application. You could read and write to a SQLite database or a custom binary file by simply telling Core Data how you want it to save your stuff. Memory management with undo and redo Core Data can be extremely efficient about managing objects in memory and tracking changes to objects. You can use it for undo and redo, paging through huge databases of information, and more. 396   Chapter 8

But before we do any of that, we need to tell Core Data about our objects...

tab bars and core data

Core Data needs to know what to load We need Core Data to load and save the fugitive information that we need to populate our detailed view. If you think back to DrinkMixer, we used dictionaries to hold our drink information and accessed them with keys, like this:

Fugitive Name

Fugitive ID#

nameTextField.text = [drink objectForKey:NAME_KEY];

ingredientsTextView.text = [drink objectForKey:INGREDIENTS_KEY]; directionsTextView.text = [drink objectForKey:DIRECTIONS_KEY];

The problem with dictionaries and plists was that we had to store all our data by using basic types and get to this data with dictionary keys. We could have easily had a bug if we put the wrong type in the Dictionary or used the wrong key, causing lots of problems down the road. What we really want is to use normal Objective-C classes and objects where we can declare properties for the fields, use real data types, etc. You know, stuff you’re already really good at. That’s exactly what Core Data lets us do.

DrinkMixer used dictionaries to store drink information. It ty worked, but was pret primitive.

we’d These are the types ng is iti use if we were wr . th -C class in Objective

Fugitive NSString *name NSDecimalNumber bounty int fugitiveID NSString *desc

We want t typed datao, use strongly to get to th have properties use the usual at data, and oriented goo objectvalidating th dness of e data.

Dictionary

Fugitive

name = Cupid’s Cocktail

ingredients =

Cherry liqueur, peach ...

directions =

Shake ingredients and strain into...

Core Data works withs entities and propertie to give us the OO benefits we want.

Bounty: Fugitives Capt ured

We can use Core Data to populate this.

Dictionaries worked for DrinkMixer, but don’t provide any kind of type safety or encapsulation of our data. Time for something better...

We need to define our types Not only can Core Data give us the OO-based view of our data that we want, it can even define our data graphically. There’s one snag though— out of the box, Core Data supports a specific set of data types, so we need to define our entity using the types it offers... you are here 4   397

managed object model

Core Data describes entities with a Managed Object Model Entities controlled by Core Data are called Managed Objects. The way you deal with your entity descriptions (properties, relationships, type information, etc.) for Core Data is through a Managed Object Model. Core Data looks at that Managed Object Model at runtime to figure out how to load and save data from its persistent store (e.g., a database). The Xcode template we used comes with an empty Managed Object Model to get us started. Let’s take a closer look.

an empty h it w s e plate com l in the Our temd Object Mode ountyHunter Manage group called iBon that to get Shared modeld. Click .xcdata w. this vie

Fugitive

The Managed Model describesObject objects we’re go the for or try to saing to ask ve.

It also contai Data needs tonsreall the information Core from storage. ad and write this data

The template is set up so that Core Data will try to load all the Managed Object Models defined in your application at startu We’ll only need this onp.e. By default, our objec mo del is empty; we’ll need tot de fi ne the Fugitive entity.

te a Technically, you can crelea in code Managed Object ModXcode tools or by hand, but the easier. make it much, much

Let’s go ahead and create our Fugitive entity... 398   Chapter 8

tab bars and core data

Build your Fugitive entity We need to create a Fugitive entity in our Managed Object Model. Since our Fugitive doesn’t have any relationships to other classes, we just need to add properties. Open up iBountyHunter.xcdatamodeld to create the Fugitive data type. 1

 o add the Fugitive entity, click the “plus” button T all the way down here at the bottom of the window and change the name to “Fugitive”.

If we had multiple entities, you’d see the others here too, along with their relationships.

3

2

 nce the entity exists, you can O add attributes to the data model, using this plus button here.

 se these fields to edit the name U and type of the property. You should use your normal property naming convention when naming these.

Geek Bits

The property editor lets you er constraints for your properties, too—min, max,ent wh etc. We’re not going to use these ether it’s required, just yet...

You can change the Editor Style to a tree diagram style view.

you are here 4   399

no dumb questions

Q:

Why did you use an NSDecimalNumber for the bounty? Why not a float or a double?

A:

We’re going to store a currency value in the bounty field, so we want precision with the decimal part of the figure. Floats and Doubles are approximations, so you tend to get things like $9.999999998 instead of $10.00 when using them for currency calculations. Our choice of NSDecimalNumber for the bounty has nothing to do with Core Data and everything to do with what we’re trying to store.

Q:

What are the Transient and Indexed checkboxes for in Xcode when you create properties?

A:

The Transient checkbox indicates that Core Data doesn’t need to load or save that property. Transient properties are typically used to hold values that you only want to calculate once for performance or convenience reasons, but can be calculated based on the other data you save in the Entity. If you use transient properties, you typically implement a method named awakeFromFetch: that is called right after Core Data loads your Entity. In that method, you can calculate the values of your transient properties and set them.

The Indexed checkbox tells Core Data it should try to create an index on that property. Core Data can use indexes to speed



up searching for items, so if you have a property that you use to look up your entities (customer IDs, account numbers, etc.), you can ask Core Data to index them for faster searching. Indexes take up space and can slow down inserting new data into the store, so only use them when they can actually improve search performance.

Q:

I’ve seen constants declared with a “k” in front of them. Are they different somehow?

A:

Nope. It’s just a naming convention. C and C++ programmers tend to use all caps, while Apple tends to use the lowercase “k” instead.

Q:

What if I need to use a type that Core Data doesn’t support?

A:

The easiest way is obviously to try to make your data work with one of the built-in types. If that doesn’t work, you create custom types and implement methods to help Core Data load and save those values. Finally, you could stick your data into a binary type (binary data or BLOB) and write some code to encode and decode it at runtime.

Q:

What other types of persistence does Core Data support?

A:

Core Data supports three types of persistence stores on the iPhone: Binary files, SQLite DBs, and in-memory. The

SQLite store is the most useful and what we’re using for iBountyHunter. It’s also the default. Binary files are nice because they’re atomic, meaning that either everything is successfully stored at once, or nothing is. The problem with them is that in order to be atomic, the iPhone has to read and write the whole file whenever something changes. They’re not used too often on the iPhone. The in-memory persistence store is a type of store that isn’t actually ever saved on disk, but lets you use all the searching, sorting, and undo-redo capabilities that Core Data offers with data you keep in-memory.

Q:

What SQL datatypes/table structures does Core Data use when it writes to a SQLite database?

A:

The short answer is you don’t need to know. Even though it’s writing to a SQLite database, the format, types, and structures are not part of the public API and could potentially be changed by Apple. You’re supposed to treat the SQLite database as a blackbox and only access it through Core Data.

Q:

So this is a nice GUI and all, but I still don’t see what this gets us over dictionaries. It seems like a lot of work.

A:

We had to tell Core Data what kind of information we’re working with. Now that we’ve done that, we can start putting it to work.

Make sure your object model matches ours exactly! When you’re writing your own apps, there are lots of ways to set up your data model, but since we’re going to give you a database for iBountyHunter, your model must match ours exactly!

400   Chapter 8

tab bars and core data

Managed object Model Construction

Finish building the Fugitive entity in the Managed Object Model based on the Fugitive information we want to store. Remember, Core Data Types won’t match our Objective-C types exactly. Make sure you name your properties the same as we have in the Fugitive diagram shown below.

You should uncheck for “Optional”the each of s propertie we you add­—m want the all to be required.

Make sure the same pryoou use names as we perty did.

Fugitive NSString *name NSDecimalNumber bounty int fugitiveID NSString *desc

e-C he Objectiv; t e r a e s e h T ant to use types we dw to pick the you’ll nee e Data types right Cor build the entity. when you

you are here 4   401

construction solution

Managed object model Construction solution

Finish building the Fugitive entity in the Managed Object Model based on the Fugitive information we want to store. Remember, Core Data Types won’t match our Objective-C types exactly. Make sure you name your properties the same as we used in the Fugitive diagram.

Make sure that the “optional” box is unchecked for all the properties.

Check that you used the same types for your properties as we did.

If you toggle to the relationship view...

...your Fugitive entity should have four properties and no relationships.

402   Chapter 8

tab bars and core data

Core Data Up Close Core Data is about managing objects So far we’ve talked about how to describe our objects to Core Data, but not how we’re actually going to do anything with them. In order to do that, we need to take a quick look inside Core Data.

Fugitive

Inside Core Data is a stack of three critical pieces: the Managed Object Context, the Persistent Store Coordinator, and the Persistent Object Store.

The Managed where the magObject Context is keeps track ofic happens. This class (Managed Obje all the Entities has in memory cts) our application Data to load . When you need Core Managed Obje an object, you ask the ct Context fo r it...

Managed Object Context

Persistent Store Coordinator

...and if it doesn memory, it asks ’t have it in Coordinator to the Persistent Store try to find it. Persistent Object Store

All these components know how to handle our data because of the Managed Object Model.

ent Store The Persisttor’s job is to keep Coordina Persistent Object track of he Stores actually Stores. T to read and write know how. the data ds of There are different kin for each es or Persistent Object St e Core Data th type of persistence ent Object Store supports. Our Persist is a SQLite store. you are here 4   403

delegating your data duties

So, if we want to load or save anything using Core Data, we need to talk to the Managed Object Context, right?

Exactly! But, the next question is how do we get data in and out of it? The Xcode template we used set up the Core Data stack for us, but we still need to figure out how to talk to the Managed Object Context. Given what you know about Core Data so far, how would you go about asking the framework to load and save data for you?

Use SQLite commands.

Write custom save and load code to update the data.

store, but Core We’re using a SQLite kin ds of stores. Data supports otherw it uses SQLite Everything about ho ying to access it is hidden from you. Truld be dangerous. with straight SQL wo s: first, you still This has two problemda ta is actually don’t know how the type of store stored (or even the nd, one of the being used), and seco Core Data is to big reasons we’re usingd of code. avoid writing this kin

Use Core Data to generate classes to do the work for you.

404   Chapter 8

Because of our This is what we’re afteel,r!Core Data knows Managed Object Mod know to create everything it needs toall the loading and classes for us and do to ask it. saving—we just need

tab bars and core data

Xcode can create a Fugitive class from our Managed Object Model that we can use like any other class. Follow the steps below to create the Fugitive class you need.

1

Select the iBountyHunter.xcdatamodel and click on the Fugitive Entity. You need to have a Core Data entity selected before you ask Xcode to generate a class for you.

2

Create a new Managed Object Class. Select File→New→New File. There will be a new type of file that you can add, the NSManaged Object Class. Select this file and click Next. After confirming the save location for the new file (iBountyHunter should be the Group)...

3

Generate the .h and .m files. Click Create and you should have a Fugitive.h and a Fugitive.m added to your project. Go ahead and drag these up to the /Supporting Files group.

you are here 4   405

sharpen solution

Xcode can create a Fugitive class from our Managed Object Model that we can use like any other class.

1

2

Select the iBountyHunter .xcdatamodel and click on the Fugitive Entity. You need to have a Core Data entity selected before you ask Xcode to generate a class for you.

Create a new Managed Object Class... Select File→New→New File. There will be a new type of file that you can add, the NSManaged Object Class. Select this file and click Next. After confirming the save location for the new file (iBountyHunter should be the Group)...

3

Generate the .h and .m files. Click Create and you should have Fugitive.h and a Fugitive.m added to your project. Go ahead and drag these up to the /Supporting Files group.

406   Chapter 8

Make sure you selec “Core Data” under t iOS.

Now when you create a Cocoa Touch Class, you should have an option to create a Managed Object Class.

tab bars and core data

Your generated Fugitive class matches the Managed Object Model Xcode created two new files from our Fugitive entity: a Fugitive.h header file and a Fugitive.m implementation file. Open up both files and let’s take a look at what was created.

ss inherits The new Fugitive clajec it is a from NSManagedOb t— Managed Object. #import @interface Fugitive : {

NSManagedObject

}

@property (nonatomic, retain) NSDecimalNumber * bounty; @property (nonatomic, retain) NSString * name; @property (nonatomic, retain) NSString * desc;

@property (nonatomic, retain) NSNumber * fugitiveID;

erties The class has the prop s in we’d expect, but no field the class?!?!

@end

selected in our The Core Data types elwehave been mapped Managed Object Modtive-C types. to appropriate Objec

Fugitive.h

NSManagedObject handles storage and memory for generated properties The generated Fugitive class has properties for name, description, etc., but no fields in the class. The Core Data framework (and NSManagedObject in particular) are responsible for handling the memory associated with those properties. You can override this if you want, but in most cases, this does exactly what you need.

Things get even more interesting in Fugitive.m... you are here 4   407

that’s pretty handy

There’s no code in there either... but I’m guessing that I’m not going to need to worry about that?

Right! The Core Data framework takes care of it. The Fugitive.m class is nearly empty, and instead of synthesizing the properties, they’re declared with a new directive, @dynamic.

#import “Fugitive.h”

@implementation Fugitive @dynamic bounty; @dynamic name; @dynamic desc;

@dynamic fugitiveID; @end

Fugitive.m

The implementation of the Fugitive class is almost completely empty!

408   Chapter 8

tab bars and core data

NSManagedObject also implements the properties The new @dynamic directive tells the compiler not to worry about the getter and setter methods necessary for the properties. They need to come from somewhere, though, or else code is going to crash at runtime when something tries to access those properties. This is where NSManagedObject steps in again. Because NSManagedObject handles the memory for the fields backing the properties, it also provides runtime implementations for the getter and setter methods. By having NSManagedObject implement those methods, you get a number of other neat benefits:

 he NSManagedObject knows when properties are T changed, can validate new data, and can notify other classes when changes happen.

 SManagedObject can be lazy about fetching N property information until someone asks for it. For example, it does this with relationships to other objects.

You get all of this without writing a line of code!

 SManagedObject can keep track of changes to N properties and provide undo-redo support.

How do you think you’ll get Core Data to load a Fugitive from the persistent store?

you are here 4   409

NSFetchRequests

Use an NSFetchRequest to describe your search In order to tell the Managed Object Context what we’re looking for, we need to create an NSFetchRequest. The NSFetchRequest describes what kind of objects we want to fetch, any conditions we want when it fetches them (like bounty > $1,000), and how Core Data should sort the results when it gives them back. Yeah, it does all that for us.

Entity Info

NSFetchRequest

Predicate

An NSFetchR the search weeqwuest describes to execute for usant Core Data .

ype uest whatictking q e r e h t ll e p You t to look for by of data from your managed an entity del. object mo You can provide a predicate that describes conditions the entities must meet, like if you wanted only entities beginning with the letter B. We want them all, so no predicate for us.

Sort Descriptor

Ask the Managed Object Context to fetch data using your NSFetchRequest All that’s left is to ask the Managed Object Context to actually execute your NSFetchRequest. That means we’ll need a reference to a Managed Object Context. Fortunately, the template set one up for us in the App Delegate. We can get to it like this: iBountyHunterAppDelegate *appDelegate =

s criptor tellnt the s e d t r o s e Th how you wa Core Dataed before it sends data sort hese are just like it back. Tdescriptors we used the sort ixer. in DrinkM

(iBountyHunterAppDelegate*)[[UIApplication sharedApplication] delegate];

NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;

410   Chapter 8

tab bars and core data



NSFetchRequest *request = [[NSFetchRequest alloc] init];

NSEntityDescription *entity = [NSEntityDescription entityForName:@”Fugitive” inManagedObjectContext:managedObjectContext]; [request setEntity:entity];

We specify the entity by name, a Fugitive.



NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@”name” ascending:YES]; NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

[request setSortDescriptors:sortDescriptors];



[sortDescriptor release];



We want the Fugitives sorted alphabetically by name.

[sortDescriptors release];



NSError *error;



if (mutableFetchResults == nil) {

NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];

// Might want to do something more serious... NSLog(@”Can’t load the Fugitive data!”);



}



self.items = mutableFetchResults;



[request release];



[mutableFetchResults release];

naged All that’s left is to ask our Ma and ad ahe go Object Context to ’ll ask it execute our fetch request. inWean array to give us back the results s. nce ere ref and clean up our

Where do we put all this code? And where are we going to store the results? What about actually displaying the fetched data?

you are here 4   411

brain barbell solution

n UTio l O S Where do we put all this code? And where are we going to store the results? What about actually displaying the fetched data?

Since Bob is going to want to see his list as soon as his view shows up, the fetching code needs to go into viewWillAppear in FugitiveListViewController.m. As for storing the results, we’ll get back an array, but we release it right away. We need to keep a reference to that array in our view controller. In order to actually show this data, we’re going to need to implement the cellForRowAtIndexPath to pull the data from the array.

The next two exercises together will get you all the code you need to view the fetched data properly...

Let’s get all of these pieces into the app.

1

Create the mutable array to hold the fetched items. Create an array in the FugitiveListViewController called items to hold the results of the fetch. Don’t forget to synthesize the property and clean up memory.

2

Import the appropriate headers into FugitiveListViewController.m. Make sure that you #import headers for the App Delegate and the Fugitive classes into FugitiveListViewController.m.

3

Implement the fetch code inside viewWillAppear. Take what we learned on the previous couple of pages and get the fetch working. You’ll need to get the Managed Object Context from the delegate, create the fetch, and then execute it. Remember to update the code to actually hang onto the results by assigning them to the array we just created.

412   Chapter 8

tab bars and core data

Table Cell Magnets

Use the code snippets below to customize the table cells for the fugitive list.

- (NSInteger)numberOfSectionInTableView:(UITableView *)tableView {

}

return 1;

// Customize the number of rows in the table view.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {

}

// Customize the appearance of table view cells.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:

(NSIndexPath *)indexPath {



static NSString *CellIdentifier = @”Cell”;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];

if (cell == nil) {



}

cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:CellIdentifier autorelease];

// Configure the cell...

ryType cell.accesso = fugitive.name;

return cell;

Fugitive *fugitive

cell.textLabel.text

#pragma mark Table view data source

return [items count];

= UITableViewCellAccessoryDisclosureIndicator; - (NSInteger) numberOfSectionsInTableView: = [items objectAtIndex:indexPath.row];

you are here 4   413

sharpen solution

It’s a lot of code to implement, but when you’re done, Core Data will be fetching the data you need for the fugitive list.

Create the mutable array to hold the fetched items.

1

#import

@interface FugitiveListViewController : UITableViewController {



NSMutableArray *items_;

}

@property(nonatomic, retain) NSMutableArray *items;

@end

FugitiveListViewController.h

Import the appropriate headers into FugitiveListViewController.m.

2

#import “FugitiveListViewController.h”

#import “iBountyHunterAppDelegate.h” #import “Fugitive.h”

@implementation FugitiveListViewController

@synthesize items=items_;

- (void)dealloc {

[items_ release];

}

414   Chapter 8

[super dealloc];

FugitiveListViewController.m

tab bars and core data

3

Implement the fetch code inside viewWillAppear.

- (void) viewWillAppear:(BOOL)animated {

[super viewWillAppear:animated];

iBountyHunterAppDelegate *appDelegate = (iBountyHunterAppDelegate*) [[UIApplication sharedApplication] delegate]; NSManagedObjectContext *managedObjectContext = appDelegate. managedObjectContext; NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@”Fugitive” inManagedObjectContext:managedObjectContext]; [request setEntity:entity]; NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@”name” ascending:YES]; [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]]; NSError *error; NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; if (mutableFetchResults == nil) { // Handle the error; }

}

self.items = mutableFetchResults; [mutableFetchResults release]; [request release];

FugitiveListViewController.m

you are here 4   415

magnets solution

Table Cell Magnets Solution Use the code snippets below to customize the table cells for the fugitive list. data source #pragma mark Table view methods view Table mark #pragma

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {



}

return 1;

// Customize the number of rows in the table view.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {

}

return [items count]; return [items _ count];

// Customize the appearance of table view cells.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:

(NSIndexPath *)indexPath {



static NSString *CellIdentifier = @”Cell”;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];

if (cell == nil) {



}



// Configure the cell...

cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

Here’s Core Data at work. The data is stored in normal Objective-C Fugitive objects. No more magic dictionary keys here...

objectAtIndex:indexPath.row]; = [items_ Fugitive *fugitive objectAtIndex:indexPath.row]; ve = [items e *fugiti Fugitiv

cell.textLabel.text = fugitive.name; = fugitive.name; cell.textLabel.text

cell.accessoryType

= UITableViewCellAccessoryDisclosureIndicator;

return cell;}

Do this! 416   Chapter 8

To completely wire up your table view, in Interface Builder make sure that the table view in the Fugitive List has its datasource as the FugitiveListViewController.

tab bars and core data

Wow, the court got its digital program together fast! Here’s a URL for the data I’m getting.

Very cool. Let’s grab the data. Browse over to http://www.headfirstlabs .com/books/iphonedev and download iBountyHunter.sqlite. In XCode, rightclick on your iBountyHunter project and select “Add Files to iBountyHunter” and make sure it is copied into the project’s /Supporting Files directory. As you do that, think about what this means for your app. Will adding in a new database mean a bunch of refactoring, or can Core Data help out here, too?

Bob’s database is a resource We have all this code already in place to load data—it came with the Core Data template. But how do we get from there to actually loading the database?

We’ve handled the object model, the Managed Object Context, and the Fugitive Class.

Fugitive

Now we need to look at the other end. We need to connect Core Data to our Fugitive Database.

you are here 4   417

databases are resources

Back to the Core Data stack Remember the Core Data stack we talked about earlier? We’ve gotten everything in place with the Managed Object Context, and now we’re interested in where the data is actually coming from. Just like with the Managed Object Context, the template set up the rest of the stack for us.

Managed Object Context

Persistent Store Coordinator

Persistent Object Store

ck for us and The template set up the staOb ject Store, ent sist we only have one Per as is. tor ina ord Co so we can leave the is the The Persistent Object Storerea ding ly one responsible for actual That’ s a. and writing the raw dat where we need to look.

Let’s take a look at what the template set up for us in the App Delegate... 418   Chapter 8

tab bars and core data

The template sets things up for a SQLite DB The Core Data template set up the Persistent Store Coordinator to use a SQLite database named after our project. As long as the database is named iBountyHunter.sqlite, then Core Data should be ready to go.

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (__persistentStoreCoordinator != nil) { The template sets return __persistentStoreCoordinator; up to use a DB namthings the same as your pr ed } ojec

t.

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppending PathComponent:@”iBountyHunter.sqlite”]; NSError *error = nil; __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLite StoreType configuration:nil URL:storeURL options:nil error:&error]) { NSLog(@”Unresolved error %@, %@”, error, [error userInfo]);

}

abort(); } return persistentStoreCoordinator;

sistent Object The template code adds a Perfig ured with the Store to the coordinator con NSSQLiteStoreType.

iBountyHunterAppDelegate.m

Test Drive Now that the database is in place and the Persistent Object Store can be used as-is, go ahead and run the app. you are here 4   419

test drive solution

Test Drive

Where is the data? We added the database to the project. The code looks right. This all worked with DrinkMixer. What’s the deal??

Core Data is looking somewhere else. Our problem is with how Core Data looks for the database. Well, it’s actually a little more complicated than that.

420   Chapter 8

tab bars and core data

iOS Apps are read-only Back in DrinkMixer, we loaded our application data from a plist by using the application bundle. This worked great and our data loaded without a problem. But remember how we talked about how this would only work in the simulator? It’s time to sort that out. As part of iOS security, applications are installed on the device as read-only. You can access any resources bundled with your application, but you can’t modify them. The Core Data template assumes you’re going to want to read and write to your database, so it doesn’t even bother checking the application bundle. It checks the application documents directory instead.



This code will only w ork in the simulator! The code use ! d to save the plist will work miserably on fine on the si a real device mulator but . The proble apps are allo fail m is with file wed to store permissions data. We’ll ta but for now, a n d where lk go ahead with a lot more a working on th bout this in C this version. e simulator o h apter 7, T his is a perfe nly to find th e device act ct example o ually behave f things s differently.

mplate looks in threthe The Core Data te ts directory fo application documeenapplication bundle. database, not th

NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] URLByAppendingPathComponent: @”iBountyHunter.sqlite”]];

iBountyHunterAppDelegate.m

Let’s take a closer look at how those directories are set up... you are here 4   421

iOS app structure

The iPhone’s application structure defines where you can read and write For security and stability reasons, iOS locks down the filesystem pretty tight. When an application is installed, the iOS creates a directory under /User/Applications on the device using a unique identifier. The application is installed into that directory, and a standard directory structure is created for the app.

o its own Each application gets installede int is a universally directory. This directory nam ’t told what it is. unique ID (UUID) and the app isn Application Home ectory named The app itself is stored in a dir the er.app. Its resources, plists, Th unt yH unt iBo iBountyHunter.app here. is actual binary, etc., are all stored lication. directory is read-only to the app Documents The Documents and Library directories are read-write for the application and also backce.ed Library up by iTunes when the user syncs their devi This is where user data needs to go. Preferences The caches directory lasts most of the time, between launches and through updates, but you need to be able to Caches recreate it, since it isn’t backed up... tmp

too, The tmp directory is read-wraitesync. ing dur but it isn’t backed up time. This data could be deleted at any

Use the Documents directory to store user data Since most Core Data applications want to read and write data, the template sets up our Core Data stack to read and write from the Documents directory. An application can figure out where its local directories are by using the NSSearchPathForDirectoriesInDomains, just like the template does in the App Delegate: - (NSURL *)applicationDocumentsDirectory {

return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory       inDomains:NSUserDomainMask] lastObject];

}

422   Chapter 8

tab bars and core data

Copy the database to the Documents directory When the application first starts, we need to check to see if there’s a copy of the database in our Documents directory. If there is, we don’t want to mess with it. If not, we need to copy one there.

Add this!

You’ll need to declare this method in AppDelegate_ Shared.h.

-(void)createEditableCopyOfDatabaseIfNeeded { // First, test for existence - we don’t want to wipe out a user’s DB NSFileManager *fileManager = [NSFileManager defaultManager]; NSURL *documentsDir = [self applicationDocumentsDirectory]; NSURL *writableDBPath = [documentsDir URLByAppendingPathComponent:@”iBoun tyHunter.sqlite”]; BOOL dbexists = [fileManager fileExistsAtPath:writableDBPath]; if (!dbexists) { // The writable database does not exist, so copy the default to the appropriate location. NSURL *defaultDBPath = [[[NSBundle mainBundle] URLForResource:@”iBountyHunter” withExtension:@”sqlite”];

Here, we grab the master DB from our copy. application bundle; this is the read-only

NSError *error; BOOL success = [fileManager copyItemAtURL:defaultDBPath toURL:writableDBPath error:&error]; Copy it from the read-only to if (!success) { the writable directory. NSAssert1(0, @”Failed to create writable database file with message ‘%@’.”, [error localizedDescription]); } } }

- (BOOL)application:(UIApplication *)application didFinishLaunchi ngWithOptions:(NSDictionary *)launchOptions iBountyHunterAppDelegate.m { // Override point for customization after application launch. [self createEditableCopyOfDatabaseIfNeeded]; [self.window makeKeyAndVisible]; return YES; }

you are here 4   423

that’s a lot of perps

You need to uninstall the old version of your app from the simulator.



This deletes the empty database that Core Data created earlier. When you build and run again, your new code will copy the correct DB into place.

Test Drive Now that the app knows where to find the database, it should load.

All the data is in there!

424   Chapter 8

tab bars and core data

Q:

Why didn’t we have to do all of this directory stuff with the plist in DrinkMixer?

A:

We only ran DrinkMixer in the simulator, and the simulator doesn’t enforce the directory permissions like the real device does. We’d basically have the same problem with DrinkMixer on a device. The reason this was so obvious with iBountyHunter is that Core Data is configured to look in the correct place for a writable database, namely the application’s Documents directory.

Q:

How do I get paths to the other application directories?

A:

Just use NSSearchPathForDirectories InDomains but with different NSSearch PathDirectory constants. Most of them you won’t ever need; NSDocumentsDirectory is the most common. You should never assume you know what the directory structure is or how to navigate it—always look up the specific directory you want.

Q:

So what happens to the data when someone uninstalls my application?

A:

When an application is removed from a device, the entire application directory is removed, so data, caches, preferences, etc., are all deleted.

Q:

The whole Predicate thing with NSFetchRequest seems pretty important. Are we going to talk about that any more?

Q:

How about object models? Can we have more than one of those?

A: Q:

A: Q:

A:

A:

Yes! We’ll come back to that in Chapter 9.

So is there always just one Managed Object Context in an application? No, there can be multiple if you want them. For most apps, one is sufficient, but if you want to separate a set of edits or migrate data from one data source to another, you can create and configure as many Managed Object Contexts as you need.

Q:

I don’t really see the benefit of the Persistent Store Coordinator. What does it do?

Yup—in fact, we’re going to take a look at that in Chapter 9, too. Do I always have to get my NSManagedObjects from the Managed Object Context? What if I want to create a new one?

No, new ones have to be added to the context—however, you can’t just alloc and init them. You need to create them from their entity description, like this: [NSEntityDescription insertNewObjectFor EntityForName:@”Fugitive” inManaged ObjectContext:managedObjectContext].

That will return a new Fugitive instance and after that you can use it like normal.

A:

Our application only uses one Persistent Object Store, but Core Data supports multiple stores. For example, you could have a customer’s information coming from one database but product information coming from another. You can configure two separate persistent object stores and let the persistent store coordinator sort out which one is used based on the database attached.

you are here 4   425

building the detail view

Now we need to build the detail view, right?

Exactly. We have the database loading with detailed information, but the user can’t see it yet. Now, we just need to build out the detail view to display that information as well.

You’re almost done with your list!

o Do List iBountyHunter T d .m llers (both .h an ro nt co ew vi e at 1. Cre Captured views. d an e iv it ug F files) for the d the bar view, and ad ab t he t e at re 2. C a to it along with er ll ro nt co ar b tab app delegate. he t m ro f e nc re refe r the Fugitive fo s er ll ro nt co v 3. Add the na ews. and Captured vi r the Fugitive fo s ew vi le ab t 4. Build the ews. and Captured vi ew with a nib, and a ail vi 5. Create a det h .h and .m files. it w er ll ro nt co view And you definitely know how to do this...

426   Chapter 8

tab bars and core data

Building the detail view isn’t anything new for you—so get to it! Here is what you’re working with from our earlier sketch for the detail view. Create a new view controller and nib called the FugitiveDetailViewController. Lay out the nib using Interface Builder to have the fields we need. Then update the new view controller to have outlets for the fields we’ll need to set and a reference to the Fugitive it’s displaying.

e will The detail view for each fugitivname. any on g be available by clickin

Fugitive Name, ID, Boun ty, and the Bounty value sh ou ld all be labels.

Fugitive Name Fugitive ID#

This area is for notes and details about the fugitive.

The Fugitive Det be a UITextView ail should will automatically. That scrolling long cont handle ent.

Bounty: Fugitives

Captured

be readAll of the fields shouldnt users only since we don’t was. tweaking the bountie

The value of the bounty will be here.

you are here 4   427

long exercise solution

Go through and check the code, outlets, declarations, and dealloc. The files that you need for the new view are: FugitiveDetailViewController.h, FugitiveDetailViewController.m, and FugitiveDetailViewController.xib. To create them, just select File→New File and create a UIViewController file and check the box that says “With XIB for User Interface.” After that, you’ll need to move the .xib file into /iPhone directory within Xcode.

When you create the new class files, you’ll have the .m, .h, and .xib files.

428   Chapter 8

tab bars and core data

@interface FugitiveDetailViewController : UIViewController {

@private

Fugitive *fugitive_; UILabel *nameLabel_;

FugitiveDetailViewController.h

UILabel *idLabel_;

UITextView *descriptionTextView_; }

UILabel *bountyLabel_;

@property (nonatomic, retain) Fugitive *fugitive;

@property (nonatomic, retain) IBOutlet UILabel *nameLabel; @property (nonatomic, retain) IBOutlet UILabel *idLabel;

@property (nonatomic, retain) IBOutlet UITextView *descriptionTextView; @property (nonatomic, retain) IBOutlet UILabel *bountyLabel;

@end

@implementation FugitiveDetailViewController

@synthesize fugitive=fugitive_;

@synthesize nameLabel=nameLabel_; @synthesize idLabel=idLabel_;

@synthesize descriptionTextView=descriptionTextView_; @synthesize bountyLabel=bountyLabel_; - (void)dealloc {

[fugitive_ release];

FugitiveDetailViewController.m

[nameLabel_ release]; [idLabel_ release];

[descriptionTextView_ release]; [bountyLabel_ release]; }

[super dealloc];

you are here 4   429

long exercise solution

Now build the view in Interface Builder.

Start out by getting all the elements in the right spots, and then go back and customize them. To get the simulated navigation bar, in the Utilities Panel, set the Top Bar to “Navigation Bar”.

These are both labels, but change the font of the ID # to 12 pt.

The TextView needs to be upsized to 240 x 155 using the inspector. “Bounty” is a separate. label from the value

Use the inspector to change the default values of each of these elements to “Fugitive Name”, “Fugitive ID”, etc. 430   Chapter 8

tab bars and core data

Here’s the final listing of the components of the detail view.

Make sure that all the added elements are children of the main view.

These links are from File’s Owner to the appropriate view element.

you are here 4   431

geek bits

UI Geek Bits We’re going to add some spit and polish to this view. It’s fine the way it is, but here’s some iPhone coolness to add.

Add a rounded rectangular button, right on top of the UITextView.

We’ll make this button function as a border. To do that, you need to do two things: 1

I n the element listing for the view, move the button above the Text View. This will move the button behind the text.

2

 ith the button still selected, use the Utilities pane to uncheck the enabled box W (under the Attributes panel).

432   Chapter 8

tab bars and core data

Now, populate the detail view from the Fugitive List. You know how to do this from what we did earlier with DrinkMixer.

1

You should be able to figure out which one.

The other files need to know that the detail view exists. In some implementation file, you’ll need to #import FugitiveDetailViewController.h.

2

The detail view needs to be displayed. In that same implementation file, the table view needs some selection code. It’ll be similar to the code that we used in DrinkMixer.

3

The fields need to be populated with the data. The detail view code needs to populate the existing fields with the data from the fields that were set up with the Fugitive.h and Fugitive.m classes and the Core Data code. In viewWillAppear:

These are just a couple fugitiveNameLabel.text of examples but should fugitiveIdLabel.text = give you all the hints you’ll need.

= fugitive.name;

[fugitive.fugitiveID stringValue];

you are here 4   433

populate the detail view

Now, populate the detail view. You know enough from before to do this.

1

Just add this to the top of the e. FugitiveListViewController.m fil 2

The other files need to know that the detail view exists. In some implementation file, you’ll need to #import FugitiveDetailViewController.h

The detail view needs to be displayed. In that same implementation file, the table view needs some selection code. It’ll be similar to the code that we used in DrinkMixer.

#pragma mark - Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

FugitiveDetailViewController *detailViewController = [[FugitiveDetailViewController alloc] initWithNibName:@”FugitiveDetailV iewController” bundle:nil]; detailViewController.fugitive = [items_ objectAtIndex:indexPath. row];

[self.navigationController pushViewController:detailViewController animated:YES];

}

View Controller Here, we tell the detaldil display. which fugitive it shou

[detailViewController release];

FugitiveListViewController.m

434   Chapter 8

tab bars and core data

3

The fields need to be populated with the data.

#import “FugitiveDetailViewController.h” @implementation FugitiveDetailViewController

@synthesize fugitive, fugitiveNameLabel, fugitiveIdLabel, fugitiveDescriptionView, fugitiveBountyLabel;

- (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated];

Adding the stringValue on the end of these two declarations handles the fact that they were not strings, but NSNumber and NSDecimalNumbers.

self.nameLabel.text = fugitive_.name;

self.idLabel.text = [fugitive_.fugitiveID stringValue]; self.descriptionTextView.text = fugitive_.desc; }

self.bountyLabel.text = [fugitive_.bounty stringValue];

FugitiveDetailViewController.m

you are here 4   435

test drive

Test Drive After populating the detail view, you can see the information about each fugitive.

The back button is working thanks to the nav control.

Now an entry can be selected in the view.

The detail view is fully populated.

It all works!

436   Chapter 8

The labels have been replaced with values from the database.

tab bars and core data

It works great! Having all that information with me makes it much easier to catch outlaws. I should be able to almost double my business with this app!

Great! After a couple of weeks, Bob is back with a new request...

That really worked! I’ve caught a ton of people already! How can I keep track of who I’ve caught? And when do I get the iPad app?

To be continued...

you are here 4   437

coredata cross

CoreData cross

Untitled Puzzle

There’s a lot of terminology with Core Data; let’s make sure you remember it!

Header Info 1 Header Info 2 etc...

1

2

3

4

5

6

7

8

9

10 11

Across

Down

2. Each app has a ____________ directory. 5. NS______Descriptor captures how data should be sorted. 6. In the middle of the Core Data stack is the Persistent Store _________. 7. The ______________ template is pretty basic. 8. ___________ can manage different types of data. 10. The managed object __________ is the top of the Core Data stack. 11. NSFetch__________ describes a search.

1. The Persistent Object Store is at the ___________ of the Core Data stack. 3. Core Data has ______ and redo. 4. The __________ controller is good for switching views. 9. The managed ______________ model describes entities.

438   Chapter 8

tab bars and core data

Match each field we need to implement for the data view to its Core Data type.

Field for the Detail View

Name

Core Data Type

Int32

String

Bounty

Boolean

Fugitive ID#

A 32 bit integer

Equivalent to an NSString attribute A BOOL value (YES or NO)

Decimal

Description

Date

A fixeddecimal nupoint mber

you are here 4   439

who are you?

A bunch of Core Data code in full costume are playing a party game, “Who am I?” They’ll give you a clue—you try to guess who they are based on what they say. Assume they always tell the truth about themselves. Fill in the blanks to the right to identify the attendees.

Who a

m I?

Tonight’s attendees: Managed Object Model, NSManagedObject, Managed Object Context, NSFetchRequest, and NSSortDescriptor. Any of the charming items you’ve seen so far just might show up!

Name

Describes the search you want to execute on your data. Includes type of information you want back, any conditions the data must meet, and how the results should be sorted.

Responsible for keeping track of managed objects active in the application. All your fetch and save requests go through this.

Captures how data should be sorted in a generic way. You specify the field the data should be sorted by and how it should be sorted.

Describes entities in your application, including type information, data constraints, and relationships between the entities. A Objective-C version of a Core Data entity. Subclasses of this represent data you want to load and save through Core Data. Provides the support for monitoring changes, lazy loading, and data validation.

440   Chapter 8

tab bars and core data

Untitled Solution Puzzle CoreData cross

Header Info 1 Header Info 2 etc...

So, did you remember all those words?

1

B

O 2

D

O

C

3

U M E

N

N 4 6

C

O

O

R

D

I

N

T

A

T

S

T 5

D

S

T

O

R

A

S

E

D

R

E

D

A

T

A

O

N

T

E

X

T

O

R

T

M

B 7

W

I

N

D

O W S

B A

8

C

9

O B J E

10

C

11

R

E

Q

U

E

S

T

Across

Down

2. Each app has a ____________ directory. [DOCUMENTS] 5. NS______Descriptor captures how data should be sorted. [SORT] 6. In the middle of the Core Data stack is the Persistent Store _________. [COORDINATOR] 7. The ______________ template is pretty basic. [WINDOWSBASED] 8. ___________ can manage different types of data. [COREDATA] 10. The managed object __________ is the top of the Core Data stack. [CONTEXT] 11. NSFetch__________ describes a search. [REQUEST]

1. The Persistent Object Store is at the ___________ of the Core Data stack. [BOTTOM] 3. Core Data has ______ and redo. [UNDO] 4. The __________ controller is good for switching views. [TABBAR] 9. The managed ______________ model describes entities. [OBJECT]

you are here 4   441

who does what solution

SOlUTion Match each field we need to implement for the data view to its Core Data type.

Field for the Detail View

Core Data Type

Int32

Name String

Bounty Boolean

Fugitive ID#

This will be represented as an NSNumber in Obj-C. Equivalent to an NSString attribute A BOOL value (YES or NO)

Decimal

Description

We used a fixed-pointcause decimal for Bounty be it’s a dollar value anderwerors. don’t want rounding

442   Chapter 8

Date

This is r an NSDeecpresented as Obj-C. imalNumber in This is represente NSDate in d as an Obj-C.

A bunch of Core Data code in full costume are playing a party game, “Who am I?” They’ll give you a clue—you try to guess who they are based on what they say. Assume they always tell the truth about themselves. Fill in the blanks to the right to identify the attendees. Tonight’s attendees: Managed Object Model, NSManagedObject, Managed Object Context, NSFetchRequest, and NSSortDescriptor.

Who a

m I?

n SOlUTio

Any of the charming items you’ve seen so far just might show up!

Name

Describes the search you want to execute on your data. Includes type of information you want back, any conditions the data must meet, and how the results should be sorted.

Responsible for keeping track of managed objects active in the application. All your fetch and save requests go through this.

Captures how data should be sorted in a generic way. You specify the field the data should be sorted by and how it should be sorted.

Describes entities in your application, including type information, data constraints, and relationships between the entities. A Objective-C version of a Core Data entity. Subclasses of this represent data you want to load and save through Core Data. Provides the support for monitoring changes, lazy loading, and data validation.

NSFetchRequest

Managed Object Context

NSSortDescriptor

Managed Object Model

NSManagedObject

you are here 4   443

CHAPTER 8

tab bars and core data

core data toolbox

Your Core Data Toolbox

The Data Model

You’ve got Chapter 8 under your belt and now you’ve added Core Data to your toolbox.

Tab Bars s a separate view.

n Each tab mea asks that are t h it w ll e w k Tabs wor al. not hierarchic

Core Data

Provides a stack that manages the data so you don’t have to. Can manage different types of data. Great for memory management and tracking changes.

ƒƒ Core Data is a persistence framework that offers loading, saving, versioning and undo-redo. ƒƒ Core Data can be built on top of SQLite databases, binary files, or temporary memory. ƒƒ The Managed Object Model defines the Entities we’re going to ask Core Data to work with.

444   Chapter 8

Works with entities that have properties called attributes. Can be edited directly in Xcode. Has several different data types.

ƒƒ The Managed Object Context is our entry point to our data. It keeps track of active Managed Objects. ƒƒ The Managed Object Context is part of the Core Data stack that handles reading and writing our data.

9 migrating and optimizing with Core Data

Things are changing How about this one? I just can’t seem to decide which outfit to wear...

We have a great app in the works. iBountyHunter successfully loads the data Bob needs and lets him view the fugitives easily. But what about when the data has to change? Bob wants some new functionality, and what does that do to the data model? In this chapter, you’ll learn how to handle changes to your data model and how to take advantage of more Core Data features.

this is a new chapter   445

bob wants to get paid

Bob needs documentation

To get paid, I need to be able to show who was captured when...

Bob needs to record more information. Bob has to keep track of his work so he can be paid. That means we need somewhere to store the day and time of a capture and then use that to build the Captured view...

Remember that Captured view we built in the last chapter?

How are we going to update iBountyHunter to handle the new information? 446   Chapter 9

migrating and optimizing with core data

We need to figure out how to update iBountyHunter to handle this new data. Look at each piece of our application and write what, if anything, needs to change.

iPhone Views

SQLite Database View Controllers

Managed Object Model

Where do we start?

you are here 4   447

start with the new data

We need to figure out how to update iBountyHunter to handle this new data. Look at each piece of our application and write what, if anything, needs to change.

- A spot to mark fugitives as caught. - Show the date and time of capture.

iPhone Views

- Populate the Captured list.

SQLite Database

- Add a captured flag to fugitives.

- Fill in the date and time of capture data.

- Add the captured time for the fugitive.

- Display only the captured fugitives in the Captured view.

- Add the captured date for the fugitive.

View Controllers

Managed Object Model

- Add information about the changes to the data for display in the app.

Where do we start? Since nearly everything depends on the new data

we need to add, let’s get that in our object model first; then we can update the rest.

448   Chapter 9

migrating and optimizing with core data

Everything stems from our object model So now you’ve figured out that the Fugitive entity needs a few more fields: the date and time, and something to indicate whether the fugitive has been captured. The database is built from the data model, so we can just update the data model to add the information we need. The Core Data date type includes both a date and time, so we only need two more properties on our Fugitive entity:

Since all fugitives will be either captured or not, it needs to exist for all of them.

captured - Boolean

- NOT Optional - NO by default

This provides both the date and the time.

Since this fie only exist forldthewill captured fugi it’s optional. tives,

captdate

- Date

- Optional

Update the data model with the new captured fields.

Use the data model editor to update the model with the two new fields.

After you update the model, you’ll need to delete the two old fugitive class files and generate new ones with the new fields included.

you are here 4   449

your updated model

Use the tools that Xcode comes with to quickly make those changes.

Use the Data Model Editor to update the model with the two new fields. captured - Boolean

- NOT Optional - NO by default

Make sure that your attributes match ours.

Here’s the updated entity

captdate

- Date

- Optional

After you update the model, you’ll need to delete the two old fugitive class files and generate new ones with the new fields included. @interface Fugitive : NSManagedObject { @private

}

@property (nonatomic, retain) NSDecimalNumber * bounty; @property (nonatomic, retain) NSString * name;

@property (nonatomic, retain) NSNumber * fugitiveID; @property (nonatomic, retain) NSString * desc;

@property (nonatomic, retain) NSNumber * captured; @property (nonatomic, retain) NSDate * captdate;

@end Fugitive.h

450   Chapter 9

These two fields are the new generated classes.

migrating and optimizing with core data

#import “Fugitive.h”

@implementation Fugitive @dynamic bounty;

The new fields have been added as dynamic properties, just like the earlier ones.

@dynamic name;

@dynamic fugitiveID; @dynamic desc;

@dynamic captured; @dynamic captdate;

@end Fugitive.m

Test Drive

Once you’ve made the changes, go ahead and run iBountyHunter.

It crashed! What did we miss?

you are here 4   451

data needs updating

The data hasn’t been updated If you take a close look at the console report of the crash, you can figure out what’s wrong...

This error seems to be coming from Core Data. It’s complaining that our data model isn’t compatible with the one that created the database.

The data model is different than what was used to actually write the data.

Our original Fugitive entity only had four attributes...

Core Data caught a mismatch bet ween our DB and our model We created this problem when we added new fields to the Fugitive entity. Our initial fugitive database was created with the old model, and Core Data has no idea where to get those new fields from. Rather than risk data corruption, it aborted our application with an error. That’s good, but we still need to figure out how to fix it.

452   Chapter 9

...but the cu entity has trrent Fugitive our capturedwo extra: captured fla date and g.

migrating and optimizing with core data

Data migration is a common problem Realizing you need to add new data or changing the way you store old data is a pretty common problem in application development. But just because it’s common doesn’t mean it’s easy. Core Data works hard to make sure it doesn’t corrupt or lose any data, so we’re going to have to tell it what to do with our new Fugitive entity.

Managed Object Context

is that the Our problembject Context is Managed Oget our new Fugitive looking to m the Persistent entity fro re... Object Sto

Persistent Store Coordinator

Persistent Object Store

t Store looks ita’s t c je b O t n e st ity ...so the Perdsihash value of the entthat with s e e t r u a comp retrieve and compa its DB... trying to lue of the entity in the hash va calculated This hash isally by the Core Data automatic based on attribute framework es, and relationships. names, typ

...but the DB was created with the old Fugitive Entity, so it has the old hash value. The Persistent Object Store fails to load the data, saying it can’t load what’s in that database into the new entity.

Right now, we have the old data and the new data model. How can we get the new data?

you are here 4   453

two data models

Migrate the old data into the new model We made the changes to the data model, but we need everything up and down the Core Data stack to be able to deal with those changes. In order to do that, we need to migrate the data. To migrate anything, you need to go from somewhere to somewhere. Core Data needs to have both of these data models to make data migration work for the entire stack. We need a new approach to changing the data model, besides just changing the old one. Let’s undo what we did earlier so we can load the data from the database again.



Data model Demolition In order for our data model to have a starting point and an ending point, we need to go back into iBountyHunter.xcdatamodel and remove the two new fields—for now.

Delete these 2 fields.

Then go back and check that it’s working again; you may need to delete it from the simulator first. It will save lots of time and trouble later.

Our t wo models need different versions It’s easy enough to change the data model by hand, but Core Data needs to be able to work with both the old and new data. We need to give Core Data access to both, but tell them they’re different versions of the same model. Even more importantly, we need to tell Core Data which one we consider our current version.

The Persistent Object Store needs to know that this is what we consider our current version.

This is what we started with, and the Persistent Object Store is expecting this data model. Old data model iBountyHunter .xcdatamodel

454   Chapter 9

New data model iBountyHunter 2.xcdatamodel

migrating and optimizing with core data

Xcode makes it easy to version your data model Fortunately, it’s pretty easy to create a new version of your data model using Xcode: 1

Highlight iBountyHunter.xcdatamodeld. Then go to the Editor→Add Model Version menu option. That will generate a new file under the iBountyHunter.xcdatamodeld directory called iBountyHunter 2.xcdatamodel.

2

Set the current version. Highlight the iBountyHunter.xcmodeld directory, then select the File Inspector on the Utilities Pane. Then in the Versioned Data Model section, select “iBountyHunter 2” as the Current version.

Here’s what the final file listing will look like.

3

Update the new data model. Select iBountyHunter 2.xcdatamodel and re-edit the data model to add the captdate and captured fields back in as we did before. Now the old version is preserved and the changes are where they belong.

Geek Bits Normally, you’d also need to delete and regenerate the Fugitive class, but since we made the same changes to the new file, the generated class would be the same.

How does the app map between the two versions? you are here 4   455

data migration

Jim: Ugh. I guess we need to write a bunch of migration code or something. Joe: Why? Jim: I assume we’re going to have to tell Core Data how to get from the old version of the data to the new one, right? Frank: Well, actually, I think we can do it automatically.

Jim

Jim: What?

Frank

Frank: Core Data has a feature that allows you to tell the app about both models and it can migrate the data for you. Jim: Nice! When does the data actually get migrated? Frank: Runtime, when the Persistent Object Store sees that the data is in the old format. That means that we’ll just need some code to tell iBountyHunter to actually do the migration. Joe: OK, so it looks like some of that code is auto-generated, and some of it needs to be added. Jim: This is great; so we can just change whatever we want? Frank: There are certain data changes that Core Data can handle automatically, like adding new attributes. More complex changes to the data need to be handled manually.

Joe

Joe: Yeah, it says here that we can do automatic migration if we’re adding attributes, or changing the optional status of an attribute. Jim: What about renaming? Frank: Renaming gets tricky—sometimes you can and sometimes you can’t. Joe: So, how can we migrate the data we have?

456   Chapter 9

migrating and optimizing with core data

Core Data can “lightly” migrate data Lightweight data migration is a powerful Core Data tool that allows you to cleanly update your underlying data to match a new data model without needing a mapping model. It only works with basic data changes: adding new attributes, changing a required attribute to an optional one, or making an optional attribute required with a default value. It can also handle limited renaming of attributes, but that gets trickier. Automatic data migration happens at runtime, which means that your app needs to know that it’s going to happen so that the data can be migrated. You’ll do that in the AppDelegate:

iBountyHunterAppDelegate.m - (NSPersistentStoreCoordinator *)persistentStoreCoordinator { all the Remember, by default, Core Data will load ns if (__persistentStoreCoordinator != nil) it will mea t Tha le. object models in your app bund { of ion vers ent curr the see both the old version and return __persistentStoreCoordinator; our model. } NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent :@”iBountyHunter.sqlite”]; NSError *error = nil;

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumb er numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManaged ObjectModel:[self managedObjectModel]]; if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) All we need to do to enable { lightweight migration is turn NSLog(@”Unresolved error %@, %@”, error, [error userInfo]); it on. abort();

We changed this from nil: options to pass the options to the persistentStoreCoordiator.

Test Drive

After adding the code to the app delegate, build and debug...

If you run into issues here, try Build-> Clean first, then Build and Debug. Strangely, Xcode doesn’t always properly recompile the first time you version your model, but cleaning should fix it. you are here 4   457

test drive

Test Drive

Awesome! It’s working with a whole new data model. 458   Chapter 9

migrating and optimizing with core data

The Persistent Object Store Exposed This week’s interview:

Do you really have any staying power? Head First: Hi, Persistent Object Store, mind if I call you POS for short? Persistent Object Store: I’d rather you didn’t. Just “Store” is fine. Head First: OK, Store, so I understand you’re part of the Core Data stack? Store: Yep—one of the most important parts, actually. It’s my job to read and write your actual data. Head First: Right, you’re the guy who translates into a bunch of different formats. Store: Exactly. When you use Core Data, you don’t really need to know if your data is going into a simple file or a sophisticated database. You just ask me to read and write a bunch of data and I handle it. Head First: That’s convenient. I understand you can be pretty particular, though. I hear you don’t take well to change. Store: I don’t think you’re getting the whole picture. See, it’s my job to make sure your data is loaded and saved exactly right. Head First: I get that, but still, small changes are OK, right? Store: Sure—I just need to make sure you really want me to do them. You need to tell me what data I’m looking at and then tell me how you want me to return it to you. Tell me it’s OK to infer the differences and do the mapping and I’ll take care of the rest. Head First: So do you actually migrate the data or just translate it when you load it?

Store: Oh, I actually migrate the data. Now, here’s where things get cool. Simple stores like the binary file ones just create a new file with the migrated data. But if I’m using a SQLite DB, I can usually do the migration right in place. Don’t need to load the data and the whole migration is nearly instant. Head First: Nice! I thought lightweight migration was kind of a noob’s migration. Store: Oh no, if you can let me do the migration through lightweight migration, that’s definitely the way to go. Now if you need to do something more complicated, like split an old attribute into two new ones or change the type of something, you’ll need to help me out. Head First: And people do that through code? Store: Sort of. Basically, you need to give me one more model, a mapping model. That tells me how to move your data from the old format to the new format. Head First: Hmm, OK, makes sense. I guess this applies to renaming variables too? Store: Actually, most of the time I can handle that too, as long as you tell me what the old name was. If you look at the details of an attribute in your object model, you can give me the old name of an attribute. If it’s there, and I have to do a migration, I can handle renaming, too. Head First: Wow, you’re not nearly as boring as I thought... Store: Thanks, I guess.

you are here 4   459

no dumb migration questions

Q:

How many versions of a data model can I have?

A:

As many as you need. Once you start adding versions, you’ll need to keep track of your current version so that Managed Object Model knows what you want when you ask for an entity. By keeping all the old versions around, Core Data can migrate from any prior version to the current one.

Q:

When is renaming something OK for a lightweight migration? When isn’t it?

A:

You can rename variables as long as you don’t change the type. If you rename them, click on the little wrench on the attribute properties in Xcode and specify the renaming identifier to be the old attribute. Core Data will handle the migration automatically from there.

Q:

Can I use migration to get data I have in some other format into Core Data?

A:

No. Migration (lightweight or otherwise) only works with existing Core

Data. If you have legacy data you want moved into Core Data, you’ll need to do that yourself. Typically, you just read the legacy data with your own code, create a new NSManagedObject to hold it, populate the new object, and save it using Core Data. It’s not pretty, but it works. There are a couple other approaches you can look at if you have large amounts of data to migrate or streaming data (for example, from a network feed). Take a look at the Apple Documentation on Efficiently Importing Data with Core Data for more details.

Q:

Does it make a difference if I use lightweight migration or migrate data myself?

A:

Use lightweight migration if you can. It won’t work for all cases, but, if it can be done, Core Data can optimize the migration if you’re using a SQLite store. Migration time can be really, really small when done through lightweight migration.

Q:

What do I do if I can’t use lightweight migration?

A:

You’ll need to create a mapping model. You can do that in Xcode by selecting

ƒƒ Lightweight automatic migration needs both versions of the data model before it will work. ƒƒ Automatic migration can change a SQLite database without loading the data.

460   Chapter 9

Design→Mapping Model, then picking the two models you want to map between. You’ll need to select your source entities and attributes, then select the destination entities and attributes. You can enter custom expressions to do data conversions if you need to. To find out more information on mapping models, check out the Apple Documentation on Core Data Migration.

Q:

Xcode lets me enter a hash modifier in the Versioning Settings for an attribute. What are those for?

A:

Core Data computes a hash for entities using attribute information so it can determine if the model has changed since the data store was created. However, it’s possible that you need to change the way your data is stored without actually changing the data model. For example, let’s say you always stored your time values in seconds, but then decided you needed to store milliseconds instead. You can continue to store the value as an integer but use the version hash modifier to let Core Data know that you want two models to be considered different versions and apply your migration code at runtime.

ƒƒ Migration of data happens at runtime. ƒƒ You can use lightweight migration to add variables, make a required variable optional, make an optional one required with default, and to do some renaming.

migrating and optimizing with core data

Here’s what you’ve done so far...

Views

SQLite Database

- A spot to mark fugitives as caught. - Show the date and time of capture. - Populate the Captured list. - Fill in the date and time of Capture data. - Display only the captured fugitives in the captured view.

- Add the captured flag to fugitives. - Add the captured time for the fugitive. - Add the captured date for the fugitive.

View Controllers

These are both done! Managed Object Model

- Add information about the changes to the data for display in the app.

What kind of changes do we need to make to the UI to add the captured information?

you are here 4   461

bob should keep his day job

Bob has some design input

I want all this captured info on the detail view. Here, I sketched up some ideas for you on how to do that.

Save

Cancel

I was thinking I could just type in Y or N when I capture a guy? Then fill in the date and time below.

Captured? Y/N me: Capture Date & Ti

But Bob’s sketch has some problems... 462   Chapter 9

migrating and optimizing with core data

Bob’s view needs some improving. As an experienced iOS developer, you can probably come up with some better UI designs. Time for you to help him out.

1

Can Bob’s view actually work with the app as it’s currently written? (Circle one)

2

If not, why not?

3

 o properly implement this view, you need to know what data is editable. What T data can the user edit and what is the best way to handle that input?

4

Sketch up your plan for the final detail view:

Yes

No

Don’t forget about the Tab Bar Controller down here.

you are here 4   463

a better interface

Now that you’ve thought through the design implications, what should the detail view look like?

1

Can Bob’s view actually work as is with the app as written? (Circle one)

2

If not, why not?

Yes

No

We already have a Back button where Bob wants to put a Cancel

button. Asking the user to input the “Y” or “N” and type the date and time is not a great UI. 3

 o properly implement this view, you need to know what data is editable. T What data can the user edit and what is the best way to handle input? The

only data

that will need to change is the Captured field and the Captured date and time. Since Captured is a boolean, a switch or some kind of control will work better than typing. Since Bob will hit the control when he captures the bad guy, we can just get the current date and time from iOS and save even more typing. 4

Sketch up your plan for the final detail view:

?

Save

These two fields will be labels, like before. Don’t forget about the Tab Bar Controller down here. 464   Chapter 9

Captured?

Capture Date & Time:

Date, time, info

We might need this later—we’ll have to see....

A segmented control will work great here. That will mean NO typing required to input the data.

Label with date & time info. populated by the time from when the yes/no is toggled.

migrating and optimizing with core data

Make the additions you need to the detail view to include the additional fields.

1

Open up FugitiveDetailViewController.xib in Interface Builder. Go ahead and add the visual elements you need: the three labels and the segmented control. You’ll need to add a simulated tab bar to make sure everything will fit. Don’t worry about the Save button for now.

2

In FugitiveDetailViewController.m (and .h), add properties and initialization code. Now that all those interface elements exist, give them the back end in Xcode, but don’t worry about linking them just yet...

you are here 4   465

exercise solution

Here are the additions to the view and the code to support them.

1

Open up FugitiveDetailViewController.xib in Interface Builder.

The segmented control needs to be configured (it says first/second by default).

Toggle this selection to get to the other half of the control.

This is the label that will hold the captured date and time, but it’s empty unless the switch is toggled to Yes. 2

Make sure “Segment 0 Yes” is selected and both segments are enabled. We’ll need this in a minute.

In FugitiveDetailViewController.m (and .h), add properties and initialization code.

UISegmentedControl *capturedToggle_; UILabel *capturedDateLabel_;

Add this with the other @ properties.

Add this inside the @interface.

@property (nonatomic, retain) IBOutlet UISegmentedControl *capturedToggle; @property (nonatomic, retain) IBOutlet UILabel *capturedDateLabel;

FugitiveDetailViewController. h

466   Chapter 9

migrating and optimizing with core data

@synthesize bountyLabel=bountyLabel_; @synthesize capturedToggle=capturedToggle_;

@synthesize capturedDateLabel=c apturedDateLabel_;

FugitiveDetailViewController.m - (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated];

self.nameLabel.text = fugitive_.name;

self.idLabel.text = [fugitive_.fugitiveID stringValue]; self.descriptionTextView.text = fugitive_.desc;

Convert the date to a label for the description.

self.bountyLabel.text = [fugitive_.bounty stringValue];

self.capturedDateLabel.text = [fugitive_.captdate description];

self.capturedToggle.selectedSegmentIndex = [fugitive_.captured boolValue] ? 0 : 1;

}

Set the selectedSegmentIndex based on whether they are captured: 0 = YES, 1 = NO. - (void)dealloc {

Do this!

[fugitive_ release];

[nameLabel_ release]; [idLabel_ release];

[descriptionTextView_ release]; [bountyLabel_ release];

[capturedToggle_ release];

Finally, link the capturedToggle outlet for the segmented control to File’s Owner in Interface Builder.

[capturedDateLabel_ release];

[super dealloc]; }

FugitiveDetailViewController.m

you are here 4   467

test drive

Test Drive

All the view elements look good! Now we just need to implement their behaviors...

Q:

Why didn’t we use the switch instead of the segmented control?

A:

Because there’s no Apple-sanctioned way to change the text of the switch. By default, the options are On and Off, which won’t work for us.

Q:

Why didn’t we use a checkbox for the captured field?

468   Chapter 9

A:

It turns out that the checkbox isn’t a standard control. It’s certainly surprising, since you see them so often in iOS apps.

They can be done, however, by creating a custom button with three images (an empty box, a selected box, and a checked box), and switching between them.

migrating and optimizing with core data

Toggle Code Magnets

Now that we have the controls laid out the way we want them, we need to actually give them some behavior. Use the magnets below to implement the method that will handle the segmented control switching. Then everything will be ready for linking to the segmented control in Interface Builder.

- (IBAction) capturedToggleChanged: (id) sender {

if (

) {



= [NSDate



= [NSNumber numberWithBool:YES];

self.capturedDateLabel.text = [fugitive_captdate description];

}

else {



= nil;

fugitive_.captured = [NSNumber numberWithBool:NO];



}



self.capturedDateLabel.text = [

}

x == 1

fugitive_.captured

];

te fugitive_.captda

@”” date];

self.capturedToggle.selectedSegmentIndex

fugitive_.captdate NSDate

text

== 0

you are here 4   469

toggle code magnets solution

Toggle Code Magnets Solution

Now that we have the controls laid out the way we want them, we need to actually give them some behavior. Use the magnets below to implement the method that will handle the segmented control switching. Then everything will be ready for linking to the segmented control in Interface Builder.

This will only be called if the value actually changed, so if the selected index is - (IBAction) capturedToggleChanged: (id) sender { now 0, the fugitive wasn’t capt ured prio r to this call. Remembe r , segmen ==== 0) 0) { pturedToggle.selectedSegmentIndex if ( self.ca capturedToggle.selectedSegmentIndex This will return an on our ct 0 is YES ont NSDate set to the = [NSDate date]; rol. gitive_.captdate fufugitive_.captdate current date and self.capturedDateLabel.text = [fugitive_captdate time. description];



}

= [NSNumber numberWithBool:YES]; fugitive.captured fugitive_.captured

else {



= nil; fugitive.captdate fugitive_.captdate

fugitive_.captured = [NSNumber numberWithBool:NO];

If the fugitive isn’t captured, clear the old capture date if there was one.



}



@”” self.capturedDateLabel.text = @””

}

Core Data stores booleans as vert NSNumbers, so we need to con to ues val our boolean YES/NO e. NSNumbers to update the fugitiv

This will return a text representation of an NSDate.

; NSDate

text

date];

x == 1

These were extras...

Test Drive Add the code above to FugtiveDetailViewController.m and don’t forget the corresponding declaration in the .h file: - (IBAction) capturedToggleChanged: (id) sender;

470   Chapter 9

Link the Value Changed event from the segmented control to the capturedToggleChanged and the captureDateLabel action to the empty Date & Time label in the Files’s Owner in Interface Builder.

migrating and optimizing with core data

Test Drive

Now that all that work is done, you should have a functioning detail view. Give it a try...

The view looks great and the segmented control is set to No, just like it should be.

If you toggle the segmented control, the date and time are filled in.

OK, but if I come in and out of the app, it’s not restarting. What’s the deal with that? I thought that only one app was running at a time...

That was true before iOS 4. Now developers can access multitasking behavior on iPhone. That means that apps don’t terminate when you leave them, or the phone rings, or you flip over to your iPod to change your music. So what does that mean for iBountyHunter? you are here 4   471

saving with managed object context

Your app has a lifecycle all its own... The Core Data template we used tries to save our data when our application quits (look in applicationWillTerminate). The problem is, as of iOS 4, most iOS devices support multitasking and the application lifecycle has changed. Because of the constraints of mobile hardware, the applications aren’t running in a full multitasking model like you’d see on a desktop. Instead, applications have a very specific lifecycle they go through as iOS starts and stops them.

our app At first nying... isn’t run

application:didFinishLaunchingWithOptions

Not running

icon, ps on youred, and a t r e s u a ...when is loaded, launch your app Delegate gets told your App. about it

Inactive applicationWillEnterForeground: applicationDidBecomeActive:

Suspended

ter going Briefly afbackground, into the will be your app d. It can suspende off here be killed p memory to free uned to the or retur state if the inactive ts it again. user star

When your app is visible, you’ll become active. This can also happen if the user unlocks the screen and you’re still running, for example. Background

applicationWillResignActive:

Inactive

applicationDidEnterBackground:

Once your ap background, yoplu’ication is in the it and given a shre told about prepare things ort period to for hibernation.

472   Chapter 9

Active

of Your app will spend a lot ing tt ge , ing time here—runn doing UI events, generally normal app things.

ome l hit the Hll will come il w r e s u e ca th Eventually ck the screen, or paplication that lo a , button S will warn your or it’s about to in and iO isn’t looking at it kground. the user led off to the bac be shuff

migrating and optimizing with core data

Multitasking rules of engagement Apple provides excellent documentation in the iOS Application Programming guide detailing each state transition and the corresponding method calls and notifications you’ll receive. A couple points are critical, however: You have a limited time to handle moving to the background. You have approximately 5 seconds to return from applicationDidEnterBackground before iOS takes action and suspends or kills you. At a minimum, you should save any modified user data when moving into the background. You should also save application state information you might want to restore the application to where the user left it. They shouldn’t know that your application was terminated between when they left it and when it restarted. You can request more background time if you need it. There are limited things you can do in the background state, but if you need more time or want to maintain some network connectivity, for example, you can request it in your applicationDidEnterBackground: method. Once you enter the background state, you won’t be told if you’re killed. Once you enter the background state, iOS will try to keep you in memory in the suspended state. However, applications are terminated based on resource availability, so if you can reduce memory usage, your app may run longer. Your app will not receive any further notifications before it’s killed off, which is why it’s so important to persist everything you need to start up again later as soon as you get backgrounded. If the device doesn’t support multitasking, your app will be terminated. If the device your app is running on doesn’t support multitasking (iPhone 3G, for example) or you have opted out of multitasking support, your application is simply terminated when the user leaves it. This is similar to the pre-iOS 4 behavior and you will receive an applicationWillTerminate: call before you’re shut down.

Add the save code to the right place to handle multitasking.

Copy [self saveContext] from applicationWillTerminate in iBountyHunterAppDelegate.m.

The application isn’t going to terminate anymore, it’s going to go into the background, thanks to multitasking support. Paste [self saveContext] in applicationDidEnterBackground in iBountyHunterAppDelegate.m.

you are here 4   473

always be saving

Relocate the save code to the right place to handle multitasking.

- (void)applicationDidEnterBackground:(UIApplication *)application {

}

There are some notes that came with the template that you can delete in here...

[self saveContext];

- (void)applicationWillTerminate:(UIApplication *)application {

// Saves changes in the application’s managed object context before the application terminates.

}

[self saveContext];

iBountyHunterAppDelegate.m

The Managed Object Context saves new or changed items We’ve used the managed object context to load our Fugitives, but it is also responsible for coordinating saving your data. Remember how NSManagedObject can keep track of changes to entities? The Managed Object Context can take advantage of this information to tell if you if there are any changes in the objects it’s managing. Similarly, if you create a new instance of an NSManagedObject, you need to tell it which Managed Object Context it belongs to and that Managed Object Context knows it has new entities to keep track of. The Core Data template takes advantage of this during application exit to see if the Managed Object Context has any new or changed data. If it does, the application simply asks the context to save them.

- (void)saveContext

om This template code frleg e.m is iBountyHunterAppDe as atyou exit checking for changes the app.

{

NSError *error = nil;

NSManagedObjectContext *managedObjectContext = self. managedObjectContext; if (managedObjectContext != nil) {

474   Chapter 9

if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])...

migrating and optimizing with core data

Q:

You said if I create new instances of NSManagedObjects, I need to tell them which Managed Object Context they belong to. How do I do that?

A:

It’s part of the EntityDescription we mentioned earlier. If you want to create a new instance of an NSManagedObject, you just do this: [NSEntityDescription insertNew ObjectForEntityForName:@”Fugitive” inMan agedObjectContext:managedObjectContext];. The Managed Object Context is provided right from the start.

Q:

What’s the “&error” that’s being passed to the save call?

A:

Most Core Data load/save operations point to an NSError in case something goes wrong. The “&” in Objective-C behaves just like it does in C or C++ and returns the “address of” the item. We declare a pointer to an NSError, then pass the address of that pointer into the save method in case something happens. If the save call fails, Core Data will populate that error argument with more detailed information. Keep in mind that just checking for an NSError does not work for determining if a method succeeded. For that, you should check the BOOL return value, since the error is only tracking if success was NO.

Q:

Speaking of errors, what should I do if this comes back with an error?

A:

That’s really application-specific. Depending on when you detect the problem, you can warn the user and try to recover; other times, there’s not too much you can do. For example, if the error happens during the applicationWillTerminate method, there’s not much you can do other than tell the user the save failed and possibly stash the data somewhere else.

Q:

Should I only ever call save in applicationWillTerminate?

A:

No, not at all. The Core Data template set it up this way for convenience, but you should save whenever it’s appropriate in your application. In fact, if you’re using a SQLite database backend for your data, saves are significantly faster than when we were working with plists in DrinkMixer. You should consider saving additions or changes to the data as soon as possible after they are made to try to avoid any kind of data loss.

Q:

You said Core Data could do data validation; where does that fit into all of this?

A:

At a minimum, Core Data will validate objects before they’re stored in the Persistent Store. So, it’s possible that you could get a validation error when you try to save your changes if you have invalid data in one of your managed objects. To avoid such late notice, you should validate your NSManagedObjects as close to the time of change as possible. You can explicitly validate a new NSManagedObject like this: [fugitive validateForInsert:&error]. Similarly, there are methods for validating updates and deletes. You can call these methods at any time to verify that the NSManagedObject is valid against constraints you put in the data model. If it’s not, you can notify the user and ask them to correct the problem.

Q:

What if I don’t want to save the changes in the Managed Object Context? Can I reset it?

A:

It’s easier than that—just send it the rollback: message. When a Managed Object Context is told to roll back, it will discard any newly inserted objects, deletions, and unsaved changes to existing objects. You can think of the Managed Object Context as managing transactions—changes to entities, including insertion and deletions, are either committed with a save: message or abandoned with a rollback: message.

you are here 4   475

bob’s demo

A quick demo with Bob After seeing the detailed view and all the captured stuff, Bob’s thrilled, but has one quick comment:

This is definitely way easier than what I came up with. But, um, where is that list of captured people?

After all that, we forgot to populate the Captured list!

476   Chapter 9

migrating and optimizing with core data

OK, I know how to populate the table cells and stuff—but how can I only pick captured guys?

We can use Core Data to filter our results. We already have capture information in our Fugitive data; we just need to use it to get the Captured list. We need a way to tell Core Data we only want Fugitives where the captured flag is true.

Where is a natural place to put this kind of filtering?

you are here 4   477

predicates in Xcode

Use predicates for filtering data In database languages all over the world, predicates are used to scope (or limit) a search to only find data that match certain criteria. Remember the NSFetchRequest we talked about in Chapter 8? For that we used the Entity Information and Sort Descriptor, but we haven’t needed the predicate support...until now.

Entity Info

NSFetchRequest

Predicate

An NSFetchR the search weeqwuest describes to execute for usant Core Data . Sort Descriptor

NSFetchRequest concepts are nearly identical to SQL The three major concepts in an NSFetchRequest are nearly identical to the expressions in standard SQL. Here’s what that would look like:

SQL is a language used for managing databases.

Entity Information tells Core Data the type of the data we’re searching for (and want back). For us, this is a Fugitive class.

Here’s the piece we haven’t used before. The predicate captures conditions the data must match. If it doesn’t match, it doesn’t get returned with the results. We used the Sort Descriptor to order the data alphabetically in the results.

SELECT * FROM FUGITIVES WHERE captured = 1 ORDER BY name ASC

This is similar to our Entity info.. Not ex the same, but close. actly All we need to do is provide the predicate information to our NSFetchRequest and Core Data handles the rest. We can use an NSPredicate for that... 478   Chapter 9

Here’s the SQ predicate... L

This is the clause for asort SQL comman d.

migrating and optimizing with core data

We need to set a predicate on our NSFetchRequest NSPredicate is a deceptively simple class that lets us express logical constraints on our NSFetchRequest. You use entity and attribute names along with comparison operators to express your constraint information. You can create a basic NSPredicate with a string format syntax similar to NSString, like this: NSPredicate *predicate = [NSPredicate predicateWithFormat:@”captured == YES”]; [request setPredicate:predicate]; But NSPredicates don’t stop with simple attribute comparisons. Apple provides several subclasses like NSComparisonPredicate, NSCompoundPredicate, and NSExpression, as well as a complex grammar for wildcard matching, object graph traversal, and more. You can even build complex fetch requests graphically in Xcode. For iBountyHunter, a simple attribute condition is all we need to get Bob’s view working, so we’ll create the predicate programmatically.

Time to populate the Captured view! There’s some work to get the Captured view updated to where the fugitive view is, and then a tweak to display what we need. 1

Set some captured fugitives. Build and run the old version of the app and toggle a handful of the fugitives to “captured” before making any changes. You’ll need that for testing.

2

Update the Captured view to match the Fugitive view. When we left off in Chapter 8, we hadn’t yet done the work to populate the Captured list. Since we’re just going to be filtering the data that’s in the Fugitive list, the easiest way is to start with the entire list and then add the filtering code. Don’t forget the tableview datasource and delegate methods.

3

Add the predicate code. Update your NSFetchRequest to use an NSPredicate so it only finds captured fugitives. This needs to go into the viewWillAppear method in the CapturedViewController.m.

you are here 4   479

updating the captured view

v

You should recognize the code from Chapter 8 to get the captured view working, and then add the predicate code to get the filtered data.

Just do this through the app—any 5 that you want!

1

Set some captured fugitives.

2

Update the Captured view to match the Fugitive view.

@interface CapturedListViewController : UITableViewController {

@private

}

NSMutableArray *items_;

Create the array to store the captured fugitives.

@property (nonatomic, retain) NSMutablearray *items; @end CapturedListViewController.h

#import “CapturedListViewController.h”

#import “Fugitive.h”

#import “iBountyHunterAppDelegate.h”

#import “FugitiveDetailViewController.h”

Make sure the Captured view is importing all the delegates and data that we need.

@synthesize items=items_;

Authors’ note: Step 2 code continues on pages 482–483.

480   Chapter 9

CapturedListViewController.m

migrating and optimizing with core data

- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated];

This code is exactly the same code that we used for the FugitiveListViewController.

iBountyHunterAppDelegate *appDelegate = (iBountyHunterAppDelegate*) [[UIApplication sharedApplication] delegate]; NSManagedObjectContext *managedObjectContext = appDelegate. managedObjectContext; NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@”Fugitive” inManagedObjectContext:managedObjectContext]; [request setEntity:entity]; NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@”name” ascending:YES]; [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@”captured == YES”]; 3 Add the predicate code. [request setPredicate:predicate]; NSError *error; NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; if (mutableFetchResults == nil) { // Handle the error; } self.items = mutableFetchResults; [mutableFetchResults release]; [request release]; // Since the user could have changed how many fugitives are captured, we need to reload our table view [self.tableView reloadData]; } CapturedListViewController.m

you are here 4   481

exercise solution

You should recognize the code from Chapter 8 to get the captured view working, and then the predicate code to get the filtered data. 2

Get the Captured view to match the Fugitive view (continued).

#pragma mark Table data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NS Integer)section { // Return the number of rows in the section. return [items_ count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndex Path:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @”Cell”; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier :CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellSt yleDefault reuseIdentifier:CellIdentifier] autorelease]; } // Configure the cell... Fugitive *fugitive = [items_ objectAtIndex:indexPath.row]; cell.textLabel.text = fugitive.name;

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

}

return cell;

CapturedListViewController.m

482   Chapter 9

migrating and optimizing with core data

#pragma mark - Table view delegate

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPa th *)indexPath {

FugitiveDetailViewController *detailViewController = [[FugitiveDetailViewController alloc] initWithNibName:@”FugitiveDetail ViewController” bundle:nil]; detailViewController.fugitive = [self.resultsController objectAtIndexPath:indexPath];

[self.navigationController pushViewController:fugitiveDetailVie wController animated:YES];

}

[detailViewController release];

- (void)dealloc {



}

[items_ release]; [super dealloc];

CapturedListViewController.m

Test Drive

Go ahead and fire it up—the captured view should be ready to go!

you are here 4   483

test drive

Test Drive

It works! These are the five fugitives we marked as captured. 484   Chapter 9

migrating and optimizing with core data

Hang on—you said we should be careful with memory and performance and blah blah...Now we have two arrays of fugitives and we reload them every time the view appears. It seems pretty dumb. What if we moved this code to viewDidLoad so it’s only done once per view?

True, we can make this a lot more efficient. But not by moving it to viewDidLoad. If we move the code there, we’re going to end up with two new problems. We need another solution...

What problems would we introduce if we moved the fetching code to viewDidLoad? What else could we do to improve performance?

you are here 4   485

results handling

Core Data controller classes provide efficient results handling The code for both the FugitiveListViewController and the CapturedListViewController is in viewWillAppear. The problem is that viewWillAppear gets called every time the view is shown, which means we’re reloading all the fugitives and all the captured fugitives every time, regardless of whether anything’s changed. We could move the code to viewDidLoad, but that only gets called when the views are loaded from their nibs. That causes two problems. First, if we mark a fugitive as captured, the Captured List won’t reflect that since it only loads its data once. The second problem is that viewDidLoad gets called before our applicationDidFinishLaunching, which means the views will try to get their data before the app delegate gets a chance to copy the master database in place. What we need is a better way to manage our fetched data.

Table views and NSFetchedResultsControllers are made for each other Since UITableViews are such a common component and frequently deal with large amounts of data, there’s a special Core Data class designed to support them. The NSFetchedResultsController works together with the Managed Object Context and your NSFetchRequest to give you some pretty impressive abilities: Very efficient memory usage The NSFetchedResultsController works with the NSFetchRequest and the ManagedObjectModel to minimize how much data is actually in memory. For example, even if we have 10,000 fugitives to deal with, the NSFetchedResultsController will try to keep only the ones the UITableView needs to display in memory, probably closer to 10 or 15. High performance UITableView support UITableView needs to know how many sections there are, how many rows there are in each section, etc. NSFetchedResultsController has built-in support for figuring that information out quickly, without needing to load all the data.

486   Chapter 9

Built-in monitoring for data changes We’ve already talked about how the Managed Object Context knows when data is modified. NSFetchedResultsController can take advantage of that to let you (well, its delegate) know when data that matches your fetch results is modified.

migrating and optimizing with core data

Time for some high-efficiency streamlining We need to do a little refactoring to get NSFetchedResultsController in there, but when it’s done, Bob could give us a database of 100,000 fugitives and iBountyHunter wouldn’t blink. We’re going to do this for the CapturedListViewController, but the same refactoring can be applied to the FugitiveListViewController, too. First, we need to replace our items array with an instance of an NSFetchedResultsController, like this:

We want the controller to tell us we when data changes— need to conform to its delegate protocol.

@interface CapturedListViewController : UITableViewController

{ @private

}

NSFetchedResultsController *resultsController_;

@property (nonatomic, readonly) NSFetchedResultsController *resultsController;

We’re creating a read-only property since we don’t want anyone setting it.

@end

CapturedListViewController.h

@implementation CapturedListViewController

@synthesize items=items_;

- (void)dealloc {

[resultsController_ release];

}

[super dealloc];

Remove the items array and its property. We won’t need those any longer.

Delete the reference to the items array here and release the new controller.

@end CapturedListViewController.m

Next we need to change the search to use the controller... you are here 4   487

use the controller

Create the new FetchedResultsController getter method - (NSFetchedResultsController *)resultsController { // If we’ve already initialized our results controller, just return it if (resultsController_ != nil) { tell Since the NSFetchedResultsController can ally return resultsController_; actu to need only we us when data changes, } fetch once. If we’ve already done this (the view

is being shown again), we can just bail.

// This must be the request for our results controller. // yet so we need to build it up.

We don’t have one

iBountyHunterAppDelegate *appDelegate = (iBountyHunterAppDelegate*)[[UIApplication sharedApplication] delegate]; NSManagedObjectContext *managedObjectContext = appDelegate. managedObjectContext; NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@”Fugitive” inManagedObjectContext:managedObjectContext]; [request setEntity:entity]; NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@”name” ascending:YES]; [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@”captured == YES”]; [request setPredicate:predicate]; NSFetchedResultsController with

Create and initialize the our fetch request and the Managed Object Context.

resultsController_ = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@”captured_list.cache”]; resultsController_.delegate = self;

}

NSError *error; BOOL success = [resultsController_ performFetch:&error]; if (!success) { Now instead of asking the Managed // Handle the error; Object Model to perform the fetch, } we ask the controller. [request release]; return resultsController_;

488   Chapter 9

CapturedListViewController.m

migrating and optimizing with core data

Hmm, so if we get rid of the array of Fugitives, then we’re going to have to re-implement the datasource and delegate methods too, right? My guess is we’re going to use the NSFetchedResultsController there as well?

Yes. The NSFetchedResultsController gives us everything we need to access the fetched data. In fact, it can do it a lot more efficiently.

We’ve given you the code to set up the NSFetchedResultsController. Now you need to update the tableview delegate and datasource methods to use the controller instead of the view.

1

Refactor numberOfSectionsInTableView and numberOfRowsInSection to use the controller. NSFetchedResultsController has a sections property that is an array of NSFetchedResultsSectionInfo objects. Use those to figure out how many sections there are and how many rows in each section.

2

Refactor cellForRowAtIndexPath and didSelectRowAtIndexPath to use the controller. NSFetchedResultsController makes it easy to implement these methods using its objectAtIndexPath method.

you are here 4   489

sharpen solution

Here is the final code for CapturedListViewController.m table methods.

#pragma mark Table data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

}

return [[self.resultsController sections] count];

// Customize the number of rows in the table view.

For the number of sections, we can just return the count of the sections in the controller.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section { return [[[self.resultsController sections] objectAtIndex:section] numberOfObjects];



}

// Customize the appearance of table view cells.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NS IndexPath *)indexPath {



static NSString *CellIdentifier = @”Cell”;



if (cell == nil) {



}



UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellI dentifier];

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

// Set up the cell...

Fugitive *fugitive = [self.resultsController objectAtIndexPath:indexPath]; Nothing





cell.textLabel.text = fugitive.name;



return cell;



}

fancy here—just get the Fugitive at the given indexPath.

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

CapturedListViewController.m

490   Chapter 9

migrating and optimizing with core data

#pragma mark - Table view delegate

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPa th *)indexPath {

FugitiveDetailViewController *detailViewController = [[FugitiveDetailViewController alloc] initWithNibName:@”FugitiveDetail ViewController” bundle:nil]; detailViewController.fugitive = [self.resultsController objectAtIndexPath:indexPath];

[self.navigationController pushViewController:fugitiveDetailVie wController animated:YES];

}

[detailViewController release];

One more lookup for the indexPath to get the Fugitive, and we’re all set.

- (void)dealloc {



}

[resultsController_ release]; [super dealloc];

CapturedListViewController.m

Test Drive Go ahead and run iBountyHunter to make sure the changes didn’t break anything. The views should be loading just like they were...sort of. Do some quick testing—if you mark a fugitive as captured, does he switch lists? What if you exit and come back into the app using the Home key?

you are here 4   491

test drive

Test Drive Now that you’re using the controller instead of just a predicate, the behavior of the app should be the same. But people are showing up in the Captured list even when they’re not marked as captured!

Why aren’t fugitives properly changing lists when you change their captured status? Hint: think back to the “Five-Minute Mystery” from Chapter 6...

492   Chapter 9

migrating and optimizing with core data

We need to refresh the data The fugitives aren’t properly changing lists when you change their status because we’re not refreshing the data every time the Captured list view is displayed. We need to set up the NSFetchedResultsController to let us know when things have changed so we can update the table.

Do this!

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {

}

[self.tableView reloadData];

You can add this anywhere in the CapturedListViewController.m file.

We ask the whole tableview to reload its data when we detect a change. There are more efficient means of just reloading modified rows when you have larger tableviews.

NSFetchedResultsController can check for changes Now that we’ve set up the app to work with the NSFetchedResultsController instead of just an array, we can leverage the methods embedded with the controller to help us. The view controller has built-in support for monitoring the data for changes through a delegate. We had set ourselves up as that delegate but never implemented the code to handle data changing. Having the view completely reload when it detects a change can become cumbersome if you are dealing with a large amount of data; however, the FetchedResultsController delegate also has support built-in for notifying you of the specific cell that is changed, and you can modify just that. Check Apple’s documentation for more details.

Test Drive Implement the controllerDidChangeContent method from above, and make sure everything’s working. CapturedListViewController.m

you are here 4   493

test drive

Test Drive Do the same thing you did last time, build and run, and then change the status of one of the fugitives to pull him dynamically out of the captured list.

Start with 7 captured fugitives...

...remove one from the list...

It works!

494   Chapter 9

...and she’s immediately gone!

migrating and optimizing with core data

This is awesome! The advantage I’m going to have over the competition is great, and having all that information with me means that I’ll be making way fewer trips back to the police station. Thanks!

There’s nothing like a satisfied customer!

you are here 4   495

no dumb questions

Q:

Where can I find the full syntax for NSPredicate?

A:

NSPredicate has a pretty complex syntax available for expressing constraints on your data. There’s a simple summary available in the NSPredicate class documentation, but Apple has an entire document available to help you write advanced predicates.

Q:

It seems like it would be pretty easy to make a mistake typing predicate syntax into code like that. Isn’t that sort of like embedding SQL?

A:

Yes, and Xcode can offer a lot of help here. Instead of embedding your predicates in code, you can build them graphically using Xcode’s data modeler, just like we did with the Managed Object Model. To build a predicate graphically, select an entity in Xcode, then click on the plus as though you were adding an attribute. Select “Add Fetch Request” to create a new fetch request and click Edit Predicate to bring up the graphical editor. You can name your fetch requests whatever you like. You’ll need to retrieve them in code like this:

NSFetchRequest *fetchRequest = [managedObjectModel fetchRequestFromTemplateWithName: @”capturedFugitives” substitutionVariables:[ NSDictionary dictionaryWithObject:capturedF lag forKey:@”captured”]]; Then just use that fetch request instead of one created in code. You can also use Xcode’s builder to assemble a predicate, then just cut and paste that into your code if you’d prefer to keep them there.

Q:

Reloading the whole table when data changes seems pretty inefficient. Aren’t we trying to optimize things?

A:

Yes it is, and yes, we are. There are a number of delegate methods you can implement to get finer-grained information about what’s happening with the Managed Object Context. With that information, you can find out if you just need to update a specific table view cell, insert a cell, or remove a cell. We took the easier route and just asked the table view to reload completely.

The results controller will use that file name to cache information like the number of items, number of sections, etc. It will keep an eye on the data store and regenerate the cache if something changes. You can also forcibly ask it to remove a cache, but in general, you shouldn’t need to.

Q:

Our results controller only has one section. How do I get it to split things into multiple sections?

A:

Just provide an attribute name for the sectionNameKeyPath. The NSFetchedResultsController will group your results using that attribute and return each grouping as a section. You can get really sophisticated and create a transient property if you want to group them by something you’re not actually storing in the database and calculate the value using a custom getter added to your object model.

Q:

What’s with that cache value we gave to the results controller?

ƒƒ NSFetchRequest can take an NSPredicate to filter data based on logical conditions. ƒƒ You can express NSPredicate conditions in code or using Xcode’s predicate builder.

496   Chapter 9

A:

ƒƒ NSFetchedResultsController provides highly efficient memory management and change monitoring for UITableViews ƒƒ Be careful about what you put in viewWillAppear, as it will be called every time your view is shown.

migrating and optimizing with core data

DataMigration cross Untitled Puzzle

We have some new data lingo to try out, so flex those verbal skills...

Header Info 1 Header Info 2 etc...

1

2

3

4 5

6 7

8

9

10

Across

Down

2. viewDidLoad and view_______________ both load views, but with different frequency. 5. The _________ is responsible for reading and writing data. 7. Automatic migration is called _______________ data migration. 8. To update the data, we need to ____________ it. 9. The FetchedResultsController is good at ____________ management. 10. NSFetchResultsController can ___________ for changes.

1. _____________ concepts are similar to NSFetchResults concepts. 3. ______________ are used for filtering data. 4. The new model is the current ____________. 6. The Managed Object Context saves new or _____________ items.

you are here 4   497

datamigration cross solution

DataMigration cross Solution Untitled Puzzle

We have some new data lingo to try out, so flex those verbal skills...

1

Header Info 1 Header Info 2 etc...

S

Q 2

W

I

L

L

A

3

P

P

E

A

R

R 4 5

S

T

O

R

V

E

E

D 7

R

L

S 8

M

I

G

H

T W E

I

G

C G

R

O N

I

6

A

M E

H

T

A T

E

N

T 9

C

G M O

R

S

Y

10

C

H

E

C

K

D

Across

Down

2. viewDidLoad and view_______________ both load views, but with different frequency. [WILLAPPEAR] 5. The _________ is responsible for reading and writing data. [STORE] 7. Automatic migration is called _______________ data migration. [LIGHTWEIGHT] 8. To update the data, we need to ____________ it. [MIGRATE] 9. The FetchedResultsController is good at ____________ management. [MEMORY] 10. NSFetchResultsController can ___________ for changes. [CHECK]

1. _____________ concepts are similar to NSFetchResults concepts. [SQL] 3. ______________ are used for filtering data. [PREDICATES] 4. The new model is the current ____________. [VERSION] 6. The Managed Object Context saves new or _____________ items. [CHANGED]

498   Chapter 9

migrating and optimizing with core data

You’ve got Chapter 9 under your belt and now you’ve added migrating and optimizing data to your toolbox.

Persistent Obj Store

Actually reads and writes the da ta. Does data migration, sometimes without actually needing to load the data. Uses mapping models if the change s are too much for lightweight migration.

NSFetchResultsCo n

Data Migration

Core Data can use lightweight migration to automatically make database changes. of Versioning is used to keep track the data migrations. d Lightweight migration can be use ng to add attributes or changi optional status.

Saving

The Managed Object Context handles saving new or changed items.

trollers

Maximize s memory efficienc Has highy. p e r formance support. UITableV iew Built-in data cha support for monit nges. oring

Filtering Data

Predicates are used for filtering results data. The predicate needs to be set on the NSFetchRequest.

you are here 4   499

CHAPTER 9

Your Data Toolbox

bob’s not done yet...

Hey wait! The court says I’m gonna need pictures to get paid quickly.

It’s a good thing the iPhone comes with a camera...

500   Chapter 9

10 camera, map kit, and core location

Proof in the real world I can take a perfectly fine picture with this. I don’t need a fancy iPhone...

iOS devices know where they are and what they see. As any iPhone, iPod Touch, or iPad user knows, these devices go way beyond just managing data: they can also take pictures, figure out your location, and put that information together for use in your app. The beauty of incorporating these features is that just by tapping into the tools that iOS gives you, suddenly you can import pictures, locations, and maps without much coding at all.

this is a new chapter   501

bob needs a picture

For Bob, payment requires proof Bob is working hard on getting as many fugitives off the street as he can, but to get paid he has to document his captures.

I need a picture of the arrest when it happens, and since my phone has a camera, I was thinking you might be able to help out. This is more important than the iPad app, that can wait...

That should be easy enough. Bob wants a picture of his catch and he’s going to need it to be pretty big—so let’s go ahead and put it on its own view. Those pictures will be great for advertising too, not to mention that it will speed up payment!

502   Chapter 10

camera, map kit, and core location

Here’s what the app looks like so far. Where and how should we add photos?

Your ideas go here.

you are here 4   503

a detail view worth flipping over

.

Here’s what we came up with for the photo view. It could use some design love, but it’s functional.

Space is getting a bit tight down here. If we shrink up the space for the description, we can move the capture info up and leave room for the new button.

Caught photo. You’ll need to shrink this up a bit.

i

Flip over.

Done

This is the bottom of the image. Done button to flip the picture back over.

Flip over for the detail view We can use a built-in animation for some nice usability with a little flair here. Since Bob will only want the photo after drilling down through to the detail view (what he’ll use to find his fugitive), it makes sense to stick it on the back of the detail view. This is a really common interface for the utility apps on the iPhone. Typically, there will be two views, one with an info button on it, and another that is revealed by flipping over when the info button is clicked. The flipping is just another transition that comes with UIKit. We can steal the idea to give a nice baseball-card look to our fugitive detail view.

What kind of view will you need to implement for this? Hint: You used it for DrinkMixer...

504   Chapter 10

camera, map kit, and core location

Enough planning and hints. Build the view and get it implemented!

1

Build the new CapturedPhotoViewController. That’s going to mean a view with a UIImageView and a Done button. Don’t forget the action to tie in with the button and dismiss the view.

Don’t worry about an IBOutlet for the nd. UIImageView yet; we’ll get to that in a seco

2

Start with the FugitiveDetailViewController updates. The detail view needs a new info button, and an action to trigger the new flip view. The info button is just a regular button with the Info Dark type.

Don’t forget to connect the button and the IBAction in Interface Builder!

3

Use a custom animation to show the new view when the info button is pressed. You already know how to present a modal view, but this time we want to do it with a custom animation. The animation you want to use is the UIModalTransitionStyleFlipHorizontal. Take a look at the UIViewController documentation if you’re stuck on how to use it.

you are here 4   505

long exercise solution

v

This one is a whole bunch of functionality that you added without much help! Here’s what we came up with: 1

Build the new CapturedPhotoViewController. Start by creating a new UIViewController subclass, with a .xib file to use for our image. Make sure they’re under the /iPhone folder.

@interface CapturedPhotoViewController : UIViewController {

Declare the action for the button.

}

- (IBAction) doneButtonPressed; @end

- (IBAction) doneButtonPressed { }

CapturedPhotoViewController.h

When the done button is pressed, we want the view to go away.

[self dismissModalViewControllerAnimated:YES];

CapturedPhotoViewController.m

2

Start with the FugitiveDetailViewController updates.

Add the actio respond to then buto tton press.

- (IBAction) showInfoButtonPressed;

FugitiveDetailViewController.h

#import “CapturedPhotoViewController.h” FugitiveDetailViewController.m

506   Chapter 10

camera, map kit, and core location

Use a custom animation to show the new view when the info button is pressed.

3

- (IBAction) showInfoButtonPressed {

Instantiate the view controller and open up the flip view nib.

CapturedPhotoViewController *vc = [[CapturedPhotoViewController alloc] initWithNibName:@”CapturedPhotoViewController” bundle:nil];

vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; [self presentModalViewController:vc animated:YES];

}

[vc release];

Ah ha! It’s a modal view...

Wire this up to File’s Owner, at the top of the dialog box.

FugitiveDetailViewController.m

The info button is just a UIButton configured in the inspector as an “info dark” type.

Make sure you have the simulated tab bar in here too, so it doesn’t get hidden! you are here 4   507

long exercise solution

This one is a whole bunch of functionality that you added without much help! Here’s what we came up with: 1

Build the new CapturedPhotoViewController (continued).

You can get this UIImageView right from the library

Don’t forget the button!

508   Chapter 10

camera, map kit, and core location

Test Drive Run the app and see the cool animation working!

Tapping this info button will flip the view .

This is the image view— ­ it’s empty, but not for long...

Now the views and animations are all working properly, what about the image itself? Think about the data model when you fill in the blanks below.

The UIImage will be stored in the The

and the

The The image has to come from the

need to be

again so this will work.

has to know about the image and where to display it. or the

you are here 4   509

sharpen solution

Now the views and animations are all working properly, what about the image itself? Think about the data model when you fill in the blanks below.

database

The UIImage will be stored in the

database

The

and the

data model

CapturedPhotoViewController

The

The image has to come from the

Do this!

camera

need to be

migrated

has to know about the image and where to display it. or the

photo library

You’ve migrated a database before, and you’re going to need to do it again. Just so it’s handled and out of the way, get into Xcode and do another database migration.

1

Highlight iBountyHunter 2.xcdatamodel. Then go to the Editor→Add Model Version menu option. You will have iBountyHunter 3.xcdatamodel in the iBountyHunter .xcdatamodel directory.

2

Set the current version. Highlight the iBountyHunter.xcmodeld directory, and select the File Inspector on the Utilities pane. Then in the Versioned Data Model section, select “iBountyHunter 3” as the Current version.

3

510   Chapter 10

again so this will work.

Add the new field to the new data model (iBountyHunter3) and generate the new Fugitive class. For the image, we’ll need a new attribute called “image” that is a binary data type. Then delete the old Fugitive.h and Fugitive.m files and generate new ones via the New File menu option.

Check out Cha you’re still fuzzpty er 9 if on how to do this.

camera, map kit, and core location

The way to the camera... ...is through the UIImagePickerController. Why? Because our real mission here is to pick an image (after one is taken by the camera, or from a stored one). iOS implements image selection through a picker that allows you to get your image from different places, like the camera or the photo library. The UIImagePickerController class has a lot of built-in functionality, plus it’s modal, so once you implement it, a lot of things start happening without any additional code in your app:

ill ViewController wthen to ho P ed ur t ap C r, Our ePickerControlle create a UIImag odal view. present it as a m Captured Photo View

If it received view controlleranneimage, our its fugitive with eds to update that will be pers the image time save is calle isted the next d.

CapturedPhoto ViewController

UIImage

UIImage Picker Controller View

The UIImagePickerController tells its delegate (our CapturedPhotoViewController) wh has an image, or if the user can en it The delegate gets an image the celled. same way, regardless of whether it came fro Photo Library or was just taken m the with the camera.

The view for the picker is already image and will automat written used when you prically be the controller. esent

UIImagePicker Controller

Now it’s just a matter of some syntax... you are here 4   511

ready bake code

Ready Bake Code

Here is some code you’ll need to tie the image picker together. This code goes in our CapturedPhotoViewController.m as part of the exercise on the next page.

- (void) viewWillAppear:(BOOL)animated { When the view appears, display [super viewWillAppear:animated]; the fugitive capture image if if (fugitive_.image != nil) { there is one. self.fugitiveImage.image = [UIImage imageWithData:fugitive_.image]; } } - (IBAction) takePictureButtonPressed { CapturedPhotoViewController.m NSLog(@”%@”, @”Taking a picture...”); UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.sourceType = UIImagePickerControllerSourceTypeCamera | UIImagePickerControllerSourceTypePhotoLibrary; This allows the users to edit picker.delegate = self; the photo they are choosing. picker.allowsEditing = YES; [self presentModalViewController:picker animated:YES]; The picker is displayed asynchronously. } #pragma mark - UIImagePickerControllerDelegate methods - (void) imagePickerController:(UIImagePickerController *)picker didFinishPi ckingMediaWithInfo:(NSDictionary *)info { fugitive_.image = UIImagePNGRepresentation([info objectForKey:UIImagePi ckerControllerEditedImage]); [self dismissModalViewControllerAnimated:YES]; [picker release]; Remove the picker interface and release the picker object. } - (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker { [self dismissModalViewControllerAnimated:YES]; [picker release]; } 512   Chapter 10

camera, map kit, and core location

Time to get some images! Using the code for the image picker that we gave you, as well as some of your Objective-C skills, let’s get the image going. 1

Import the Fugitive header file and declare a property for the fugitive. The CapturedPhotoViewController needs to know what fugitive it’s working with. Add a Fugitive field and property named “fugitive” to the CapturedPhotoViewController.

2

Store the image when it’s selected and update the UIImageView. You need to set the image information on the fugitive when the picker gives us an image, then make sure the UIImageView is updated when the view is shown. You’ll need an outlet for the UIImageView; then link it in Interface Builder.

3

Add the code for the UIImagePickerController in the takePictureButtonPressed action. Use the code that we gave you to finish up the UIImagePickerController. You’ll need to say our CapturedPhotoViewController conforms to the UIImagePickerControllerDelegate and UINavigationControllerDelegate protocols in order to make it the delegate.

4

Add the “Tap here to add photo” button. Using Interface Builder, you’ll need to create a button that covers the entire UIImageView and is then set behind it. Don’t forget to connect it to your takePictureButton action.

5

n, ate the buttoview re c u o y er t f A sting of the just use the dli put the image elements an he “Tap here to view under tbutton. fugitive. add photo”

Change the FugitiveDetailViewController’s showInfoButtonPressed method to set the You’ll need to pass the fugitive information along to the CapturedPhotoViewController when it’s created and before it’s pushed.

you are here 4   513

exercise solution

Here are all the pieces put together to implement the button.

#import “Fugitive.h”

1

Import the Fugitive header file and declare a property for the fugitive.

@interface CapturedPhotoViewController : UIViewController

{ @private

UIImageView *fugitiveImage_;

}

Fugitive *fugitive_;

We’ll need an outlet so we can update the UIImageView with the selected image.

@property (nonatomic, retain) IBOutlet UIImageView *fugitiveImage; @property (nonatomic, retain) IBOutlet Fugitive *fugitive; - (IBAction) doneButtonPressed;

- (IBAction) takePictureButtonPressed; @end

CapturedPhotoViewController.h @implementation CapturedPhotoViewController

@synthesize fugitiveImage=fugitiveImage_; @synthesize fugitive=fugitive_;

2

Store the image when it’s selected and update the UIImageView.

- (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; if (fugitive_.image != nil) { self.fugitiveImage.image = [UIImage imageWithData:fugitive_.image]; } } CapturedPhotoViewController.m

514   Chapter 10

camera, map kit, and core location

3

Add the code for the UIImagePickerController in the takePictureButtonPressed action.

- (IBAction) takePictureButtonPressed { NSLog(@”%@”, @”Taking a picture...”); UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.sourceType = UIImagePickerControllerSourceTypeCamera | UIImagePickerControllerSourceTypePhotoLibrary; picker.delegate = self; picker.allowsEditing = YES; [self presentModalViewController:picker animated:YES]; } #pragma mark - UIImagePickerControllerDelegate methods - (void) imagePickerController:(UIImagePickerController *)picker didFinishP ickingMediaWithInfo:(NSDictionary *)info { fugitive_.image = UIImagePNGRepresentation([info objectForKey:UIImagePi ckerControllerEditedImage]); [self dismissModalViewControllerAnimated:YES]; [picker release]; } - (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker { [self dismissModalViewControllerAnimated:YES]; [picker release]; } - (void)dealloc

{

}

[fugitiveImage_ release]; [fugitive_ release]; [super dealloc];

you are here 4   515

exercise solution

Here’s all of the pieces put together to implement the button. 4

Add the “Tap here to add photo” button. Drag and drop the button from the Utilities panel. To keep them in the right order in the view, make sure the button is listed above the image view in the listing.

Change the type to “custom”.

Change the default text to “Tap here to add a photo”; since it’s behind the picture, you’ll only see it if there’s no image. This is a little tough to make out, but the button is behind the UIImageView.

Change the FugitiveDetailViewController’s showInfoButtonPressed method to set the fugitive.

5

- (IBAction) showInfoButtonPressed {

CapturedPhotoViewController *vc = [[CapturedPhotoViewController alloc] initWithNibName:@”CapturedPhotoViewController” bundle:nil];

vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

vc.fugitive = self.fugitive;

[self presentModalViewController:vc animated:YES];

}

[vc release];

property We just need to set the fugitive on the new ller. ntro wCo we added to the CapturedPhotoVie FugitiveDetailViewController.m

516   Chapter 10

camera, map kit, and core location

Test Drive Build and run to see your new picture view in action.

What does this mean?

Argh! It crashed!

What’s wrong? Think outside the simulator here...

you are here 4   517

the iPod Touch is different

The simulator doesn’t have a camera!

Right! And neither do some iPod Touches and iPads. The simulator is reacting to the fact that you are asking for the camera and it doesn’t have one. But more than the simulator not having the camera, some iPod touches and iPads don’t either. Who cares? Apple.

The iPhone isn’t the only device using apps One of the things that Apple requires when you release an app is that it can work on all devices you claim it can, even if a particular device lacks certain hardware capabilities. Part of the approval process for apps is that they are checked for compatibility with the iPod Touch and iPad. All this means that you need to be aware of when your app may be straying into areas where an iPhone behaves differently than other devices.

How many differences are there, really? 518   Chapter 10

camera, map kit, and core location

Pool Puzzle

Your job is to take items from the pool and place them into the list for the iPhone, iPad, or iPod Touch. You may use the same item more than once. Your goal is to make a complete list of the functionality for the iPhone, iPad, and iPod Touch.

iPod Touch

iPhone

iPad

Note: Each thing from the pool can be used more than once!

Cell phone May have Video viewing Camera Camera Magnetometer External speaker May have GPS Run iPhone and Runs most Limited location iPad apps Accelerometer Wi-Fi apps GPS Video recording iPod

you are here 4   519

pool puzzle solution

Pool Puzzle Solution

Your job is to take items from the pool and place them into the list for the iPhone, iPad, or iPod Touch. You may use the same item more than once. Your goal is to make a complete list of the functionality for the iPhone, iPad, and iPod Touch.

iPod Touch

iPhone

iPod

iPod

Runs most apps

Runs most apps

Video viewing

Video viewing

Limited location

GPS

Accelerometer Wi-Fi May have Camera

You can get some info about location from Wi-Fi.

This one can be an issue.

520   Chapter 10

This list will change.

Apple is always coming out with new devices and updating capabilities. You need to check! iPad

You may have noticed some random stuff on this list— who would’ve thought about the speaker?

iPod Runs iPhone and iPad apps Video viewing

Accelerometer

May have limited location

Wi-Fi

Accelerometer

Cell phone

Wi-Fi

Camera External speaker Video recording Magnetometer

Note: Each thing from the pool can be used more than once!



Only on the 3GS and newer

May have GPS May have Camera External speaker Video recording

camera, map kit, and core location

There’s a method for checking With all these little things that can be different between devices, pretty much every time you go to use something from the device, you need to check and see if it’s there. For the camera, the UIImagePickerController has a method to check. You need to be careful to check for specific capabilities for a device, too. For example, we only use still image capture, but if you wanted to do video capture, you need to check that not only does the device have a camera, but also that it’s capable of video capture. If you’re trying to capture HD video, you need to make sure it can do that, too. For example, the iPhone 3GS can capture video, but not at the same resolution as the iPhone 4.

[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]

from Since we’re getting the infoand see ck a source, we need to che re. the is if the source you want In our case, we have another option: the photo library. If there’s no camera, we can get an image from there instead. If you’re writing an application that cannot do something without a camera and the check fails, you need to disable that feature (and potentially hide the controls so the user isn’t confused trying to enable that feature). If your application cannot work at all without a camera (or net access or accelerometer, etc.), you can specify device capabilities for your application so users without those capabilities can’t even install your application on their device.

So what happens when the user taps the “Take a photo” button? You check for the camera, then what? What’s the user flow?

you are here 4   521

action sheets

Prompt the user with action sheets Action sheets slide up from the bottom of the page and give the user options to proceed. It’s similar to a modal view because the user has to address the action sheet before she can move on to anything else. Action sheets are really straightforward to use: they take strings for their buttons and have built-in animations for appearing and disappearing. Our code for the action sheet has some standard stuff included:

This button woulind get highlighted ve red. We don’t ha one.

First, allocate the action

UIActionSheet *photoSourceSheet = sheet, and pass it a title. [[UIActionSheet alloc] initWithTitle: All action sheets need a cancel @”Select Fugitive Picture” button, so you can dismiss it, delegate:self cancelButtonTitle:@”Cancel” like modal views. just destructiveButtonTitle:nil otherButtonTitles:@”Take New Photo”, @”Choose Existing Photo”, nil];

Declare the other two buttons and you’re done.

[photoSourceSheet showInView:self.view];

Unlike the oller, we UIImagePickerContrsheet release the action immediately.

[photoSourceSheet release];

Action sheets frequently have a “Yes, I know will delete all my stuff. Please do it” button,this which is the destructive button.

Use action sheets to let the user pick the image source We know that our options are to use the camera, use the photo library, or cancel, so we’ll need to implement the behavior for each option.

Go to the camera, take a picture, and then come back and put your new image into the Fugitive. Once you hand off to UIImagePickerControllerSourceTypeCamera, it’ll handle the rest.

Go to the photo library, pick an image, and then come back and stuff that image into the Fugitive. Here, the UIImagePickerControllerSourceTypePhotoLibrary handles the rest. Go back to the image view. 522   Chapter 10

camera, map kit, and core location

Time to implement the action sheet. There’s a lot here to think about since we’re changing the flow of the app a bit.

Implement the delegate methods for the action sheet. Here’s enough to get you started. Think about the options for case 1 and the default, and make sure you release the picker and present the view. Also don’t forget to declare the UIActionSheetDelegate in the header file.

1

- (void) actionSheet:(UIActionSheet *)actionSheet didDismissWithB uttonIndex:(NSInteger)buttonIndex { if (buttonIndex == actionSheet.cancelButtonIndex) {

NSLog(@”%@”, @”The user cancelled adding an image.”);

}

return;

UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self;

picker.allowsEditing = YES; switch (buttonIndex) { case 0:

NSLog(@”%@”, @”User wants to take a new picture.”);

picker.sourceType = UIImagePickerControllerSourceTypeCamera; break;

2

3

Modify the takePictureButtonPressed action in CapturePhotoViewController.m to include the action sheet. iBountyHunter needs to check for the camera, and if there is one, the user gets to pick whether to use the camera or an existing picture. If not, the app should just go straight into the photo library.

This is where the action sheet comes in.

Make your code readable! We divvied up the implementation code into three #pragmas: the takePictureButton code, the UIImagePickerController code, and the action sheet delegate methods.

you are here 4   523

sharpen solution

The action sheet should be ready to go and your app has a good user flow now...

Implement the delegate methods for the action sheet.

1

CapturedPhotoViewController.h @interface CapturedPhotoViewController : UIViewController

- (void) actionSheet:(UIActionSheet * ) actionSheet didDismis sWithButtonIndex:(NSInteger)buttonIndex { if (buttonIndex == actionSheet.cancelButtonIndex) { NSLog(@”%@”, @”The user cancelled adding an image.”);

}

return;

UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self;

picker.allowsEditing = YES; switch (buttonIndex) { case 0:

NSLog(@”%@”, @”User wants to take a new picture.”);

picker.sourceType = UIImagePickerControllerSourceTypeCamera; t

break;

case 1: NSLog(@”%@”, @”User wants to use an existing picture.”); picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; break; } [self presentModalViewController:picker animated:YES]; CapturedPhoto } ViewController.m

524   Chapter 10

camera, map kit, and core location

Modify the takePictureButtonPressed action in CapturePhotoViewController.m to include the action sheet.

2

- (IBAction) takePictureButtonPressed {

NSLog(@”%@”, @”Taking a picture...”);

Change this to SourceTypePhotoLibary iftheyou want to see the action sheet working on simulator.

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTy peCamera]) {

NSLog(@”%@”, @”This device has a camera. want to use.”);

Asking the user what they

UIActionSheet *photoSourceSheet = [[UIActionSheet alloc] initWithTitle:@”Select Fugitive Picture” delegate:self cancelButtonTitle:@”Cancel” destructiveButtonTitle:nil otherButtonTitles:@”Take New Photo”, @”Choose Existing Photo”, nil]; [photoSourceSheet showInView:self.view];

}

[photoSourceSheet release];

else { // No camera, just use the library. init];

UIImagePickerController *picker = [[UIImagePickerController alloc] picker.sourceType =

picker.delegate = self;

UIImagePickerControllerSourceTypePhotoLibrary;

picker.allowsEditing = YES;

[self presentModalViewController:picker animated:YES]; }

}

CapturedPhotoViewController.m

Does it work? you are here 4   525

test drive

Test Drive Fire up iBountyHunter and drill down through a fugitive to the point of taking a picture. If you’ve used the SourceTypePhotoLibrary in the takePictureButtonPressed code, you’ll get everything to work and see the action sheet.

Your simulator will not have images unless you install them. The easiest way to do that is to drag and drop photos onto the simulator, then click and hold on them in Safari and save the image.

The action sheet pops up, and once you select choose the existing photo...

...you get launched into the photo library and you can select a photo.

Geek Bits It might be time to register with Apple’s Developer Program. If you do, you can install the app on your actual iPhone and test it yourself. Check out the appendix at the end of the book to help you walk through the provisioning process to make it work.

526   Chapter 10

camera, map kit, and core location

Q:

Don’t newer iPhones and iPads support video now? How do I get to that?

A:

It’s another media type you can access when you use the UIImagePickerController. By default, it uses still images, which is what we want for iBountyHunter.

Q:

What about the whole augmented reality thing with the camera? Can I do something like that?

A:

Yes. You can give the UIImagePickerController a custom overlay view to use if it invokes the camera. There are still limitations on what you can actually do in the camera view, but you can overlay it with your own information if you want.

Q:

What’s with the allowEditing thing we turned on in the UIImagePickerController?

A:

The picker controller has built-in support for cropping and zooming images

if you want to use it. The allowEditing flag controls whether or not the users get a chance to move and resize their image before it’s sent to the delegate. If you enable it, and the user tweaks the image, you’ll be given editing information in the callback.

Q:

Do we really have to worry about the devices without cameras?

A:

Yes. When you submit your application to Apple for inclusion in the iTunes App Store, you specify the devices your application works with. If you say it works, Apple will test it on both types of devices. They also run tests where your application cannot get network access to ensure you handle that properly as well. Think defensively. Apple is going to test your application in a variety of scenarios.

Q:

Is there any way to test the camera in the simulator?

the code for the camera and test it with the photo library. You’ve learned a lot so far, and lots of the functionality that you’re moving into has outgrown the simulator. GPS functionality, the accelerometer, speaker capabilities, all of these things can’t be tested in the simulator, and to really test them, you’ll need to install them on your device.

Q:

What’s the deal with Apple’s Developer Program again?

A:

In order to install an app on your device or to submit an app to the App Store, you need to be a registered iOS developer with Apple. The fee currently is $99. Even if you want to just install an app for your own personal use, you’ll need to be registered. Look at the appendix for more detailed directions on how installing an app on your phone actually works.

A:

No. What we’ve done is about as close as you can get, which is to implement

Let’s show it to Bob...

you are here 4   527

location is important

Bob needs the where, in addition to the when You’ve given Bob a way to record the proof he captured someone with a photo, and an easy way to note when it happened, but what about the where?

Cool—I love the pictures—but I need location info about the grab, too.

Bob has a jurisdiction problem. There are rules about where Bob can nab criminals, so he needs to keep track of where the capture occurred. The easiest way for Bob to keep track of these things is by recording the latitude and longitude of the capture.

528   Chapter 10

camera, map kit, and core location

How are two new fields going to affect the app? Use this space to show where, and on what view, the latitude and longitude info will end up.

ere

Sketch h

What needs to happen to the data model and the data itself?

you are here 4   529

sharpen solution

Here’s what we came up with for the new view and the data changes:

This will just be a label.

Location: Lat., Long.

Since we’re running low on space in the view, e we’re going to list th e latitude and longitud together.

What needs to happen to the data model and the data itself?

The database needs to be updated: we’re going to be getting a latitude and longitude value in degrees. To hold them in the database, they’ll need to be broken up into two new attributes for the Fugitive class: latitude and longitude.

530   Chapter 10

camera, map kit, and core location

location Construction Get into it and get the app ready for the capture coordinates:

Implement the new fields in the view for the location label and the latitude and longitude fields.

Migrate the database again and produce the new Fugitive class with the latitude and longitude fields.

We called them capturedlat and capturedlon and made them type “Double”.

you are here 4   531

location construction

location Construction Get into it and get the app ready for the capture coordinates:

Implement the new fields in the view for the location label and the latitude and longitude fields. UILabel *capturedLatLon_;

@property (nonatomic, retain) IBOutlet UILabel *capturedLatLon;

FugitiveDetailViewController.h

@synthesize capturedLatLon=capturedLatLon_;

Add this to viewWillAppear.

capturedLatLon.text = [NSString stringWithFormat: @”%.3f, %.3f”, [fugitive.capturedLat doubleValue], [fugitive.capturedLon doubleValue]];

[capturedLatLon release];

FugitiveDetailViewController.m

We’ve added the Lat Lon field here. The values will be added here when the fugitive is captured.

532   Chapter 10

camera, map kit, and core location

Migrate the database again and produce the new Fugitive class with the latitude and longitude fields.

We’re up to iBountyHunter 4.xcdatamodel.

The new fields, capturedLat and catpuredLon, are both of type “Double”.

OK, so I’d bet you can get latitude and longitude from the GPS on the iPhone, but didn’t you just warn us that iPod Touches and Wi-Fi iPads don’t have that?

That’s true, but you’ve got options. You may remember back in that pool puzzle we said something about the iPod Touch and Wi-Fi iPads being able to handle limited location. iOS has more than one way to get at where you are in the world...

you are here 4   533

core location

Core Location can find you in a few ways GPS is often the first thing most people think of for getting location information, but the first generation iPhone didn’t have GPS, and neither do iPod Touches or Wi-Fi iPads. That doesn’t mean that you’re out of options though. There are actually three ways available for iOS to determine your location: GPS, cell tower triangulation, and Wi-Fi Positioning Service. GPS is the most accurate, followed by cell towers and Wi-Fi. iPhones can use two or three of these, while iPod Touches and Wi-Fi iPads can only use Wi-Fi, but it beats nothing. If your head is starting to spin, don’t worry! Core Location actually decides which method to use based on what’s available to the device and what kind of accuracy you’re after. That means none of that checking for source stuff; the iOS will handle it for you with the LocationManager: - (CLLocationManager*) locationManager { if (locationManager_ == nil) {

Allocate the CLLocation Manager.

locationManager_ = [[CLLocationManager alloc] init];

locationManager_.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; } }

locationManager_.delegate = self;

return locationManager_;

Once the locationManager has the position, it will start sending it back to the delegate.

Core Location relies on the LocationManager

To use Core Location, you simply need to create a location manager and ask it to start sending updates. It can provide position, altitude, and orientation, depending upon the device’s capabilities. In order for it to send you this info, you need to provide it with a delegate as well as your required accuracy. The CLLocationManager will notify you when positions are available or if there’s an error. You’ll want to make sure you’re also properly handling when you don’t get a position from the location manager. Even if the device supports it, the users get asked before you collect location information, and can say “No” to having their position recorded (either intentionally or by accident).

Where should we implement this code in our app?

534   Chapter 10

We set the accuracy to 10 meters for Bob. Higher accuracy can take longer to get a fix, affecting the performance of your app.

camera, map kit, and core location

I guess we’re going to need a new header file for some Core Location constants?

Yes, and a new framework. To keep the size of your app small, Apple breaks apart functionality into libraries. As you start adding new functionality, like Core Location, you’ll need to start adding frameworks. Since the Core Location framework isn’t included by default, we need to add it.

you are here 4   535

core location framework

Add a new framework It’s time to add the Core Location framework to the app. Highlight the iBountyHunter target. Click on the Build Phases tab and expand the Link Binary with Libraries section. Then click the + button and choose the CoreLocation framework.

The new framework should be relocated to the /Frameworks folder.

Then update the header file We still need to declare ourselves as conforming to the CLLocationManagerDelegate protocol and add our property:

it.h> #import
> ion/CoreLocation.h #import
Include the new CoreLocation framework.

h” #import “Fugitive.

ewController Controller : UIVi ew Vi il ta De ve ti gi te, @interface Fu We’re working through the delega agerDelegate> {




so that needs to be there.

*locationManager_; CLLocationManager



}

nager so we can use it and Declare the CLLocationMatai ewController.m. synthesize it in FugitiveDe lVi

ionManager; tionManager *locat ca Lo CL ) ly on ad re ic, @property (nonatom FugitiveDetail ViewController.h

536   Chapter 10

camera, map kit, and core location

BE the developer

Your job is to be the developer and figure out where you’re going to implement Core Location into our user flow. Assume that Bob needs the location and date and time to mark a capture.

1

What method will be used to kick off Core Location in the detail view?

2

What happens when the location is returned to the view controller?

3

What happens if Core Location can’t get anything or the user disables it?

4

When will you shut down Core Location?

5

What about other devices?



Core Location inhales batteries. Making frequent calls from your app to find locations will quickly drain batteries, since it turns on the GPS/cellular/ Wi-Fi receiver. That’ll lead to upset users and cranky iTunes reviews. Keep it to a minimum!

you are here 4   537

be the developer solution

BE the developer solution

Your job is to be the developer and figure 1 out where you’re going to implement Core Location into our user flow. Assume that Bob needs the location and date and time to mark a capture. 2

 hat method will be used to kick off W Core Location in the detail view?

The code to initialize Core Location could go into viewWillAppear for the detail view, but we’re going to refactor here and create a new method called refreshFugitiveInformation.

What happens when the location is returned to the view controller?

We’ll know the location manager can get the current position. If the user marks the fugitive as captured, we need to get the current position from the location manager and update the fugitive. - (IBAction) capturedToggleChanged: (id) sender {

if (self.capturedToggle.selectedSegmentIndex == 0) { // The user just marked this fugitive as captured fugitive_.captdate = [NSDate date];

fugitive_.captured = [NSNumber numberWithBool:YES];

We don’t need the continually updating the locations, so we’ll ask its location manager for e last location when th user toggles the captured control.

CLLocation *curPos = self.locationManager.location;

fugitive_.capturedLat = [NSNumber numberWithDouble:curPos. coordinate.latitude]; fugitive_.capturedLon = [NSNumber numberWithDouble:curPos. coordinate.longitude]; }

else {

fugitive_.captdate = nil;

fugitive_.captured = [NSNumber numberWithBool:NO];

fugitive_.capturedLat = nil;

}

}

fugitive_.capturedLon = nil;

[self

This is going to call our new method with all the fugitive information, including updated rmation. refreshFugitiveInformation]; location info

FugitiveDetailViewController.m

538   Chapter 10

camera, map kit, and core location

@interface FugitiveDetailViewController () - (void) refreshFugitiveInformation;

@end

- (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated];

[self refreshFugitiveInformation];

n, To create the new refreshFugitiveInformatiothe of out ion rmat info we’ll need to pull some viewWillAppear method and relocate it.

// Disable the capture toggle button until we have a location fix

self.capturedToggle.enabled = NO;

}

[self.locationManager startUpdatingLocation];

- (void) refreshFugitiveInformation {

self.nameLabel.text = fugitive_.name;

self.idLabel.text = [fugitive_.fugitiveID stringValue]; self.descriptionTextView.text = fugitive_.desc;

self.bountyLabel.text = [fugitive_.bounty stringValue];

self.capturedDateLabel.text = [fugitive_.captdate description];

self.capturedToggle.selectedSegmentIndex = [fugitive_.captured boolValue] ? 0 : 1; self.capturedDateLabel.text = [fugitive_.captdate description]; if (fugitive_.capturedLat != nil) {

self.capturedLatLon.text = [NSString stringWithFormat:@”%.3f, %.3f”, [fugitive_.capturedLat doubleValue],

}

[fugitive_.capturedLon doubleValue]];

else {

}

}

self.capturedLatLon.text = @””;

FugitiveDetailViewController.m

you are here 4   539

be the developer solution

3

BE the developer solution

What happens if Core Location can’t get anything or the user disables it?

Since Bob needs the location info when he marks a fugitive as captured, we’ll need to disable the captured switch if we can’t get anything. - (CLLocationManager*) locationManager { if (locationManager_ == nil) {

locationManager_ = [[CLLocationManager alloc] init];

locationManager_.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; }

}

locationManager_.delegate = self;

er. If we This is the property gelocttation manager haven’t configured a re. yet, we’ll set it up he The location manager wi notify the delegate (us)ll it gets a valid position up when date.

return locationManager_;

- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSLog(@”%@”, @”Core location claims to have a position.”);

}

self.capturedToggle.enabled = YES;

If the location mana get a fix, it will sendgeusr can’t error information.

- (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@”%@”, @”Core location can’t get a fix. toggle.”);

}

Disabling capture

self.capturedToggle.enabled = NO;

n’t have a nice Since the segmented controller really doesusing a UIAlertView ider disabled look, you might want to cons captured. as ne anyo k to warn the user that they can’t mar

540   Chapter 10

FugitiveDetailViewController.m

camera, map kit, and core location

4

When will you shut down Core Location?

We’ll shut it down when we leave the detail view. - (void) viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated];

NSLog(@”%@”, @”Shutting down core location.”); }

Make sure you shut do the location manager when wn yo u need it to conserve batt don’t eries.

[self.locationManager stopUpdatingLocation];

FugitiveDetailViewController.m - (void)dealloc {

[fugitive_ release];

[nameLabel_ release]; [idLabel_ release];

[descriptionTextView_ release]; [bountyLabel_ release];

[capturedToggle_ release];

[capturedDateLabel_ release];

5

What about other devices?

We’re good. All we do is tell Core Location the accuracy we want and it deals with the rest. So, the iPod Touch can get just the best data it can, and we’ll get that.

[capturedLatLon_ release];

[locationManager_ release];

}

[super dealloc];

Implement all this code and then take it for a spin... you are here 4   541

no dumb core location questions

Q:

We start and stop Core Location in viewWillAppear and viewWillDisappear. Is that normal?

A:

It’s normal to start and stop Core Location as you need it. It uses a fair amount of power while it’s running, so it’s best to shut it down if you don’t need it. This gets a little tricky because Core Location can require some time to get its initial position information. To try to make that a little smoother for the user, we enable it as soon as the view appears to give it a head start before the user needs the location.

Q: A:

Is there any way to speed up that initial position?

Core Location will try to cache previous position information so it can give you something as quickly as possible. Because of this, if you’re really concerned about accuracy, you should check the timestamp sent along with the position information to make sure the position is recent enough for your needs.

Q:

Does location accuracy impact things like startup time or battery usage?

A:

Absolutely. The more accurate a position you ask for, the more battery Core Location will consume, and it will potentially take longer to figure out. Lower-fidelity information tends to come to you faster.  Use whatever accuracy you need for your application, but be aware of the implications of high-resolution information.

Q:

Is there a way to just wait for Core Location to have a position rather than having it call back to the delegate like that?

542   Chapter 10

A:

No. Core Location, like a lot of other frameworks in iOS, calls back asynchronously as data is available. Network access generally works this way as well. You need to make sure you keep your users informed of what’s going on in the application and what they can and can’t do at the moment. For example, we disable the Captured button if there’s no position information available. Other options display a wait indicator (like a spinning gear) or display position status with a disabled indicator like an icon, button, or label.

Q:

Why did we have to move the code around and do all that refactoring?

A:

We did it to follow the DRY principle (Don’t Repeat Yourself). We cleaned up the code and eliminated duplication by pulling it out into a separate method and calling that from the two places that need it

Q: A:

What’s the deal with the private interfaces again?

Remember that our header file captures our public interface or API. We don’t want this internal method to be part of our API (in other words, we don’t want other people to call it). We still want to declare it so the compiler can check that we’re calling a valid method, so we add a Private set of methods to our interface in the implementation file. Some people actually put an _ (underscore) before their private method names so that it’s obvious that you shouldn’t be calling this from anywhere but the class’s own implementation. Apple, however, reserves this convention for their private methods.

camera, map kit, and core location

Test Drive Implementing Core Location really wasn’t that hard, but making it work in the user flow required a bit more work. Now that it’s all done, you should be up and running...

l To operate the app here, Bow,b wil ich wh vie ail det the navigate into ion cat Lo re Co will kick off the manager. shoplifting jewelry

Since we added capturedToggle. enabled = NO; to the ’t viewWillAppear, the user canCo engage the control before reates. Location starts returning upd

When the user navigates away from the detail view, Core Location shuts down to save batteries.

It’s working! Bob should be psyched... you are here 4   543

bob gets visual

Just latitude and longitude won’t work for Bob

That’s great for my forms and everything, but I’m more of a visual person...

It’s an iPhone. A map would really be more appropriate. What’s the point of all the network connectivity and fancy graphics if we just show a text field? With just a little bit of code and the iOS Map Kit, we’ve got something a lot more appealing in the works.

544   Chapter 10

camera, map kit, and core location

Map Kit comes with iOS When Apple opened up the API for the Map Kit in iOS 3.0, developers gained access to the maps that come from Google maps, including satellite imagery. There’s lots of customization that you can do with the maps, such as how wide an area they show, what view they start with, and pins and annotations. Logistically, using Map Kit is a lot like Core Location: you’ll need a new framework and will have to #import in the header file.

MKMapView is a control that pulls map information from Google Maps. You can configure it for the normal road display, satellite imagery, or a hybrid, like you see here.

Map Kit comes with built-in support for pushpins at specified locations, called annotations.



Depending on the information you want to show on the map, you can create your own views for annotations and show anything you want, like pictures, formatted text, etc.

Map Kit requires a network connection.

Since Map Kit pulls imagery information from Google, you’ll need to have a network connection for it to be useful. That’s not a problem for the simulator (assuming your Mac is online), but it could be an issue for any device with limited connectivity, depending on the location. Map Kit handles this gracefully, but it’s something to be aware of.

How can we put this to work? you are here 4   545

map custom setup

A little custom setup for the map Like Core Location, it’s not a lot of work to get basic Map Kit support going in iBountyHunter. We’re going to create another private method called initializeMapView that we’ll call from viewWillAppear in the CapturedPhotoViewController to display the capture location on a hybrid (satellite plus road information) map.

CapturedPhoto - (void) initializeMapView { ViewController.m if ([fugitive_.captured boolValue]) { CLLocationCoordinate2D mapCenter = CLLocationCoordinate2 DMake([fugitive_.capturedLat doubleValue], Here, we’ll pass in the value

These values allow us to configure the size of the default map shown.

of the lat and lon where the

fugitive was captured. [fugitive_.capturedLon doubleValue]); MKCoordinateSpan mapSpan = MKCoordinateSpanMake(0.005, 0.005); MKCoordinateRegion mapRegion = MKCoordinateRegionMake(ma pCenter, mapSpan); We pull al l thi information tosge initialize the mapther to .

}

}

self.mapView.region = mapRegion; self.mapView.mapType = MKMapTypeHybrid;

There are a few map types; hybrid is both satellite and road information. The mapRegion specifies should be centered and howhere the map be visible north and sout w much should This effectively sets the h (in degrees). zoom level of the map.

546   Chapter 10

The size of the map is in degrees. We want the map to be pretty zoomed in.

camera, map kit, and core location

Q: A:

What’s the difference between Core Location and Map Kit?

Map Kit is about displaying a map, position-sensitive information, and user interface. Core Location is about getting you information about where you are. You can drag and drop a map onto your view in Interface Builder; you pass it some values and it just works. Core Location, on the other hand, returns values to the delegate, and you need to decide what to do with them. We’re going to take that information from Core Location and give it to Map Kit to show us a map of the capture location, for example.

Q:

Where do all these frameworks come from? What if I want one that’s not on the list?

A:

The frameworks are included as part of the SDK. The actual path to the frameworks varies by version and what platform you’re developing for. For example, the Map Kit framework we’re using is here: /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/ iPhoneOS4.3sdk/System/Library/Frameworks/MapKit.framework. In general, you should be able to add frameworks using the method we described in Xcode and not need to worry about a specific location, but if a framework isn’t listed or you’re adding a custom one, you can point Xcode to the actual path.

Implement the map to show the area where the fugitive was captured.

1

Add the Map Kit framework and the #import. Add the framework just like we did with Core Location. While you’re at it, make sure that you do the #import in the detail view to include the Map Kit header.

2

Configure the photo view to show the map. Rather than adding a whole new view, go ahead and add the map to the CapturedPhotoView with the image. Resize the image and the button then drag an MKMapView to the bottom half of the view.

3

Add the outlets and code for the MKMapView. Now that you have all the support stuff in place, go ahead and add the outlets and the actual Map Kit code we gave you to make the map work. Make sure you wire up the outlet in Xcode and call the new initializeMapView method in viewWillAppear.

Resize the image and the button...

...and use the bottom of the view for the MKMapView.

you are here 4   547

exercise solution

Implement the map to show the area where the fugitive was captured.

1

Add the Map Kit framework and the #import.

Here’s the Map Kit framework...

#import @interface CapturedPhotoViewController : UIViewController { @private

UIImageView *fugitiveImage_; Fugitive *fugitive_;

}

MKMapView *mapView_;

3

Add the outlets and code for the Map Kit.

@property (nonatomic, retain) IBOutlet MKMapView *mapView;

CapturedPhoto ViewController.h

548   Chapter 10

camera, map kit, and core location

2

Configure the photo view to show the map.

Resize the image and the button...

...and use the bottom of the view for the MKMapView.

Just drag thefrom the MKMapViewhe view. library to t you are here 4   549

exercise solution

3

Add the outlets and code for the MKMapView.

@interface CapturedPhotoViewController (Private)

- (void) initializeMapView; @end

@synthesize fugitiveImage=fugitiveImage_; @synthesize fugitive=fugitive_;

@synthesize mapView=mapView_;

- (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated];

If the fugitiv then we ask UeIImhas image data, image from it an age to create an d set it on our image view.

if (fugitive_.image != nil) { self.fugitiveImage.image = [UIImage imageWithData:fugitive_.image]; } }

[self initializeMapView];

CapturedPhotoViewController.m

550   Chapter 10

camera, map kit, and core location

-

Like befor the mapViewe, we need to configure location and . Center on the fugit (void) initializeMapView { ive set the zoom if ([fugitive_.captured boolValue]) { level.

CLLocationCoordinate2D mapCenter = CLLocationCoordinate2 DMake([fugitive_.capturedLat doubleValue], [fugitive_.capturedLon doubleValue]); MKCoordinateSpan mapSpan = MKCoordinateSpanMake(0.005, 0.005); MKCoordinateRegion mapRegion = MKCoordinateRegionMake(ma pCenter, mapSpan);

}

}

self.mapView.region = mapRegion; self.mapView.mapType = MKMapTypeHybrid;

- (void)dealloc {

[fugitiveImage_ release]; [fugitive_ release];

[mapView_ release];

}

[super dealloc];

CapturedPhotoViewController.m

Test Drive Go ahead and build and run the app. You’ll need to make sure that you mark a fugitive as captured, and that the lat/lon field fills in, then flip over the view to look at the map. To try out the zooming on the map, you’d use the “pinching” motion on a real device. In the simulator, hold down option and then click. you are here 4   551

test drive

Test Drive To try out the zooming on the map; this is the “pinching” motion in real life. In the simulator, hold down option and then click.

You can click in the map and move it around.

The simulator pulls yoeur location based on th ur net connection for yo computer.

Excellent! Now all we need is a pin to show where the capture happened.

552   Chapter 10

finesse

camera, map kit, and core location

Annotations require a little more work

Annotations are the little flags that come up when you see a point of interest, represented by a pin. The catch? Incorporating annotations means conforming to the Map Kit annotation protocol. Map Kit uses an annotation protocol so that you can use your existing classes and provide them directly to Map Kit. The downside is that means we need to add code to our Fugitive class: #import #import

#import @interface Fugitive : NSManagedObject { @private

}

@property (nonatomic, retain) NSDecimalNumber * bounty; @property (nonatomic, retain) NSNumber * captured; @property (nonatomic, retain) NSData * image;

@property (nonatomic, retain) NSNumber * fugitiveID; @property (nonatomic, retain) NSDate * captdate; @property (nonatomic, retain) NSString * name; @property (nonatomic, retain) NSString * desc;

@property (nonatomic, retain) NSNumber * capturedLat; @property (nonatomic, retain) NSNumber * capturedLon;

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; - (NSString *) title; - (NSString *) subtitle;

The MKAnnotation prot it defines a property anocol is a little odd in that are used by the MapViewd two getters. These populate the overlay if a to position the pin and pin is tapped.

@end Fugitive.h



If you use automatic NSManagedObject file generation again, you’ll wipe out these customizations.

you are here 4   553

annotation protocol implementation

Fully implement the annotation protocol The protocol requires us to have a coordinate property, a title, and a subtitle. Instead of synthesizing that coordinate property, we’ll implement it ourselves and just return the fugitive’s position, name, etc. For an application in which you expect to have to do more data migration, you should implement a separate class conforming to the protocol that has a reference to its Fugitive (composition) rather than adding code to the Fugitive class directly.

otocol by implementing We conform to the pran d methods. We simply e the property getter alr eady have about th map these to data we fugitive. - (CLLocationCoordinate2D) coordinate {

return CLLocationCoordinate2DMake([sel f.capturedLat doubleValue], [self.capturedLon doubleValue]);

}

- (NSString *) title { }

return self.name;

- (NSString *) subtitle { }

return self.desc;

@end

Fugitive.m

554   Chapter 10

camera, map kit, and core location

Do this!

Go ahead and add the code from the previous page for the Fugitive.h and Fugitive.m files. Then add the bolded code below to your CapturedPhotoViewController.

-(void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated];

If we have a lat / lon for the fugitive, we add the fugitive as an annotation on the map. We can do this since the fugitive now conforms to the MKAnnotation protocol.

if (fugitive_.image != nil) {

self.fugitiveImage.image = [UIImage imageWithData:fugitive_.image]; }

if (fugitive_.capturedLat != nil) {

[self.mapView addAnnotation:fugitive_]; }

}

[self initializeMapView];

CapturedPhotoViewController.m

you are here 4   555

iBountyHunter works!

Test Drive That’s it! Everything should be working now. You may not have noticed as you’ve been working through all this code, but this app is huge and awesome!

556   Chapter 10

camera, map kit, and core location

This is the new map annotation code you added.

This invokes the camera, which you can see on your phone, not the simulator. you are here 4   557

bob approves

That app is awesome. We’re going to have a beautiful future together...

Now, how about that iPad app? I’m ready to get into some more research.

558   Chapter 10

camera, map kit, and core location

AddingFunctionality Untitled Puzzlecross Time to flex the right side of your brain again...

Header Info 1 Header Info 2 etc...

1

2 3 4

5

6

7

8

9

10

Across

Down

2. UIImagePickerController gets images from the __________ and the library. 4. The ______ animation comes with UIKit. 6. The info circle is just a configured _______________. 7. Additional _______________ are needed for MapKit and Core Location. 9. Your app must be able to work on the ____________ , too. 10. __________ sheets are a good way to get a user to pick an option.

1. The camera cannot be tested in the ____________. 3. The iPhone isn't the only __________ that uses apps. 5. Besides GPS and cell towers, _____________ can be used to determine location. 8. ____________ doesn't work without a Net connection.

you are here 4   559

addingfunctionality cross solution

AddingFunctionality cross solution Untitled Puzzle Time to flex the right side of your brain again... 1

Header Info 1 Header Info 2 etc...

S I

2 3

C

A M E

D F

V 6

W

U

I 7

F

I

R

A M E

I

L

I

P

T

O

A B

U

C 8

A

U 4

E 5

R

T

N

O W O

R

K

S

O

D

O

U

C

O

N

A 9

I

P

T

H

K 10

A

C

T

I T

Across

Down

2. UIImagePickerController gets images from the __________ and the library. [CAMERA] 4. The ______ animation comes with UIKit. [FLIP] 6. The info circle is just a configured _______________. [UIBUTTON] 7. Additional _______________ are needed for MapKit and Core Location. [FRAMEWORKS] 9. Your app must be able to work on the ____________ , too. [IPODTOUCH] 10. __________ sheets are a good way to get a user to pick an option. [ACTION]

1. The camera cannot be tested in the ____________. [SIMULATOR] 3. The iPhone isn't the only __________ that uses apps. [DEVICE] 5. Besides GPS and cell towers, _____________ can be used to determine location. [WIFI] 8. ____________ doesn't work without a Net connection. [MAPKIT]

560   Chapter 10

camera, map kit, and core location

You’ve got Chapter 10 under your belt and now you’ve added the camera, Core Location, and Map Kit to your toolbox.

Flip Animation

Comes with UIKit. y Is the typical interface for utilit apps on iPhone. l Is usually implemented as a moda view.

MapKit

Comes with iOS. Requires a new framework. Can be customized for a variety of views, including pushpin annotations.

Camera

Is accessed through the UIImagePickerController. Is not on all devices and you nee d to handle that. Allows you to select and edit an image for use in your app direct ly from your library.

Core Location

Can use either GPS, cell tower triangulation, or Wi-Fi positioning service. Chooses for you which method to use based on the user’s device.

you are here 4   561

CHAPTER 10

Your Location Toolbox

11 iPad UI

Natural interfaces I think it’s important to work with things in nature...

The iPad is all about existing in the real world.  We’ve built a basic iPad port of an existing app for DrinkMixer a few chapters back, but now it’s time to build an interface that works with some real-world knowledge. By mimicking things that people use in the real world, users know what to do with an interface just by opening the app. We’re going to use some real-world elements to help Bob catch the bad guys…

this is a new chapter   563

time for some research

Bob needs that iPad app, too... The iPhone app is up and running and things are great. His on-the-run scenario is handled, but Bob also has some research to do and that isn’t going to be comfortable on his iPhone.

You have to get in the fugitive’s mind. It’s a painstaking process. Research is key to determining patterns, establishing relationships, and then predicting what they’ll do so you can catch them.

This is a different use case. Bob is going to be sitting down, doing some research, coming up with a plan to track the fugitive. iPads are used much more frequently for this type of interaction: an extended period of usage time and with broader functionality. Keep this in mind as we work through building the app.



iPhones and iPads are not used the same way. While there are cases where you’re using similar apps on both devices, often the data is going to be consumed differently.

564   Chapter 11

iPad UI

p that you p a e h t is is h T r... designed earlie

1

To show the list of fugitives, we’ll have a popover that can appear in this corner with this full list. We’ll add some fun UI touches.

Navigation control Fugitive Name

3

Fugitive ID#

Image of the Fugitive

The map will show the last known location for the fugitive.

2

Map of previously known locations

Bounty: This area is for notes and details about the fugitive

When Bob selects an entry, a TexView will display text describing the sightings . for the fugitive Navigation control

iPad App

Details on past w hereabouts. They’ll have a lo cation and details about what the fu gitive was doing there. For research, Bo b needs the full dossier on each fugitive. Picture and details shou ld all be in the same view.  ll the informat A ion he uses in th e iPhone app, too.

For the landscape view, we’ll have the full list of fugitives displayed, since we have extra space. Fugitive Name

More UI goodness—cool page background that looks like a corkboard

Fugitive ID#

Bounty: Image of the Fugitive

List of Fugitive Names Map of previously known locations

This area is for notes and details about the fugitive

When Bob selects an entry, a TextView will display text describing the sightings for the fugitive.

We’ll show the fugitive information on index cards. you are here 4   565

people know how to use things

Why are we wasting all this time on a custom UI? Can’t we just use regular controls again?

We could, but it’s not ideal. DrinkMixer worked fine, but it missed out on a lot of what makes iPad apps easy to use. We simply ported our minimalist iPhone UI to a bigger screen. While that was fine for what we wanted to do, it’s time to step it up to what the iPad is really good at, which is making interfaces that are closer to the real world.

Natural user interfaces make things more real (and easier) One of the reasons that the iPad, and tablets in general, have been so successful is due to their ability to take on the form factor of lots of things that we regularly use in the real world. Books, calendars, menus, clipboards, newspapers, magazines, control panels, dials, and displays­­—they can all be replaced with the right interface in the tablet form factor. Natural user interfaces (NUIs) is a broad term that means any computer interface that tries to mimic more closely the way we interact with objects in the real world. Beyond multitouch tablets, Wii uses a gesture recognition in 3D, and Kinect reads body movements to control the system. All these interfaces have exploited the way we already know how to play sports or other games in the real world to allow us to quickly adapt to their apps. Tablet computing and iPads in particular strive to do the same thing. By leveraging the knowledge that users already have—how to read a book, use a calendar­—NUIs make using an app easier and the learning time shorter.

566   Chapter 11

iPad UI

iOS HIG user experience guidelines The HIG is a great starting point for user experience (UX) considerations for the iPad. The iOS Human Interface Guidelines—User Experience Guidelines, and the Case Studies section in particular, are full of pointers about what to do and not do. There is a great example of migrating mail from iPhone to iPad. The main thrust of moving an app from iPhone to iPad? Realism.

The detail view just has the basics.

Two iPhone views are collapsed to one iPad view.

The Split View Controller flattens the hierarchy.

The subtle touches don’t distract the user.

ed, it When mail is del.”et goes on a “pile

On the iPad detail view, there’s a textured background.

It’s about realism and details When you read about iPad design, the HIG is full of references to “stunning graphics” and “adding physicality and realism.” As you design your own interfaces, with increased real estate and expected time of interaction with the user, it’s important to keep in mind subtle things that you can add that increase the appeal of your app. It’s also partly why iPad apps have a greater value to the user, which means you can charge more!

Let’s apply this to iBountyHunter... you are here 4   567

UI needs planning too

Iterate your interface, too Most developers are familiar with iterative development for software but for some reason don’t apply those same practices to user interfaces. User interfaces, possibly more than any other part of an application, need real user feedback.

Pencil sketches and wire-frames are an important part of the UI design process, but nothing takes the place of real-world usage.

This iterative approach to user interface design is why taking advantage of natural user interfaces when possible is so important­—the designers of those interfaces (and users themselves) have spent a lot of time going back and forth fine-tuning how people interact with them. By incorporating natural UIs into your application, you can take advantage of all that refinement and preloaded user knowledge in your application. Sketches and wire-frames are a critical part of user interface design, but before you pull out your pencil and start drawing, you need to put thought into what exactly is important to your user. A critical part of user interface design is determining what needs to be left out. Some user interface elements only make sense in certain contexts; other elements just confuse things and clutter up the user experience. A good, clean design like iBountyHunter is important.

568   Chapter 11

iPad UI

Jim: I’m confused. Is this a custom UI, or are we using standard controls?

Frank

Joe

Frank: Really, it’s both. Joe: What? Frank: When you’re working with iOS, most apps are a blend of standard controls and custom interface, and that’s what we’ll do. Jim: Oh, so the Split View Controller is still ok? Frank: Yes. We still have a hierarchical data structure we’re working with, so that works for us, but we need to add more detail for Bob to be able to do his research. Joe: OK, I guess that makes sense. Jim: So, what’s the strategy for building this thing? Frank: We need to start with the overall app structure. So we’ll build the Split View Controller first, then get the views to work properly, and then we’ll add the realism. Joe: What are we adding again to make this realistic?

Jim

Frank: We’re going to add a corkboard feeling, make the borders on the map and photo thicker, a pushpin, stuff like that. Jim: OK, I see. So that will make it easier for Bob to digest what’s going on. Frank: Right. Now, for the split view...

you are here 4   569

we know how this works

BountyHunterHD is based on a Split View Controller Just like we did with DrinkMixer, we’re going to add a Split View Controller, and then modify the basics to add a detail view. Unlike DrinkMixer, we’re not going to be reusing our detail view from the iPhone app. The overall structure of the app means that we’ll be working with the same data and some of the same controls between the iPhone and the iPad, but when it comes to the views, we’re going to be keeping things separate. This is pretty typical of real-world apps. Often you start with a given data set that then needs to be applied to different use cases. This is especially true when you’re talking about augmenting a website or existing backend framework with an iOS or mobile component. Your work is really about effectively providing data to the user and setting up the interactivity for each use case. For iBountyHunter on the iPad, Bob needs to do some research. We’ll walk you through the coding, you just get them in order!

gnets it. Use think about Split viewviewMisna’t really that hard iftoyofinu ish up. ed split Adding the e steps we ne ts to order th these magne

on Delete the Navigati Controller

root view le view to the Change the tab

e add th tance e and s Declar ntroller in t to o BOutle tViewC UISpli e and its I gate files l e b l a e i D var erApp inkMix the Dr ail er Det inkMix the Dr w Add in Vie

the nce rom troller refere er f Split View con l l o Wire up the ontr ew c t Vi library Spli ode a xib in Xc Add ow-iPad. MainWind Open up roller Add a Navigation Cont to the Detail View

look This is what itre’ll done... like when you’

570   Chapter 11

refreshers These are some did in from what we may want Chapter 7. Youand go over to turn back with this it—it will help next exercise!

iPad UI

Ready Bake Code

Since you’re such a pro at getting split views set up, we think you should skip some steps. Go get the iBountyHunter iPad starter code at www.headfirstlabs.com/books/iphonedev. Here’s what the new code will contain for you:

e

d o C r e t r a t Hunter S y

Bounty ew Split View Controller, full

If you’re rusty on Core Data stuff, gothe back to chapter 8 to brush up...

n Contains a d. iPad, called e h t r o f implemente view out detail ller. d e b b u st tro Has a ierViewCon s s o all the new D e le iv d it n g a h Fu o t ted s been updaview. a h e d o c e Th the detail p so that u d e ir w elements in e r ts a riate outle p o r p p a e h All t it rks. the portra r o f d the view wo e t n e lem as been imp h r e v o p o p A abase with t a d e h t view. f no the ated versio tion information for r ig m a ’s e r ca The sly known lo the previou fugitives.

Author’s note: If you want to code it up from scratch—go for it, you already know how to do it! you are here 4   571

test drive

Test Drive Go ahead and run what we’ve given you so you know where we are.

The list is wired up properly, so it’s populated.

This is the spot for the image for the fugitive.

Here’s the new detail view.

This is the spot for the image for the fugitive.

The mapView does working data yet, n’sot have default is shown. a

572   Chapter 11

need to These will up to the be wired to get database populate. them to

iPad UI

Frank

Joe

Joe: How are we going to populate this detail view? Frank: Just like we did in the iPhone version, I’d think, but we should probably break things into a couple of separate methods. Jim: Why? Frank: Well, we have a lot to manage here. There’s that map view, the image, and the text that all need to be updated. Joe: Oh right, and that map view needs a decent amount of setup, right? Frank: Exactly. Plus, remember that with the Split View Controller, we don’t re-create the view for each fugitive. Jim: Right. We need to be able to update the view when a fugitive is selected, not just when viewDidLoad kicks off. Joe: OK, so what’s the plan?

Jim

Frank: We need to implement a couple of methods on the detail view to populate the description, the images, etc. Joe: OK, that makes sense.

Implement four new methods in FugitiveDetailViewController.m.

Create a private method called prepareFugitiveDescription that returns a string containing all the vital stats for our Fugitive: name, bounty, and description. Create a private method called prepareMapDescription that returns a string containing all the information for the fugitive’s last known locations. Create a private method called updateDossier that uses our prepareFugitiveDescription and prepareMapDescription and sets up the image, description, map, and popover. Finally, we need one method to pull everything together: the public showFugitiveDossier method. This sets the current fugitive and then calls the updateDossier method and dismisses the popover if it’s visible.

you are here 4   573

sharpen solution

Implement three methods to update FugtiveDetailViewController.m.

@interface FugitiveDossierViewController () - (void) updateDossier;

- (NSString *) prepareFugitiveDescription;

@end

- (NSString *) prepareMapDescription;

#pragma mark - Helper Methods

FugitiveDossierViewController.m

- (NSString *) prepareFugitiveDescription {

return [NSString stringWithFormat: @”Name: %@\r\nID: %@\r\ nBounty: %@\r\nDescription: %@”, fugitive_.name,

[fugitive_.fugitiveID stringValue], [fugitive_.bounty stringValue], }

fugitive_.desc];

- (NSString *) prepareMapDescription {

return [NSString stringWithFormat: @”Last Seen: %6.3f,%6.3f\r\ nDescription: %@”, [fugitive_.lastSeenLat doubleValue], [fugitive_.lastSeenLon doubleValue], fugitive_.lastSeenDesc];

}

- (void) updateDossier {

if (fugitive_.image != nil) {

self.fugitiveImageView.image = [UIImage imageWithData:fugitive_.image]; }

574   Chapter 11

into the Keep going next page... he code on t

This code goes inside the updateDossierMethod

you started...

iPad UI

fugitiveDescription_.text = [self prepareFugitiveDescription]; mapDescription_.text = [self prepareMapDescription];

CLLocationCoordinate2D mapCenter = CLLocationCoordinate2DMake([fugiti ve_.lastSeenLat doubleValue], [fugitive_.lastSeenLon doubleValue]); MKCoordinateSpan mapSpan = MKCoordinateSpanMake(0.005, 0.005); MKCoordinateRegion mapRegion = MKCoordinateRegionMake(mapCenter, mapSpan); self.mapView.region = mapRegion; self.mapView.mapType = MKMapTypeHybrid;

}

if (popOver_ != nil) { [popOver_ dismissPopoverAnimated:YES]; }

- (void) showFugitiveDossier: (Fugitive *)someFugitive { self.fugitive = someFugitive; [self updateDossier]; } FugitiveDossierViewController.m

Do this!

th *)indexPath {

Now you just need to call showFugitiveDossier when the device is an iPad, and the user taps a row. Here’s the code:

(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPa

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { [self.dossierView showFugitiveDossier:[items_ objectAtIndex:indexPath.row]]; } else {

FugitiveDetailViewController *detailViewController = [[FugitiveDetailViewController alloc] initWithNibName:@”FugitiveDetailViewControll er” bundle:nil]; detailViewController.fugitive = [items_ objectAtIndex:indexPath.row]; [self.navigationController pushViewController:detailViewController animated:YES]; [detailViewController release]; }

}

FugitiveListViewController.m

you are here 4   575

test drive

Test Drive Here is where the image fromhinour g in database will go. There’s not (bu t there right now, so it’s empty ). we’ll deal with that in a bit

This contains the text about the fugitive.

This is where the map information for the last known location will go.

576   Chapter 11

It works! Everything is updated, but now we need some polish...

iPad UI

Here is the sketch we came up with for the UI before. What do we need to work on next?

For the landscape view, we’ll have the full list of fugitives displayed, since we have extra space.

Navigation control

More UI goodness—cool page background that looks like a corkboard.

Fugitive Name

Fugitive ID#

Bounty: Image of the fugitive

List of Fugitive Names Map of previously known locations

This area is for notes and details about the fugitive.

When Bob selects an entry, a TextView will display text describing the sightings for the fugitive.

you are here 4   577

no dumb questions

Q:

We still have a few issues in the code; for instance, picking a fugitive without a last known location shows “null.” Are we going to fix those things?

A:

Yes, we’ll definitely fix some of them as we clean up the UI, but some we’ll leave for you to either put on some finishing touches or add new functionality.

Q:

You called a method private earlier. Is it really private?

A:

Sort of. By putting it as a class extension at the top of the implementation file, we aren’t declaring the method to be part of the public interface, meaning people who might look at our class’s header file won’t see the method there. However, there’s nothing preventing someone from actually trying to invoke the method if they knew it existed. So, from that perspective, it’s not really private as much as “unlisted.”

Q:

Isn’t all this “Natural User Interface” stuff just a new way of saying “eye candy”?

A:

No, most definitely not. Sure, by moving to more natural user interfaces, you generally improve the look and feel of an application, but that’s almost a side effect. You make assumptions about how to interact with things in the real world all the time. For example, you generally want to pull on a handle but push on a touch plate on a swinging door. You probably assume that turning a dial to the right will increase whatever it is you’re adjusting while turning it to the left will decrease it, etc. With the larger and high-quality display on the iPad,

578   Chapter 11

you can incorporate these concepts into your application and bring an immediate familiarity to it. If you’re writing a calendar application, don’t assume that you should (or even can) reinvent how people want to view their calendar. Take advantage of established “user interfaces” in day planners and wall calendars—they’ve been around a lot longer and have had lots of time to get it wrong evolve.

Q:

Wait—so if I’m supposed to incorporate things people are used to, doesn’t this depend on who the people are?

A:

Absolutely! That’s a really subtle but important point. By incorporating familiar interfaces and real-world analogs, you can take advantage of existing user knowledge, but you’re relying on the user having that knowledge to some extent. If you target an international audience, you need to keep each audience in mind when you localize your application. For example, if one group of users’ paper calendars always start on Monday (versus Sunday) and you’re trying to mirror the paper calendar look, getting the starting day wrong can be pretty jarring (and break the very familiarity you’re trying to achieve).

Q:

I’ve meant to ask this for a while, but is stuffing images in the database really a good idea?

A:

For our application it’s OK because it simplifies things, but in general you might want to go with something a little more scalable. One option is to have the image wrapped in its own Core Data entity that’s lazy-fetched rather than part of our main

Fugitive entity. An even better option is to store the image on the file system and just keep a URL (path) to it in the Core Data entity. Neither of these approaches would be major changes to what we’ve done so far but would give you better scalability as the number (and size) of the images grow.

Q:

Is it normal to come back and do the UI after you’ve implemented functionality?

A:

It’s normal to come back and theme the UI after you have functionality in place, but you definitely shouldn’t leave the rough UI work until the end. Things that seem like neat UI “innovations” turn out to be really difficult to use or annoying after the first few minutes. It’s also very common to realize that you really want Functionality X to be “right here” while you’re using Functionality Y. Something that was just two taps away now seems very distracting to constantly move in and out of views to reach. These are the kinds of things that are difficult to find without working prototypes of the UI and really can’t be put off until later. Styling a navigation bar with a leather texture can wait— figuring out the basic UI flow through critical use cases can’t.

Q:

How come we aren’t using any detail indicators in the table view?

A:

Good question! Since pushing the detail view doesn’t involve changing views in the iPad, we aren’t going to have a disclosure indicator. Remember, disclosure indicators mean that there is another view that will displace the current one. Since the table view list isn’t going anywhere, it isn’t appropriate.

iPad UI

The app works great! Now we need to add the custom stuff, right?

Yes! The app works, everything updates, and now it’s time to add the UI on top. This is going to add that touch of realism that makes the app easier to use and takes advantage of the iPad’s capabilities. We also need to do something about what happens when there isn’t any data—empty map views and image views don’t look very good.

Pull it all together When we first sketched up a view, we said that we needed some “UI goodness” and had a couple of ideas about what that would mean, like a corkboard background, for instance. One important thing to realize is that a lot of what we’ll do here is styling. That’s important, and will make a difference, but on more complicated applications, you need to think not just about how things look, but how users will interact with your application. We’re giving our UI a natural look, but for bigger applications, you need to think about a natural feel too.

ol page More UI goodness—coks like a background that loo corkboard

Fugitive Name

Fugitive ID#

Bounty: This area is for notes and details . about the fugitive

To make this app really look good, all those added elements need to work together, and they’ll work to create a theme.

you are here 4   579

one for all

Unifying the custom stuff More than just adding some touches of realism, like we talked about with NUIs, we’re going to put together a unified look, or theme, for the app. A UI theme consists of fonts, images, colors, and backgrounds that all work together to create a consistent look for an app. Look and feel goes beyond just images and colors, though. Smooth animations, transitions, etc., all play a role and need to be consistent with the look and feel you’re trying to achieve. For example, using a page curl transition when you’ve styled everything to look like brushed metal with steel rivets isn’t going to look cool or seem intuitive. It’s going to look and feel wrong (most likely, the developer just figured out how to do page curls and wanted to throw them in somewhere!). Creating a consistent look and feel is also where you’re likely going to start to need help. App development can be a profession, and while you can work with some small apps on your own, adding final polish may require some design help. For our purposes here though, using the real-world analog of pushpins on a corkboard and an appropriate font will be enough for iBountyHunter.

Pushpin Notebook paper feeling

Our background

Placeholder images d for missing people an places...

Do this!

580   Chapter 11

Download the four images you’ll need for the theme. Go to www.headfirstlabs.com/books/hfiphonedev and download corkboard.png, RedPushPin.png, question_mark.png, and silhouette.png.

Authors’ note: We are not designers. We have design help!

A dossiertype font

Our spy-looking font. Looks like a typewriter with bolded text for each item.

iPad UI

First, we’ll start by setting up the placeholder images and the background image with a little code. Set the background image in code. You probably noticed that the image for the corkboard is way too small to fill up a whole screen. We’re going to write a little code to set the backgroundColor, which in this case will be a pattern based on our corkboard image, like this:

Add this in the viewDidLoad for the FugitiveDossierViewController.m file.

UIImage *backgroundTexture = [UIImage imageNamed:@”corkboard. png”];

UIColor *backgroundColor = [UIColor colorWithPatternImage:bac kgroundTexture]; [self.view setBackgroundColor:backgroundColor]; Set the fugitive placeholder image.

FugitiveDossierViewController.m

Since the dossier view is just displaying an image, we need to handle which image is shown if the database entry is empty. The code needs to be changed to display the default image only if there isn’t another image from the database: - (void) updateDossier {

if (fugitive_.image != nil) {

image];

self.fugitiveImageView.image = [UIImage imageWithData:fugitive_.

}

else {

self.fugitiveImageView.image = [UIImage imageNamed:@”silhouette.png”]; }

FugitiveDossierViewController.m

Set the mapView placeholder image. The mapView doesn’t display an image at all right now, so you’ll need to add an imageView called mapOverlay on top of the mapView in the GUI editor, and then set the image the same way you set it for the fugitive.

you are here 4   581

exercise solution

First, we’ll start by setting up the placeholder images and the background image with a little code.

Set the mapView placeholder image. The mapView doesn’t display an image at all right now, so you’ll need to add an imageView called mapOverlay on top of the mapView in the GUI editor, and then set the image the same way you set it for the fugitive.

Set the background color to white for the image view and set the default image to question_mark.png.

}

If you create a new fugitive without an image or map information, does your new code work? What about for old fugitives?

582   Chapter 11

iPad UI What, where did the map go? Now it only shows the question mark, no matter what.

In Xcode, these two edges line up.

The question mark image is always on top. When we created the new imageView in Xcode, we put it on top of the mapView. But we only want the imageView to be the top visible layer if there isn’t any map information. If you’re not used to working with graphics programs for layout (including those to build GUIs), there is a concept at work here that we need to point out. Graphics programs work with the concept of layers. That means you can lay images on top of each other, and they can be completely hidden, partially hidden, or partially shown. Views work the same way. Each view can have multiple subviews stacked (layered) in a specific order. In iBountyHunter, when there is map information, we need to hide the imageView. We can do this in code.

Test Drive

Add the bolded code to FugitiveDossierViewController.m inside the updateDossier method to get the imageView out of the way depending on whether we have map information.

Put this code inside the updateDossier code to fix the overlay issues.



self.mapView.region = mapRegion;



self.mapOverlay.hidden = YES;



self.mapView.mapType = MKMapTypeHybrid; }

else {

self.mapOverlay.hidden = NO;

FugitiveDossierViewController.m

you are here 4   583

test drive

Test Drive Add the bolded code to FugitiveDossierViewController.m inside the updateDossier method to get the imageView out of the way depending on whether we have map information.

elect Once you es, this a fugitiv uld be text sho d... populate

Go ahead and click around. If you have data for a fugitive, the images will switch.

This is looking good, but what about formatting the text and the notebook paper with the push pin?

Remember this?

A dossiertype font

584   Chapter 11

iPad UI

It seems we have a problem... UITextView has some limits. Go into Xcode and highlight one of the UITextViews, then you can see what changes you can make. Among other things, you can change the font type:

Here, you can set the font type, color, and size.

...you can only change things for the entire field! Although the text view has served our purposes so far, introducing the requirement to be able to bold just one word means that we need to change our view. UITextViews can be formatted through a recent addition to iOS called NSAttributedString, but these are not for the faint of heart. If you want to start treating any of the text differently (color, bold, underlining, etc.), you need to amp things up a little. Rather than going the NSAttributeString route, we can take advantage of everyone’s favorite markup language, HTML, with some styling through CSS.

There’s another control for this kind of thing... you are here 4   585

fireside chats

UIWebview has lots of options Although inserting a web view might not be your first thought, it’s often one of the best ways to handle complex text formatting. Web views not only allow you to embed material on the web directly into your app without leaving and going into Safari, but you can also load from HTML/ CSS files that you ship (or generate) as part of your application. UIWebViews are basically Safari in a box—not only can they render HTML, they can also be used to embed lots of document types, including Microsoft Office and Apple’s iWork office suite (Pages, Numbers, and Keynote), PDFs, and RTF documents. In short, anything Safari can render, a WebView can render too, inside your application. More than just a developer convenience, this can also be really important for user flow.

Tonight’s talk: We need more options!

UIWebView:

UITextView:

Well UITextView, I guess you’ve outlived your usefulness. What? I’ve been performing great. That’s true, you’re really good at what you do, it’s just...well, you’re so limited. That’s not true. Without any code at all, you can cut and paste, scroll through text, select text, and search it. All true, but it really starts to fall apart when you need to get fancy. Developers who use me get HTML5 and CSS3 support, not to mention the ability to show PDF, Word, Excel, Keynote, Numbers, and PowerPoint files. That’s true, but I’m a whole lot simpler. And if you really want fancy colors and whatnot, you can get there if you try hard enough. 586   Chapter 11

iPad UI

UIWebView:

But I’m much more than just fancy colors. With CSS, you get all the styling and layout support you get with normal web development—and you can use the same tool. Not only that, but you can use JavaScript for interactivity.

UITextView:

But we’re talking about jamming a web browser into the application. You can just hand me a string and I’m good to go, you don’t need to go and load a website or anything.

Actually, you can give me a string, too—it just has to be an HTML string. I also support loading local resources like embedded CSS files, images, JavaScript, or even full HTML files. Interesting. OK, can you edit the text? Hmm.. not really. I can show HTML forms and things like that, but no, I guess I really don’t fit for text editing or things like that. Can you interact with the user or the rest of the application at all?

Oh yeah. Like I said, basically anything you can do with a web page you can do with me in terms of interacting with the user. As for interacting with the rest of the application, that’s a little tricker. I have a couple of delegate methods that get called when I need to know if I can follow a link that the application can use to move information out, and I can evaluate any JavaScript the application sends me and return the results. But, admittedly, it’s not quite as simple as just manipulating a string. Here’s what I think. I think I’m more about straightforward text editing, you’re more about style. I can live with that. I’m like a browser in a box, you’re more of a...souped-up Text Field. I liked how I put it better.

you are here 4   587

three’s company

HTML, CSS, and Objective-C Using a UIWebView is like a cross between a website with basic HTML/CSS and Objective-C. All three of these things work together to create the view that the user is going to see.

The Dossier View Controller constructs a new block of HTML (based on the selected fugitive) that uses a static CSS file for formatting.

FugitiveDossierViewController

The user selects a fugitive in the tableView.

[NSString stringWith Format: @”...

.css

The view controller then asks the web view to load the new HTML (and associated CSS).

Fugitive.css

The description will get drawn in our view, with bolding, lines, and an included pushpin image.

Using UIWebView For our purposes, we’re going to need to generate an HTML version of our fugitive description and last known location. We can then apply some basic CSS to get it to look however we want. We’re going to be working with CSS to actually draw the notebook paper area within the view, so it won’t be an image. We’re also going to set up the pushpin image directly from the CSS so it will stay within the WebView and move and scale as needed for the view. To do this we’re going to need to make a couple of changes. First, we need to swap out our text views for UIWebViews. That’s pretty straightforward. Next, we need to generate the HTML. We can use NSString’s stringWithFormat to do that. Normally it’s a lot easier to write the HTML and CSS you want and get things set up using a Web Browser or any other HTML editing tool, and then move it into code. We came up with HTML that looks like what you see on the opposite page: 588   Chapter 11

to be You don’t neendd CSS an HTML a llow along genius to four friendly here. But o be remiss editor would use this if she didn’tshamelessly moment to irst plug “Head F CSS and HTML with r those XTHML” foo learn who’d like t more...

iPad UI

We’ll get into more detail about the CSS in a minute... ve.css”/> e=”text/css” href=”fugiti

le ur fugitive sthy our o

d a lo e w e er it > H
Name:
%@
ID:
%@
application adle.
Bounty:
%@
d> Description:
%@
ition list n HTML defhine web view a e us ll e’ W m class. T with a custo rest. handles the

Finally, we’ll need to tell the new WebViews to load our dynamic HTML by using loadHTMLString. We want to use our local CSS file, so we need to give the WebView a code format baseURL that points to our application bundle. After that, the WebView handles the rest. Let’s get that going... First we’ll get the HTML and webViews working to replace the textViews. Leave out the CSS link code for now, we’ll get to that in a minute. 1

Delete the two text views and replace them with UIWebViews. Turn off “User Interaction Enabled” and uncheck “opaque” in the Utilities pane for both UI WebView. Don’t forget to handle the IBOutlets to these views as well. You’ll need to change their types in the header and then reconnect them in Interface Builder. Finally, set their background color to clear in viewDidLoad.

2

Refactor the code for the prepareFugitiveDescription. We’ll need to prepare the fugitive description with HTML and then we’ll #define a k constant to handle the case when we don’t have a fugitive. leave out the CSS lin

For now, working. while we get the view

3

Refactor the code for the prepareMapDescription. We’ll need to prepare the fugitive description with HTML and then we’ll #define a constant to handle the case when we don’t have a fugitive’s position.

4

Load the HTML text in updateDossier and viewDidLoad. Now that we can create HTML versions of our descriptions, we need to update our updateDossier method and viewDidLoad to tell the WebView to load its content from an HTML string rather than just setting a simple string on the text view like before.

you are here 4   589

exercise solution

First, we’ll get the HTML and webViews working to replace the textViews. Leave out the CSS link code for now, we’ll get to that in a minute.

1

Delete the two text views and replace them with UIWebViews. Turn off “User Interaction Enabled” and uncheck “opaque” in the Utilities pane for both UI WebViews. Don’t forget to handle the IBOutlets to these views as well! You’ll need to change their types in the header, then reconnect them in Interface Builder. Finally, set their background color to clear in viewDidLoad:

[fugitiveDescription_ setBackgroundColor:[UIColor clearColor]]; [mapDescription_ setBackgroundColor:[UIColor clearColor]];

e in Interfaec fun is h t t e s ld h We cou oo, but where’s t Builder t in that?

590   Chapter 11

iPad UI

2

Refactor the code for the prepareFugitiveDescription. We’ll need to prepare the fugitive description with HTML and then we’ll #define a constant to handle the case when we don’t have a fugitive.

- (NSString *) prepareFugitiveDescription { NSString *response = nil;

if (self.fugitive) { response = [NSString stringWithFormat: @”
Name:
%@
ID:
%@
Bounty:
%@
Description:
%@
”, fugitive_.name,

[fugitive_.fugitiveID stringValue], [fugitive_.bounty stringValue], fugitive_.desc];

} else { response = NO_FUGITIVE_SELECTED_HTML;

} }

return response;

to We use stringWithFormHTatML populate our generic gitive template with real fuextract this data. We could also t to clean HTML into a constanfurther. this method up even

#define NO_FUGITIVE_SELECTED_HTML @”
No fugitive selected.
”;

If we don’t have a fugitive selected, we just use static HTML.

FugitiveDossierViewController.m

you are here 4   591

exercise solution

First, we’ll get the HTML and webViews working to replace the textViews. Leave out the CSS link code for now, we’ll get to that in a minute.

Refactor the code for the prepareMapDescription. We’ll need to prepare the fugitive description with HTML and then we’ll #define a constant to handle the case when we don’t have a fugitive’s position.

3

- (NSString *) prepareMapDescription { NSString *response = nil;

if (self.fugitive && self.fugitive.lastSeenDesc) {

response = [NSString stringWithFormat: @”
Last Seen:
%6.3f, %6.3f
Description:
%@
”, [fugitive_.lastSeenLat doubleValue], [fugitive_.lastSeenLon doubleValue], fugitive_.lastSeenDesc];

}

else if (self.fugitive) {

}

response = NO_KNOWN_LOCATION_HTML;

else {

}

}

response = NO_FUGITIVE_SELECTED_HTML;

Just like in n, we prepareFugitiveDescripLtiotemplate just use a static HTMreal fugitive and populate it with data.

return response;

#define NO_KNOWN_LOCATION_HTML @”
No last known location.
”;

This constant is for when we have a fugitive selected, but don’t have a last known location.

592   Chapter 11

FugitiveDossierViewController.m

iPad UI

4

Load the HTML text in updateDossier and viewDidLoad. Now that we can create HTML versions of our descriptions, we need to update our updateDossier method and viewDidLoad to tell the WebView to load its content from an HTML string rather than just setting a simple string on the text view like before.

ion bundle RL to our applicat file ve Here, we get a Uca find the fugiti .css so the WebView rn HTML. we reference in ou NSString *path = [[NSBundle mainBundle] bundlePath]; NSURL *baseURL = [NSURL fileURLWithPath:path]; [fugitiveDescription_ loadHTMLString:[self prepareFugitiveDescription] baseURL:baseURL]; [mapDescription_ loadHTMLString:[self prepareMapDescription] baseURL:baseURL];

Once we have the ba L, we simply ask the WebView to load thesegeUR ne the fugitive information. rated HTML string with

FugitiveDossierViewController.m

you are here 4   593

test drive

Test Drive The refactoring should be complete. The formatting isn’t done yet (that will happen with the CSS), but for now, the webViews should be in place and functioning...

nd We set the backgrrou he ot to clear, but oks than that, it still salome... e th pretty much

Use CSS for the remaining formatting Now we’re ready to get into the CSS. We have a bunch of things we need to accomplish with the formatting, and all of it’s going to happen inside this CSS file:

594   Chapter 11

ne ch of the liiption a e r o f g in - Bold he fugitive descr items in t e card with t o n e k li r - A pape d lines backgroun oking font lo r e it r w e he - The typ on top of t in p h s u p a - Adding paper note card

iPad UI

Formatting Text Up Close

Here’s our stylesheet that we’ll use for formatting the text in our webview.

dl.dossier { min-width: 300px; min-height: 150px; -webkit-box-shadow: rgba(0, 0, 0, 0.5) 0 0.3em 0.3em 0; background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, colorstop(95%, #FAFAFA), color-stop(95%, #FAFAFA), color-stop(100%, #DDF)); background-size: 1.6em 1.6em; margin: 0; we have a At the top level, th ssier class. padding: .5em; nition list wi a CdoSS to get fi de font-family: “AmericanTypewriter”; e do a little fancgybackground W } the tebook-lookin

Here’s where we sethtat we typewriter font our text. want for all of

.dossier dt { padding-right: .2em; display: run-in; font-weight: bold; } .dossier dd { margin-top: .5em; margin-left: .5em; margin-right: .5em; padding-right: .5em; }

a no image. without needing an

r styles are The remainder ofboould font and some pretty simple—a ut information. padding and layo

shpin.

the pu .dossier:after And here we drop inin CSS so that the { Again, we do this everything. We could display:block; WebView handles r UIImageView on content:””; have laid anothe would have had to position:absolute; top, but then we o in code. width: 50px; coordinate the tw height: 35px; .css background: transparent url(pushpin.png); top: -.05em; left: 50%; } Fugitive.css you are here 4   595

test drive

Test Drive This is it! Go to www.headfirstlabs.com/books/hfiphonedev and get the fugitive.css file and add it to the /SupportingFiles directory. Then add the following HTML right after the tag in the four locations in FugitiveDossierViewController where you generate the map and fugitive descriptions:



596   Chapter 11

iPad UI

I love it! Now I can use all this information to track down my fugitives and catch them faster!

Justice is served! Bob is really happy, and this is a great universal app. Two completely different use cases have been handled: catching the fugitive and reporting it, as well as researching the fugitive in detail. There’s only one database to maintain, but two totally different views, and the code is shared, so making updates should be a breeze. Congrats!

you are here 4   597

NUI cross

NUI Cross This is it! Exercise your vocab skills and make sure you learned some new lingo... Untitled Puzzle

1

Header Info 1 Header Info 2 etc...

2 3

4

5

6

8

7

9

10

Across

Down

1. WebViews can be laid out with ________. 5. ___________ in UI helps users figure out how to work with the app. 6. iPhones and iPads have __________ use cases. 8. This type of view displays text with lots of formatting options. 9. Views are constructed with _________.

2. This displays text, but has limited formatting options. 3. This kind of interface is preferable for iPad. 4. Webviews are formatted with ___. 7. The unifying design for an app is sometimes called a __________. 10. Text in TextViews must be formatted the ________.

598   Chapter 11

iPad UI

Your NUI Toolbox CHAPTER 11

You’ve got Chapter 11 under your belt and now you’ve added iPad user interface skills to your toolbox.

Custom UIs

NUIs

he nowledge of t s k s’ r se u e g a r app Leve make learning real world to easier. e an details to mak d n a sm li a e r Use familiar. app feel more

Try to be consistent by using a theme. Fonts, images, and textures all work together to create a theme.

UIWebView

Can talk to local material in the app or the web. Uses HTML/CSS to format content. Much more flexible than UITextView for UI touches.

you are here 4   599

NUI cross solution

NUI Cross Solution This is it! Exercise your vocab skills and make sure Puzzle you learned some newUntitled lingo...

1

H

2

T M L 3

E

D

5

R

E

A

T

T

V

U

I

F

F

E

R

E 8

W E

B

V

I

E

4

N

X

6

Header Info 1 Header Info 2 etc...

W

L

I

L

S M S

E

N

A 9

C

7

T H

A

Y

E

R

10

S

M

A

E

M E

Across

Down

1. WebViews can be laid out with ________. [HTML] 5. ___________ in UI helps users figure out how to work with the app. [REALISM] 6. iPhones and iPads have __________ use cases. [DIFFERENT] 8. This type of view displays text with lots of formatting options. [WEBVIEW] 9. Views are constructed with _________. [LAYERS]

2. This displays text, but has limited formatting options. [TEXTVIEW] 3. This kind of interface is preferable for iPad. [NATURAL] 4. Webviews are formatted with ___. [CSS] 7. The unifying design for an app is sometimes called a __________. [THEME] 10. Text in TextViews must be formatted the ________. [SAME]

600   Chapter 11

i leftovers

The top 4 things

(we didn’t cover)

Ever feel like something’s missing? We know what you mean... Just when you thought you were done, there’s more. We couldn’t leave you without a few extra details, things we just couldn’t fit into the rest of the book. At least, not if you want to be able to carry this book around without a metallic case with castor wheels on the bottom. So take a peek and see what you (still) might be missing out on.

this is an appendix   601

internationalization and localization

#1. Internationalization and Localization iOS devices are sold in over 80 countries and support 30 languages out of the box. Depending on your application, you should consider supporting multiple languages and cultures. Internationalization is the process of identifying the parts of your application that are culture or language-specific and building your app in a way that supports multiple locales. Some of the things you should look at are: Nib files (views, labels, button text, etc.) Location or culture-specific icons and images such as flags or text Included or online help and documentation Static text in your application Once you’ve identified the culture or language-specific parts of your application, the next step is to localize them. iOS has strong support for localizing resources and separates the localizable resources from the rest of the application so you can easily use a localization team or outsource the effort all together. Up until now, our resources have been included in our application in the .app directory. Once you start localizing resources, Xcode creates an lproj directory for each localization (locale) you add and moves the locale-specific resources there. For example, if you provide both English and French translations of your nibs, then you will have an en.lproj (or English.lproj) and fr.lproj directories in your application.

Localizing nibs

guage You can change your lan going by ne ho and locale on iP l→ ra ne Ge into Settings→ International.

Xcode has built-in support for localizing nibs. Before you start translating anything, you need to ask Xcode to create the localespecific directories.

Select a nib and pull up the Utilities panel.

Localization info is here. 602   Appendix i

leftovers

Next, click on the + button under the Locialization group. Xcode will turn your nib entry in the project list into a group with each localization listed beneath it. Xcode copies your original nib into your default localization.

Click on the “+” button here to have the director structure created.y

The next time you click the + button, a drop-down box will appear and you can select any language you want to use. There will be a new nib created for each language.

Just select every country you want here.

you are here 4   603

localizing strings

Now all you need to do to localize the nib is double-click the language you want to localize and translate any text. Remember that depending on the language, you may need to adjust layout as well. For large projects, there is a command-line tool called ibtool that you can use to extract all string values from a nib into an external file, then merge translations back into the nib later. This allows for bulk extraction and translation, but you need to be particularly careful about layout issues since you’re not visually inspecting each nib. Once a nib has been translated, you can have Interface Builder mark it as locked to prevent any accidental changes to the text or layout that could impact your translations. See Apple’s documentation on bundles and nib localization for more information.

Localizing string resources In addition to nib text, text in your application that you intend on showing the user needs to be localized as well. For example, the Action Sheet used in iBountyHunter offers the user the option to take a photo, choose an existing one, or cancel. That button text is generated programmatically and needs to be translated appropriately. For this type of text, called string resources, the iOS uses strings files. You’ll generally have one of these files for each language you support. Each file contains a description of what the string is trying to communicate, the default language version of the string, and the translated version, like this:

/* Confirms a really bad decision. */ “All In” = “All In”; /* Cancels the dialog */ “Cancel” = “Cancel”;

tion n have a descripthe ca ce ur so re ng ri d stan Each st ranslators under that helps thehet string. context of t e original Then each string has th n. string and its translatio

/* Title for the important alert view */ “This is important!” = “This is important!”;

/* Warns the user about impending badness. */ “This will empty your bank account. Are you sure?” = “This will empty your bank account. Are you sure?”;

604   Appendix i

leftovers

Generating your strings file You could create your strings file by hand, but a much simpler way is to have Xcode generate it for you. Xcode does this by looking for the localization macros that load the translated text. To support localized strings, you should use one of the NSLocalizedString macros, like this:

izedString is The first argument to NSLocaltions file. This is used as a key into the translafor the string. usually the default language The second argu (IBAction) pushMePressed: (id) sender { comment to be shment is the UIAlertView *alertView = [[UIAlertView alloc] string in the stri own with the ngs file. initWithTitle:NSLocalizedString(@”This is important!”,

@”Title for the important alert view”) message:NSLocalizedString(@”This will empty your bank account. Are you sure?”, @”Warns the user about impending badness.”) delegate:nil cancelButtonTitle:NSLocalizedString(@”Cancel”, @”Cancels the dialog”) otherButtonTitles:NSLocalizedString(@”All In”, @”Confirms a really bad decision.”), nil]; [alertView show]; }

If you’ve used the NSLocalizedString macros in your code, you can generate your strings file simply by running the genstrings command at the command line, like this: genstrings -o English.lproj *.m */*.m

You’ll want to run this for each translation you support. This will create a file named Localized.strings in the specified locale directory that you can give out to translators. You’ll need to add that strings file to your Xcode project like any other resource, but once it’s there, the iOS will look in the appropriate strings file at runtime based on the language the users select for their device. The iOS provides robust localization capabilities, including currency, time, and date presentation support; we’ve just scratched the surface. Apple provides several documents on internationalization and localization, including the “Introduction to Internationalization Programming Topics” document in the Xcode documentation, to help you with more complex scenarios.



iOS caches resources!

If you’ve installed your app before doing translations, it’s likely that the iOS has cached resources so that even after adding translations, you won’t see them until you uninstall and reinstall your app!

you are here 4   605

animation is fun

#2. View animations If you’ve spent any time with an iOS device, you know that smooth transitions and graceful animations define the user experience. In the applications we’ve built so far, we’ve only touched on a few basic animations (like the flip animation used in iBountyHunter). However, everything from adding and removing table rows to sliding controls around the screen can be animated.

Animating table view updates If you’re going to add or remove multiple rows in a table view, you can ask it to provide a smooth animation (as well as a more efficient handling of updating the table view itself) by sending it the beginUpdates message before you start manipulating the data, then an endUpdates when you’re finished, like this:

[self.tableView beginUpdates]; [self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight]; [self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade]; [self.tableView endUpdates];

tableView that you’re The beginUpdates and endUpdates tell thet actu ally animate about to make multiple changes so it won’ then everything (the anything until it gets the endUpdates call;at once. insertions and deletions) will be animated

can When inserting multiple rows, you to hs Pat dex use the insertRowsAtIn indexPaths tell the tableView the new iew will you want to add. The tableV e and immediately ask the datasourcfor on ati delegate for cell inform specify those new rows and, if you they’ll the animation information, le. smoothly slide into the tab

Animating view and control changes Similar to table views, UIViews have built-in support for smoothly animating changes to several of their properties. You simply need to tell the view that you want it to animate a change by sending it the beginAnimations message, describing the end point of the change, and then asking it to start the transition by sending it the commitAnimations message. The following UIView properties can be animated automatically:

UIView property frame bounds centerpoint transform alpha 606   Appendix i

Description The physical rectangle that describes the view—the view’s origin and size—in the superview’s coordinate system The origin and size of the view in local coordinates The center of the view in the superview’s coordinates Any transformations (rotations, translations, etc.) applied to the view The transparency of the view

leftovers

#3. Accelerometer One of the most versatile pieces of hardware in iOS devices is the accelerometer. The accelerometer allows the device to detect acceleration and the pull of gravity along three axes. With just a few lines of code, you can tell whether the device is right-side up, upside down, laying flat on a table, etc. You can even detect how quickly the device is changing direction.

All you need is the UIAccelerometer Getting orientation information from your device is straightforward. There’s a shared UIAccelerometer instance you can access. Like many other iOS classes, the UAccelerometer has a delegate protocol, UIAccelerometerDelegate, that declares a single method for receiving acceleration information. The class you want to receive that acceleration information should conform to the UIAccelerometerDelegate protocol and implement didAccelerate: method. - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration;

ference to You’ll receive aetreer along with To receive acceleration information, you simply need to tell the accelerometer about the he accelerom t a UIAcceleratiouanl delegate and how frequently to send acceleration information, like this: an instance of nt ains the act class, which co formation. acceleration in self.accelerometer = [UIAccelerometer sharedAccelerometer]; self.accelerometer.delegate = self; self.accelerometer.updateInterval = 0.5f;

Get the shared... accelerometer

...then configure the delegate and an update rate in seconds. We’re asking for two updates a second. Each UIAcceleration object contains acceleration information along the x, y, and z axes and a timestamp indicating when the data was collected. In a simple example, you can update labels with the acceleration information, like this: - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { self.xOutput.text = [NSString stringWithFormat:@”%.4f”, acceleration.x]; self.yOutput.text = [NSString stringWithFormat:@”%.4f”, acceleration.y]; self.zOutput.text = [NSString stringWithFormat:@”%.4f”, acceleration.z]; }

you are here 4   607

acceleration is fun

Understanding device acceleration First, the bad news. The simulator doesn’t simulate the accelerometer at all. You’ll get no information back, regardless of how much you shake your Mac. You need to install the application on a real device to get actual accelerometer information back. But once you do...

+Y

The accelerometer along a particular axreturns acceleration is held still, the pull is. If the device defined as 1.0 along of gravity is some axis.

t phone, you can ge If you shake tnhevalue greater than an acceleratio shake (to clear the 1. To detect a ample), you can watch screen, for ex tion value greater for an accelera It’s not hard to get an than “normal.” lue above 1, but above, acceleration vaes some effort. say, 1.5 requir

-X The Z axis runs th display of the phon rough the positive Z pointing e, with front of the displa out of the the device face-up y. Place table and your Z axon the is value will be -1. Held upright, you’ along the Y axis (tll get nearly -1.0 value will be just ab he acceleration.y out -1).

+X +Z

in landscape Hold the deviceh the home button orientation witd you’ll get an x to the left an value of +1.

-Y

If you’re building a typical view-based application, UIKit hides a lot of the need for the accelerometer by letting you know about orientation changes and automatically providing undo/redo when the user shakes the phone. The accelerometer is most useful for custom-drawn applications like games (steering or balance) and utility applications (for example, levels).

608   Appendix i

leftovers

#4. A word or t wo about gaming... iOS games are a huge market and get played a lot, but they’re also pretty advanced applications. It’s outside of the scope of our book to get into those applications—which can use multi-touch interactions, Quartz and OpenGL graphics, and peer-to-peer networking—but here, we’ll give you a quick pass at the technologies that you can use and where to find more information about them.

These are all the button events than can be triggered.

Multi-touch You probably noticed that we only used one of the possible events that can be triggered for a button in our apps, the touch up inside event. iOS is capable of detecting up to five finger touches at a time and can interpret how each of those fingers is interacting with the screen with several different types of events. In addition to touches, iOS can detect swipes and gestures that can be configured as well. By defining the length and direction of a swipe, you can create lots of different ways to interact with your application. Pinching is a custom gesture that Apple uses in many of its default applications, most notably Safari, to zoom in and out of a view. It is just registering for a two-finger touch and keeping track of the change in the distance between them: If it increases, zoom out, if it decreases, zoom in. Using these events means you can create custom interfaces—not just touching buttons—for your user. Working with multi-touch means that your view needs to be configured to be a multi-touch view, and then you need code to work with each different type of event you’re interested in leveraging. Working with these events requires working with the responder chain (see the UIResponder class reference) and the UIEvents class reference.

you are here 4   609

drawing pictures

Quartz and OpenGL Quartz and OpenGL are the two ways to create graphics on iOS, and they’re both big enough to be books on their own. Here’s a small sample of what you’d be dealing with:

Quartz

Ed note: Now there’s a fine idea...

Quartz is the simpler of the two, allowing you to draw in two dimensions directly into the view. The drawing code uses the Core Graphics Framework and renders directly into the view. It follows a painter’s model, which means that the order of commands is important. The first thing drawn will be covered up with a subsequent drawing in the same location. Quartz can handle shading, color, and interfacing with other image and video types. The Quartz 2D Programming Guide in the developer documentation has a lot of information to help get you started.

OpenGL OpenGL can work in two or three-dimensional graphics and is significantly more complex, but that means that you have more flexibility to work with. It is a well-established, crossplatform library that has been implemented for mobile devices with OpenGL ES, and it’s used through the OpenGL ES Framework. You can use it to draw lines, polygons, and objects, and animate them as well. A good place to get started is with the OpenGL ES Programming Guide for iOS in the developer documentation.

Game Kit New with the iOS 3, the Game Kit framework allows you to use both peer-to-peer networking and voiceover bluetooth to facilitate interaction with other devices within game play. This functionality does not exist for the first generation iPhone, iPod Touch, or the simulator alone. Similar to the image picker, there is a GKPeerPickerController that provides a standard interface for finding other devices running your application and establishing a connection. After that connection is established, you can transmit data or voice between devices. A good place to get started is with the Game Kit Programming Guide to leverage this new functionality in your app.

610   Appendix i

ii preparing an app for distribution

Get ready for the App Store It’s time to take this thing out for a spin, don’t you think?

You want to get your app in the App Store, right? So far, we’ve basically worked with apps in the simulator, which is fine. But to get things to the next level, you’ll need to install an app on an actual iPhone, iPad, or iPod Touch before applying to get it in the App Store. And the only way to do that is to register with Apple as a developer. Even then, it’s not just a matter of clicking a button in Xcode to get an app you wrote on your personal device. To do that, it’s time to talk with Apple.

this is an appendix   611

get your development certificate

Apple has rules We’ve talked about the HIG and how stringent Apple can be throughout the approval process—they’re protecting their platform. Part of that requires keeping track of what goes on your own device, even when it’s stuff you’ve written yourself. Here, we’re going to give you an overview of how you can get an app onto your device, and then, in turn, ready for submission. We can’t get into the nitty gritty of the full process—for that, you need to be a member of the iOS Development Program and pay the $99 fee.

The iOS D Xcode docuevelopment Guide in t more good mentation has some he can look at information that you Developmen before you join the t Program.

Start at the Apple Developer Portal The Developer Portal, where you first downloaded the SDK, is also your hub for managing all the parts of electronic signatures that you’ll need to get an app up and running on your iOS device.

First, get your Development Certificate Getting through the process to go from having your app in Xcode to installing it on an iPhone, iPad, or iPod Touch for testing requires a Development Certificate and a Provisioning Profile. This certificate is signed by you and Apple to register you as a developer. It creates a public and a private key, and the private key is stored on the keychain app on your Mac. Here’s how getting that certificate works.

Generate a Certificate Signing Request (CSR) in Keychain.

Submit the CSR to Apple for approval.

Apple approves the request and generates the certificate. Then it gets posted on the Portal for download.

Keychain on your Mac

Apple Developer’s Portal

The Certificate is stored in Keychain andthe identifies YOU. Xcode will use it to sign apps you build to install on a device. 612   Appendix ii

opment Download the Devstelore it in Certificate and Keychain.

preparing an app for distribution

The Provisioning Profile pulls it all together Now that you have a Development Certificate in place, to complete the process, you need a Provisioning Profile. That electronic document ties the app (through a UDID), the developer, and the certificate together for installation onto the device.

In Xcode, you’ll use the Organizer to keep all your devices and profiles straight.

To start, you need to enter you Device ID into the Developer’s r Portal to request a Provisioning Profile.

Xcode on your Mac

Apple Developer’s Portal In the Organizer you’ll attach the , Profile to your devi

Apple will issue a Provisioning Profile that you’ll need to download to the Organizer in Xcode.

iPhone or iPod Touch for testing

ce.

Finally, when you compile your app in Xcode, you’ll be able to select your iPhone as the location for the build, rather than the simulator.



You can’t get a Provisioning Profile without a Development Certificate.

you are here 4   613

stay organized

Keep track in the Organizer The Organizer is a tool that comes with Xcode that we haven’t been able to talk much about, but it’s key for keeping all this electronic paperwork straight. In Xcode, go to the Window→Organizer menu option.

If you have your iPhone plugged in, you’ll get a similar display.

Here, you’ll be able to get to your Provisioning Profiles.

This Identifier is required for a Provisioning Profile and is unique to each device.

You can also take screenshots through the Organizer.

In here, you’ll be able to configure your device for development. The Organizer will make sure that you have a valid software version for development, and if not, help you choose one that works.

A few final tips... This quick overview gives you an idea of how the process works, but you need to get into the Developer Program to learn all the details. Our goal here was just to help you see the big picture of the process. There are a couple of things to be aware of. First, when you’re developing as part of a team, the team admin has to be involved in many of these steps. Second, you need to go through this process to install anything on your device, regardless of whether you plan to release it to the world or not. And finally, what about the app store? Once you’ve joined the Developer Program and the application has been tested, then you can submit it for approval.

614   Appendix ii

More Informat

ion

After you’ve jo Program, get ined the Developer Portal and loo into the Developer’s Development Pk for the iPhone rogram User G uide. It has a lot of g o o d inform to get you th rough the pro ation cess.

Index Symbols “@” before strings 69 @dynamic directive 409 @end 105 &error 475 # import 104 @interface 103, 104 @private sections 104 @property 27, 105, 106 with @synthesize 85, 116 @public sections 104 @“sleeping” 124 @ symbol 32, 159 @synthesize 85, 106, 108 with @property 85, 116

A accelerometer, getting orientation information 607 accessors, auto-generated 109 actions 77–82 events (see events) (see also IBAction) action sheets 522–527 implementing delegate methods 523 AddDrinkViewController 231–240, 243–245 adding ability to edit 307 dismissModalViewControllerAnimated 245 saving new drinks 279–281 aesthetics 47 alloc 111 init call after 120 Allow Selection While Editing 306 Anatomy of a Crash 199 animating view and control changes 606 animation and curve properties in notification 276

annotations 553–555 APIs, undocumented 32 AppDelegate 298, 334 AppDelegate_iPhone 376, 377 AppDelegate_Shared 377, 423 App Layout Construction 45–47, 221, 241 solution 222 Apple guides and tutorials 48 Apple iOS SDK (see iOS SDK) Apple’s Developer Program 527 registering with 526 applicationWillTerminate 475 App Magnets 43 Solution 44 app patterns (see design patterns) apps aesthetics 47 building app for iPad vs. iPhone 323 configuration information 47 controls 44 crashing 294 delegate providing the content 63 design patterns (see design patterns) DrinkMixer (see DrinkMixer app) email messaging framework 44 events 76 failing to implement a required method in a protocol 74 File’s Owner 56 good design vs. bad design 56 GUI builder (see Interface Builder) icons 56 immersive (see immersive apps) Interface Builder (see Interface Builder) interface vs. protocol 74 iPhone application structure and where you can read/ write 422 iTunes App Store 20 layout 44 lifecycle of 4, 472 this is the index   615

the index

apps (continued) limits to the number of protocols a class can realize 74 multiple views (see multiple views) nib 56 picker methods 76 productivity (see productivity apps) rebuilding for other phones 20 resources 33–34 starting with a sketch 44 switching 294 templates 8–9 types 48 universal 359, 364 Universal App Distribution xvi–xvii, 324–325 usability 47 usability and aesthetics 47 utility (see utility apps) Xcode and templates 8 (see also iTunes App Store) App Store 3, 197, 211, 212, 216–218, 317, 323 basics 249 submitting app to 611 universal apps 364 arguments, named 127 arrays 298 dictionaries 202 getting data into detail view 206–210 hardcoded into implementation file 161 plists 165 saving 295–296 sorting 292 assign 108, 139 and NSString 110 Assistant Editor 11, 13, 27 asterisks 104 atomic 108 Attributes Inspector 123 auto-generated accessors 109 automatic migration 460 automatic NSManagedObject file generation 553 autorelease 111

616   Index

B background time 473 batteries and Core Location 537 BOOL, using to track keyboard 269 BountyHunterHD 570 brackets for message passing 130 breakpoints 190 Bullet Points automatic migration 460 Cocoa Touch 31 Core Data 444 filtering data based on logical conditions 496 hierarchy view for the xib files 390 IBActions 31 IBOutlets 31 Interface Builder 31 iOS tables 171 iPhone Simulator 31 lightweight automatic migration 460 lightweight migration and adding variables 460 Managed Object Context 444 Managed Object Model 444 multiple views 171 Navigation controllers 171 nib files 31 NSFetchedResultsController 496 NSFetchRequest 496 NSPredicate conditions 496 Objective-C method arguments 130 method declarations 130 method implementations 130 send messages to receivers 130 picker 74, 96 productivity apps 171 protocols 74 table views 171 viewWillAppear 496 *.xib files 171 Xcode 31 buttons adding to view 15 changing text 19

the index

HIG guidelines 51 hooking to buttonPressed method 26

C camera augmented reality 527 capturing HD video 521 checking specific capabilities 521 devices without 527 testing in simulator 527 UIImagePickerController (see UIImagePickerController) video capture 521 (see also images) cancel button 291 Captured List view, building 386 CapturedListViewController 480–483 final code for 490–491 refreshing data 493 CapturedPhotoViewController 505–516 building 506–508 displaying capture location 546 Ready Bake Code 512 cellForRowAtIndexPath 232 cell identifier 154 class, limits to the number of protocols a class can realize 74 CLLocationManager 534–543 CLLocationManagerDelegate protocol 536 Cocoa collection types NSArray 165 NSDictionary 165 Cocoa Touch 12, 14, 20, 31, 32, 54 datasource 62 delegates 62 InstaEmail 55 common warning culprits 187 compiler 108 components triggering events 28 configuration information 47 constants declared with a “k” in front of them 400

contentSize 257–258 Control-Datasource-Delegate pattern 63 controls 44 datasources 63 delegates 63 making keyboard go away 122–131 copy 108, 111, 139 Core Data connecting to Fugitive database 417–424 controller classes 486 crash 452 data types 397 data validation 475 date type 449 deleting empty databases 424 Documents directory 422 efficient results handling 486 filtering results 477–485 hash modifier 460 lightweight data migration 457 renaming 460 loading and saving data 396 Managed Object Model (see Managed Object Model) migration outside of 460 mismatch between database and model 452 monitoring for changes 486–487 NSError 475 overview 396 persistence, types of 400 requiring two models 454 SQL datatypes/table structures 400 stack 418 storing user data 422 support 361 template and databases 421 unsupported type 400 CoreData cross 438 Solution 441 Core Data Up Close 403 CoreGraphics 11 Core Location 534–543 batteries 537 disabling captured switch 540–541 framework 536–543, 547 location accuracy 542

you are here 4   617

the index

Core Location (continued) Map Kit, difference between 547 speeding up initial position 542 starting and stopping 542 viewWillAppear and viewWillDisappear 542 waiting rather than calling back to delegate 542 crashes Anatomy of a Crash 199 Core Data 452 debugging 200 unknown selector 199 creating a consistent look and feel 580–583 Creating an App for iPhone and iPad 323 crosswords AddingFunctionality cross 559 CoreData cross 441 DataMigration cross 497 iPad cross 357 iPhone cross 37, 92 iPhoneDev cross 248 MultipleViews cross 194 NavigationController cross 312 NUI cross 598 Objective-C cross 138 CSS Formatting Text Up Close 595 UIWebView 588–596

D data Core (see Core Data) detail view in DrinkMixer app 168 editing and deleting data in table view 300–307 filtering 477–485, 499 based on logical conditions 496 getting data from picker using IBOutlet 83–91 iPads vs. iPhones 564 loading and saving 396 managing (see managing data) monitoring for changes 486–487 pickers 60 controlled input 61 refreshing 493 refreshing table data 291 618   Index

saving new or changed items 499 storing user data 422 tab bar controller (see tab bar controller) viewing fetched data 412–417 ways to save and load 171 databases copying to Documents directory 423 iPhone application structure and where you can read/ write 422 saving and loading to using SQLite 171 data migration 499 at runtime 460 automatic 457 common problem 453 data model versions 455 lightweight 457 requiring two models 454 DataMigration cross 497 Solution 498 data model 449 versions 455, 460 Data model Demolition 454 datasource 96, 159, 171 Cocoa Touch 62 connecting 71 pickers 62, 65–66 section headers and footers 159 table views 152 templates 152 datasource protocols 67–74 adding implementation code 68–69 declaring controller conforms to both protocols 68 required methods 70 datasources controls 63 data types binary 510 Core Data 397 Fugitive data type 399 dealloc method 85 debuggers 9 debugging 187–193, 249 common warning culprits 187 console 188

the index

Xcode 189 breakpoints 190 state of application 191 delegate providing the content 63 delegates 24, 96, 159 Cocoa Touch 62 controls 63 implementing delegate methods in action sheets 523 pickers 65–66 table views 152 templates 152 Delegation pattern 24 Design Magnets 101 Solution 102, 106, 116–117 design patterns 41–98 Control-Datasource-Delegate 63 Delegation 24 Model-View-Controller 23, 24 View-ViewController 24 DetailedViewController 231 detail indicators in table view 578 detail view connecting to outlets 175 getting data into 206–210 iBountyHunter 427–436, 463–468 files that you need 428 populating view 434–435 “new” drink view 228–232 switching between list and detail views 177–182 view controllers instantiating 181 Developer Portal 612 Development Certificate 612 device checking 359 universal apps 364 devices checking specific capabilities 521 without cameras 527 dictionaries 249 array of 202 getting data into detail view 206–210 key-value pairs 184 organizing constants 207–209 plists 203–204 problem with 397

dictionary key names 210 didSelectRowAtIndexPath 303, 305, 311 directionsTextView 170, 174, 175 directory permissions 425 dismissModalViewControllerAnimated 245, 281 documentation, HIG (see HIG) Documents directory 422 copying database to 423 Done button and message passing 128 DrinkDetailViewController 343–348, 351–354 refreshView 345 UIScrollView 258 DrinkMixer app 141–196, 198–250, 251–314 AddDrinkViewController 231–240 saving new drinks 279–281 Anatomy of a Crash 199 App Layout Construction 221, 241 Solution 222 array hardcoded into implementation file 161 customizing table 152 debugging 187–193 common warning culprits 187 console 188 using debugger to investigate crash 200 Xcode 189–191 DetailedViewController 231 detail views 168–176 connecting to outlets 175 dictionaries organizing constants 207–209 plist of 203–204 directionsTextView 170, 174, 175 displaying drinks 154–158 drilling into data 168 editing tables through buttons on controller 223–227 getting data into detail view 206–210 ingredientsTextView 170, 174, 175 keyboard (see keyboard) migrating to iPad (see iPad, migrating to) modal view 236–239 navigation bar 242–246 name key 207 nameTextField 170, 174, 175 navigation bar 242–246 creating save and cancel buttons 244 writing save and cancel buttons 245 you are here 4   619

the index

DrinkMixer app (continued) navigation controller 151 maintaining stack of view controllers 180 switching between views 179 navigation template 146–148 “new” drink view 228–232 NSDictionary 165, 184, 185 on the iPad 316 plists 162–166 arrays 165 built-in types 163 creating empty 163–164 formatting and populating 163–164 populating table cells 154–158 refactoring code 220 reusing nib 230 RootViewController 156–157 debugging 285–286 saving array 295–296 saving new drinks 279–281 running on iPad without changes 317 switching between list and detail views 177–182 Table Cell Code Up 153 table view reloading 288 table views 146–148 Table View Up Close 151 UITableViewController 156 UITextField disabling 176 UITextViews disabling 176 user feedback 218–220 user interaction 145 view controllers defining behavior for view 229 instantiating 181 maintaining stack of 180 subclassing and extending 231 without nib 233 DrinksDirections.plist 185 DRY principle (Don’t Repeat Yourself) 542

620   Index

E Easy GUI REConstruction 256 Editing view construction 301 solution 302 Editor Pane 10 Editor Style 399 efficient results handling 486 email building with strings 134 custom input 101–102 InstaEmail (see InstaEmail) messaging framework 44 UITextField and send email button 133–137 writing app that can send 52 events 76 connecting to action 81–82 connecting to methods 29 connecting UITextField to 129 first responder 122, 124–140, 128 registering for 263 triggering 28 unregistering 263 exceptions 199–202 NSCF Dictionary 201 extracting string values from nib 604

F fetched data, viewing 412–417 fields 104 private fields 234 uneditable 239 File’s Owner 14, 32, 56, 73, 230 InstaEmailViewController 71 filtering data 477–485, 499 predicates 478 Fireside Chats IBActions speak louder than... a lot of things 78–79 UIWebView options 586–587 Universal App Distribution or not? xvi–xvii, 324–325 first responder to events 122–125, 128

the index

Five-Minute Mystery Case of the Missing Reservations 282 Solved 287 flipping images 504 Formatting Text Up Close 595 Foundation 11 framework 20, 536–543 Cocoa Touch (see Cocoa Touch) Geek Bits 11 MessageUI 52 nibs loaded by 26 free 120 Fugitive class 405–406 NSManagedObject 407 referencing rather than adding code to 554 Fugitive database, connecting Core Data to 417–424 FugitiveDetailViewController 428–436 adding properties and initialization code 465–467 latitude and longitude 532–533 updates 506, 507, 513, 516, 532, 536–541 FugitiveDossierViewController 571, 574–575, 581–584, 588, 591–593, 596 Fugitive entity adding properties 399 buiding in Managed Object Model 399 Fugitive List view building 382–383 FugitiveListViewController 481 Fugitive view controller creating new class for 379–381 FugtiveDetailViewController 573–575

G Game Kit framework 610 garbage collector 109 GDB debugger 9, 291 Geek Bits common warning culprits 187 Editor Style 399 frameworks 11 Fugitive class 455

registering with Apple’s Developer Program 526 targets 327 UIs 432 UITextField 129 getter method 105–106, 119, 139 creating new 488 good design vs. bad design 56 Google Maps MKMapView 545 GPS 534 graphics on iOS Game Kit 610 OpenGL 610 Quartz 610 GUI builder 56 guides 48 GUI editor (see Interface Builder)

H Handling Messages Up Close 126 hardware issues, iPad vs. iPhone 323 .h (header) file 10, 20, 102–103 declaring a message in 126 InstaEmailViewController 103–105 hierarchy view for the xib files 390 HIG (Human Interface Guidelines) app types 48 common issues to watch for 123 guidelines for pickers and buttons 51 iPad 319 user experience (UX) considerations for the iPad 567 HTML, UIWebView 588–596

I IBAction 31, 33–34, 39, 77–82, 105 changing button text 19 declaring in header file 80 method signatures 105 return type 128 vs. IBOutlet 78–79

you are here 4   621

the index

iBountyHunter app AppDelegate_iPhone 376, 377 AppDelegate_Shared 377, 423 building Captured List view 386 building detail view 427–436 files that you need 428 populating view 434–435 building Fugitive List view 382–383 CapturedListViewController (see CapturedListViewController) choosing template 372 clearing warnings in UITableViewController 387 connecting Core Data to Fugitive database 417–424 Core Data crash 452 filtering results 477–485 lightweight data migration 457 mismatch between database and model 452 requiring two models 454 updating data 449–451 Core Location (see Core Location) creating a consistent look and feel 580–583 creating new class for Fugitive view controller 379– 381 detail view 463–468 drawing how iPhone works 376–377 with universal app 377 FugitiveDetailViewController (see FugitiveDetailViewController) Fugitive entity (see Fugitive entity) handling changes to data model 445–500 iPad app design 365–367 iPad starter code 571 iPhone app design 368–371 managing data (see managing data) Map Kit (see Map Kit) nib 390 NSFetchedResultsController (see NSFetchedResultsController) object model 449 on iPad 570 pictures (see images) SourceTypePhotoLibrary 526 SQLite database 419 subviews 391–392 TabController view 381 to do list 378 UI design 463–468 622   Index

UITableViewController 379, 387, 414 wiring tab bar controller 386–387 (see also universal apps) iBountyHunterAppDelegate checking for changes 474 IBOutlet 19, 25–29, 31, 33–34, 39 adding IBOutlet and property to view controller 84 connecting picker to 86 dealloc method 85 getting data from picker 83–91 New Referencing Outlet 86 nibs 26 @synthesize directive 85 UITextField 102 vs. IBAction 78–79 ibtool 604 icons for applications 56 IDE 20 iDecide app 5–40 changing button text 19 connecting events to methods 29 folder 10 logic 18 template 11 triggering certain events 28 iDecideViewController 11–13, 18, 21–28, 35–37 logic 18 id type (Objective-C) 130 images 502–527 animation 504–509 CapturedPhotoViewController (see CapturedPhotoViewController) capturing HD video 521 flipping 504 photo library 521 picking 511–517 action sheets 522–527 stuffing images in database 578 takePictureButtonPressed action 515 modifying 523–525 video capture 521 (see also camera) immersive apps 48–50 implementation code 22 ingredientsTextView 170, 174, 175

the index

inheritance multiple 104 init call after alloc 120 InstaEmail app Cocoa Touch 55 creating custom view 55 creating new View-based project 52 exercise solution 58 life of root view 54–55 making keyboard go away 122–131 picker and button 51 @synthesize directive 85 UITextField (see UITextField) using pickers when you want controlled input 61 view controller (see InstaEmailViewController) InstaEmailAppDelegate 54 InstaEmailViewController 54–56, 68–72, 79–81, 84–90 building view exercise 57–58 File’s Owner 71 .h file 103–105 instance method 105, 130 instance variables syntax for 104 underscores after 159 integrating iPad and iPhone UIs 371 interface building in Xcode 14 nibs 31 Interface Builder 10, 12, 18, 20, 24, 28, 31–34, 39 Allow Selection While Editing 306 connecting UI controls to code 27 hooking button to buttonPressed method 26 (see IBAction; IBOutlet) interface vs. protocol 74 internationalization localizing resources 602 resources 605 translation 604 iOS apps read-only 421 iOS devices 1 iOS Human Interface Guide (see HIG)

iOS SDK 4, 11, 20, 33–34, 612 design patterns (see design patterns) downloading 6 install directory 7 Objective-C for iOS (see Objective-C for iOS) iOS Simulator 9, 33 limitations 527 iOS tables 171 iPad building apps for iPad vs. iPhone 323 hardware issues, iPad vs. iPhone 323 HIG (Human Interface Guidelines) 319 iBountyHunter 570 iBountyHunter and iPad app design 365–367 integrating iPad and iPhone UIs together 371 realism 567 user experience (UX) considerations for the iPad 567 video support 527 iPad Cross 357 Solution 358 iPad Exposed 320 iPad HIG 359 iPad, migrating to in DrinkMixer app 315–360 adding split view 329–333 App Delegate 334 checking devices 334 DrinkDetailViewController 343–348, 351–354 refreshView 345 RootViewController 334, 337, 344–346, 346–348 simulator 318 sketching for iPad 321–322 supporting all orientations 337–360 detail view controller 343–347 final resources directory 340 launch images 339–341 persistent view problem 345 popover 350–355 table view 346–347 UIPopoverController 351–353 UISplitViewController 350–355 UISplitViewDelegate methods 351–356 universal app distribution xvi–xvii universal apps distribution 324–325 using Xcode to build 326–328

you are here 4   623

the index

iPad UI (see NUIs) iPhone Application Programming Guide 48 application structure and where you can read/write 422 building app for iPad vs. iPhone 323 drawing how iPhone works in iBountyHunter 376– 377 hardware issues, iPad vs. iPhone 323 iBountyHunter and iPhone app design 368–371 integrating iPad and iPhone UIs together 371 popovers 356 toolbox 39, 96 video support 527 iPhone Application Programming Guide 48 iPhonecross 37, 92 Solution 38, 94 iPhoneDevcross 248 Solution 250 iPhone Simulator 7, 9, 11, 16–18, 31, 33–34, 39 common issues to watch out for 17 iterative development 568 iTunes App Store 4, 9, 20

K keyboard appearing 122 changing visible area 260 components that use 124 covering text in DrinkMixer app 252–262 UIScrollView 254–259 dimensions 276 events 276 registering for 267–268 state and size 269 making keyboard go away 122–131 notifications (see notifications) state and size 269 using BOOL track 269 Keyboard Code Magnets Part I 270–271 Solution 274 Keyboard Code Magnets Part II 272–273 Solution 275

624   Index

keys name key 207 key-value pairs dictionaries 184

L labels 171 latitude and longitude 528–533 launch screens before iPad 356 lightweight automatic migration 460 lightweight migration 499 adding variables 460 Link Binary With Libraries section 52 LLVM debugger 9 localizing nibs 602 Location Construction 531–533

M MainWindow.xib 54 malloc 120 Managed Object Context 403, 404, 417–418, 425, 440, 443–444, 499 new Fugitive entity 453 NSFetchRequest 410–411 resetting 475 saving new or changed items 474 viewing fetched data 412–417 Managed Object Model building Fugitive entity 399 Core Data Up Close 403 creating Fugitive class 405–406 describing entities with 398 Managed Object Model Construction 401 solution 402 managing data 394–427 Core Data data types 397 loading and saving data 396 overview 396 Managed Object Model (see Managed Object Model) NSFetchRequest 410–411

the index

NSManagedObject 409 viewing fetched data 412–417 Map Kit 545–554 annotations 553–555 automatic NSManagedObject file generation 553 Core Location, difference between 547 framework 547 adding 548 MKMapView 545–550 requiring network connection 545 simulator 545 memory management 120, 139 larger apps 111 (see also references) UITableView 155 up close 112 memory problems 120 messages 127 handling 126 names 127 passing Done button 128 to other objects 124 UITextField 128–129 unknown selector 199 MessageUI Framework 52 methods arguments 130 connecting to events 29 declarations 105, 130 failing to implement a required method in a protocol 74 implementations 128, 130 instance 105 names 127 picker 76 private 32 signature for 128 .m (implementation) file 10 migrating to iPad (see iPad, migrating to) migration (see data migration) minus sign 105, 126 MKMapView 545–550 modal view 236–239 navigation bar 242–246

creating save and cancel buttons 244 writing save and cancel buttons 245 Model View Controller (MVC) 23, 31, 33–34 Model-View-Controller pattern 24, 155 multiple inheritance 104 multiple views 171 navigation template 146–148 MultipleViewscross 194 Solution 196 multitasking background time 473 rules of engagements 473 multi-touch 609 pinching gesture 609 mutableCopy 111

N named arguments 127 name field labels 171 name key 207 nameTextField 170, 174, 175 Navigation control built-in apps 148 navigation controller 148, 171, 195 common issues to watch out for 150 editing tables through buttons on 223–227 maintaining stack of view controllers 180 switching between views 179 UINavigationController 151 NavigationControllercross 310 Solution 312 navigation template 146–148, 195 new (creating object with) 111 New Project 52 New Referencing Outlet 27, 86 nibs 10, 171 after compilation 32 File’s Owner 230 graphical information 228 hierarchy view for 390 how nib becomes view 53–56 you are here 4   625

the index

nibs (continued) IBOutlets 26 Interface Builder 31, 32 loaded by framework 26 moving into Resources group 171 new view controller without a new nib 232 No Dumb Questions 56 opening in Xcode 14 reusing in DrinkMixer app 230 view controller without 233 (see also .xib files) nil 120 passing messages to 125 No Dumb Questions adding icons to tab bar tabs 390 allowEditing in UIImagePickerController 527 animation and curve properties in notification 276 Apple’s Developer Program 527 applicationWillTerminate 475 apps configuration information 47 controls 44 delegate providing the content 63 email messaging framework 44 events 76 failing to implement a required method in a protocol 74 File’s Owner 56 good design vs. bad design 56 GUI builder 56 icons 56 interface vs. protocol 74 layout 44 limits to the number of protocols a class can realize 74 nib 56 picker methods 76 rebuilding for other phones 20 starting with a sketch 44 usability and aesthetics 47 arrays 298 augmented reality with the camera 527 building apps for iPad vs. iPhone 323 building views 20 cancel button 291 captured field 468 compiler 108 constants declared with a “k” in front of them 400 626   Index

Core Data data validation 475 hash modifier 460 lightweight migration, renaming 460 migration outside of 460 NSError 475 persistence, types of 400 SQL datatypes/table structures 400 SQLite store 460 unsupported type 400 Core Location and Map Kit 547 location accuracy 542 speeding up initial position 542 starting and stopping 542 waiting rather than calling back to delegate 542 crashing apps 294 data model versions 460 datasource 171 data, ways to save and load 171 detail indicators in table view 578 detail view 356 devices without cameras 527 dictionary key names 210 doing UI after implementing functionality 578 DrinkMixer 425 DRY principle (Don’t Repeat Yourself) 542 efficiency 311 &error 475 File’s Owner 32 first responder to events 122 frameworks 547 free 120 GDB debugger 291 getting paths to other application directories 425 hardware issues, iPad vs. iPhone 323 .h file 20 iBountyHunter nib 390 init call after alloc 120 integrating iPad and iPhone UIs together 371 Interface Builder 20, 32 keyboard appear 122 keyboard dimensions 276 keyboard events 276 labels and name field 171 launch screens before iPad 356 malloc 120 Managed Object Context 425 resetting 475

the index

memory management 120 memory problems 120 Navigation control and built-in apps 148 Navigation controllers 148 navigation template 148 new view controller without a new nib 232 nil 120 nonatomic keyword 108 notifications 266 application going into background 294 needing to register 294 NSCoding 185 NSDecimalNumber 400 NSDictionary documentation valueForKey: and objectForKey 210 NSManagedObjects 425 NSManagedObjects, new instance 475 NSPredicate 496 NSSearchPathForDirectoriesInDomains 425 number of views in tab bar 390 Objective-C arguments to methods 127 brackets for message passing 130 constructors 120 frameworks 20 garbage collection 120 id type 130 methods and messages 127 objects unable to respond to a message 130 requirements 20 selectors 130 sending message back to sender 130 object models 425 outlet for save/cancel button 247 Persistent Store Coordinator 425 picking a fugitive without a last known location showing “null” 578 popovers 356 in iPhone 356 private fields 234 private interfaces 542 private methods 32, 578 refreshing table data 291 registering for backgrounding notification 298 releasing objects 120 reloading whole table 496 removing applications 425 restricting the hierarchy 323 results controller 496

retaining objects 120 reusing views 210 rollback: message 475 scroll view, manipulating 276 SQLite store 460 stuffing images in database 578 subclassing 234 switching apps 294 switch instead of segmented control 468 @ symbol 32, 159 tab bar controller embedding navigation control in 371 iPad 379 table view and 379 tables automatic editing support in table view 311 cell identifier 154 moving rows 311 restricting row deletion 311 section headers and footers 159 table cell initialization code 232 table views as root view 148 customizing cells 159 datasource and delegate 159 plain versus grouped 159 table cells and reusable list 154 tabs, user switching 390 testing camera in simulator 527 toggle switch 379 UISegmentControl 379 UITabBarDelegate 390 UI touches in iPad version 371 underscores after instance variables 159 video support 527 view controller 171 viewDidLoad and viewDidUnload methods registering and unregistering 298 Watch it! reusing nib 232 *.xib file, moving into Resources group 171 Xcode data modeler 496 hash modifier in Versioning Settings 460 IDE 20 launch orientations in 356 size information of a control 356 transient and indexed checkboxes 400 nonatomic keyword 108, 109 you are here 4   627

the index

Notification center exposed 264 notifications 262, 269, 313 animation and curve properties in 276 application going into background 294 can’t find list of 266 creating own 266 needing to register 294 registering for events 263 NSArray 165 NSCF Dictionary 202 exception 201 NSCoding 185 NSComparisonPredicate 479 NSCompoundPredicate 479 NSDecimalNumber 400 NSDictionary 165, 184, 185 NSDictionary documentation valueForKey: and objectForKey 210 NSExpression 479 NSFetch ResultsControllers 499 NSFetchedResultsController 496 creating new getter method 488 monitoring for data changes 486 multiple sections 496 refreshing data 493 table views 486 updating tableview delegate and datasource 489–491 NSFetchRequest 410–411, 496 setting predicate on 479–483 NSLocalizedString 605 NSManagedObject 407, 425, 440, 443, 450, 460, 474 new instance 475 properties 409 NSMutableArray 159 NSMutableDictionary 278, 281, 291, 307 NSNotificationCenter 262–263, 267–268, 296 NSPredicate 478–481, 488 conditions 496 full syntax for 496 NSSearchPathForDirectoriesInDomains 425

628   Index

NSSortDescriptor 292 NSString assign property for 110 building email with strings 134 @ symbol 32, 159 with the title for given row 72 NSStrings 69 NUI cross 598 Solution 600 NUIs (natural user interfaces) 563–600 about 566 iterative development 568 Toolboxes 599 NullPointerExceptions 125 numberOfComponentsInPickerView:pickerView method 70

O objectAtIndex message 124 Objective-C arguments to methods 127 brackets for message passing 130 constructors 120 garbage collection 120 id type 130 libraries (see frameworks) method arguments 130 method declarations 130 method implementations 130 methods and messages 127 objects unable to respond to a message 130 private framework headers 130 requirements 20 selectors 130 sending message back to sender 130 send messages to receivers 130 toolbox 139 UI behavior 18 UIWebView 588–596 Objective-C cross 138 Solution 140 Objective-C Exposed 119 Objective-C for iOS 99–140

the index

object models 425, 449 objects retaining and releasing 120 unable to respond to a message 130 Objects Library 13 OpenGL 610 orientations (see iPad, migrating to in DrinkMixer app) outlets 77 connecting picker to IBOutlet 86 connecting to detail view 175 declarations 84 New Referencing Outlet 86

P paths, getting to other application directories 425 patterns (see design patterns) persistence, types of 400 Persistent Object Store 403, 418–419, 425, 453–456, 459, 499 Exposed 459 Persistent Store Coordinator 403, 418, 419, 425, 453 photo library 521 picking image from 522 photos (see images) pickers 74, 96 connecting picker to IBOutlet 86 data 60 controlled input 61 datasource 62, 65–66 delegates 65–66 Exposed 64–65 getting data from picker using IBOutlet 83–91 HIG guidelines 51 methods 76 using reference to pull selected values 87 pickerView:numberOfRowsInComponent method 67, 70 pickerView:titleForRow:forComponent method 67, 72 pictures (see images) plists 195 Anatomy of a Crash 199 arrays 165 built-in types 163 code used to save 295

code used to save plist 421 creating empty 163–164 dictionaries 203–204 DrinkMixer app 162–166 DrinksDirections 185 formatting and populating 163–164 problem with 397 pointer types 104 popovers 350–356 in iPhone 356 predicates 478 NSPredicate (see NSPredicate) setting on NSFetchRequest 479–483 prepareFugitiveDescription 573–575 prepareMapDescription 573–575 private fields 234 private framework headers 130 private interfaces 542 private methods 32 productivity apps 48–50, 171 DrinkMixer (see DrinkMixer app) protocols 96 angle brackets 104 datasource 67–74 adding implementation code 68–69 declaring controller conforms to both protocols 68 required methods 70 limits to the number of protocols a class can realize 74 Provisioning Profile 612

Q Quartz 610

R readonly 108, 109, 139 readwrite 108, 139 Ready Bake Code CapturedPhotoViewController.m 512 DrinksDirections.plist 185 iBountyHunter iPad starter code 571 messaging API 89–90 you are here 4   629

the index

Ready Bake plist 165 realism 567 refactoring code 589–592 references counting 109 determining how many left 113–115 registering for events 263 releasing objects 120 restricting the hierarchy 323 results controller 496 retain 108–109, 139 retaining objects 120 Return Key popup menu 123 return type 128 rollback: message 475 root view life of 54–55 table view as 148 RootViewController 334, 337, 344, 346–347 debugging 285–286 DrinkMixer app 156–157 saving array 295–296 saving new drinks 279–281 UITableView 151

S scroll view 254–262, 313 manipulating 276 subviews 254 vs. content view 260 SDK (see iOS SDK) section headers and footers 159 selectedRowInComponent: method 87 selectors 130 unknown 199 sendButtonTapped 103, 105, 125–126, 128, 135–136 sendButtonTapped method 87 setter method 106, 107, 108, 109, 119, 139

630   Index

showFugitiveDossier 573–575 simulator 7, 9, 10, 33 blue guide lines 58 camera 518, 527 code used to save plist 295 directory permissions 425 InstaEmail 58, 59, 118 iPad 317–318 selecting 317 iPhone 6, 31 limitations 17 Map Kit 545 rotating 355 uninstalling old version of app from 424 vs. actual devices 120 when app crashes in 187 Xcode’s debugging pane 188 sketches 568 sorting 313 sorting array 292 SourceTypePhotoLibrary 526 split-view controller 359 BountyHunterHD 570 Split-view Magnets 329 Solution 330–333 SQL datatypes/table structures 400 SQLite database and automatic migration 460 iBountyHunter 419 SQLite store Core Data and optimizing migration 460 static method 130 strings building email with 134 strings files 604 strongly typed data 397 subclassing 234 subviews 391–392 scroll view 254 switching tabs 390

the index

T tab bar number of views 390 tab bar controller 361 embedding navigation control 371 iPad 379 table view and 379 Tab Bar Up Close 368 TabController view 381 Table Cell Code Up 153 table cell initialization code 232 Table Cell Magnets 413 Solution 416 Table Cells Up Close 214 tables 171, 195 arrays (see arrays) automatic editing support in table view 311 cells identifier 154 populating 154–158 reusable list 154 customizing 152 editing through buttons on controller 223–227 memory management 155 moving rows 311 NSMutableArray 159 refreshing table data 291 restricting row deletion 311 section headers and footers 159 Table Cell Code Up 153 UITableView (see UITableView) tablet computing 566 table view 171 as root view 148 customizing cells 159 datasource 152, 159 delegates 152 editing 313 editing and deleting data 300–307 iPad, migrating to in DrinkMixer app 346–347 navigation template 146–148 NSFetchedResultsControllers 486 plain versus grouped 159

reloading 288 sorting array 292 standard pattern 168 switching between views 179 templates 152 UINavigationController 151 UITableView (see UITableView) up close 151 tabs, switching 390 takePictureButtonPressed action 515 modifying 523–525 targets 327 templates 9 choosing template for iBountyHunter 372 Create Local Git Repository 8 datasource 152 delegates 152 Device Family 8 iDecide 11 Include Unit Tests 8 multiple views 146 navigation 146–147 Product Name 8 table views 152 theme 580–583 Toggle Code Magnets 469 Solution 470 toggle switch 379 toolbar 11 Toolboxes Core Data 444 data 499 iOS 195, 249 iOS Development 313, 359 iPhone 39, 96 Location 561 NUIs 599 Objective-C 139 translating text in an app 604 tree diagram style 399 tutorials 48 types 104

you are here 4   631

the index

U UIApplicationDelegate 54 UIApplicationDidEnterBackground 294 UIApplicationDidEnterBackgroundNotification 295, 296 UIApplicationMain 54 UI Design Magnets 143 Solution 144 UIImagePickerController 511–515, 521–526 allowEditing 527 checking specific capabilities 521 UIKeyboardDidHideNotification 267–269, 275 UIKeyboardDidShowNotification 262–263, 267–269, 276 UIKit 11, 21–22, 504 UIModalTransitionStyleFlipHorizontal 505, 507, 516 UINavigationController 151 UIPickerView selectedRowInComponent: method 87 UIPickerViewDataSource 62, 102–104, 127 protocol 67 required methods 70 UIPickerViewDelegate 72 UIPopoverController 351–353 UIs behavior 18 components 26 triggering events 28 connecting UI controls to code 27 detail view in iBountyHunter 463–468 doing UI after implementing functionality 578 Geek Bits 432 integrating iPad and iPhone UIs together 371 iPad (see NUIs) iterative development 568 NUIs (see NUIs) separating from behavior 229 sketching DrinkMixer app for iPad 321–322 theme 580–583 universal apps 364 UIScrollView 254–259 contentSize 257–258

632   Index

Easy GUI REConstruction 256 wrapping content in 255 UIScrollView Up 254 UISegmentControl 379 UISplitViewController 350–355 UISplitViewDelegate methods 351–356 UITabBarDelegate 390 UITableView 151–157, 181 memory management 155 NSFetchedResultsController 486 reusing cells 155 RootViewController 151 UITableViewController 379, 387, 414 clearing warnings 387 DrinkMixer app 156 UITextField 102–107, 116–117 components and 124 connecting to event 129 customizing 123 disabling 176 Geek Bits 129 giving up focus 125 IBOutlet 102 list of events UITextField can send 129 message passing 128–129 send email button 133–137 send message button 134 UITextView disabling 176 UIWebView 585–587 vs. UIWebView 586–587 UI touches in iPad version 371 UIViewController 54 UIWebView 599 Formatting Text Up Close 595 HTML, CSS and Objective-C 588–596 options 586–587 vs. UITextView 586–587 underscores after instance variables 159 undocumented APIs 32 universal apps 359, 364 App Store 364 device checking 364

the index

drawing how iBountyHunter works 377 structure 374 UIs 364 Universal App Distribution, pros and cons xvi–xvii, 324–325 using Xcode to build 326–328 unknown selector 199 unregistering 263 updateDossier 573–575 usability 47 user experience (UX) considerations for the iPad 567 Utilities Pane 13 utility apps 48–50

V variables underscores after instance variables 159 video capture 521 video support 527 View-based Application InstaEmail 52 view controllers 24, 171, 229 instantiating 181 maintaining stack of 180 making keyboard go away 122–131 new view controller without a new nib 232 subclassing and extending 231 views connected to code through 31 without nib 233 viewDidLoad 125 registering and unregistering 298 views 249 adding buttons 15 building 20 connected to code through view controller 31 detail (see detail views) modal (see modal views) multiple navigation template 148 root (see root view) View-ViewController pattern 24 viewWillAppear 263, 298, 412, 415, 433, 435, 496 viewWillDisappear 263, 298

W Watch it! Apple’s new devices and updating capabilities 520 automatic NSManagedObject file generation 553 code used to save plist 295, 421 Core Location and battery life 537 HIG 123 iPads vs. iPhones 564 keyboard notification 269 Map Kit requiring network connection 545 Navigation controller 150 new project type in Xcode 52 nil passing messages to 125 object model 400 private framework headers 130 reusing nib 230, 232 Simulator 17 stopping and hitting “Build and Debug” in Xcode 297 uneditable fields 239 uninstalling old version of your app 424 wire-frames 568 wrapping objects. See Adapter Pattern, Decorator Pattern, Facade Pattern

X Xcode 4–14, 195 as hub of iOS project 10–11 building interface in 14 Bullet Points 31 data modeler 496 debugging 189 breakpoints 190 debuggers 9 debugging pane 188 state of application 191 editing view files 13 functions 33–34 hash modifier in Versioning Settings 460 IDE 9, 20 launch orientations in 356 new project type in 52 Organizer 614 size information of a control 356 you are here 4   633

the index

Xcode (continued) starting 8 stopping and hitting “Build and Debug” in 297 templates 8, 9 transient and indexed checkboxes 400 using to build universal app 326–328 XML descriptions of view (see nib) Xcode Files Up Close 12 Xcode Magnets 135 Solution 136 Xcode Organizer 614 .xib files (see nibs) XML descriptions of view (see nib)

634   Index

Subscribe

© Copyright 2013 - 2019 AZDOC.PL All rights reserved.