ebook img

iOS Programming: The Big Nerd Ranch Guide (2nd Edition) (Big Nerd Ranch Guides) PDF

548 Pages·2011·46.53 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview iOS Programming: The Big Nerd Ranch Guide (2nd Edition) (Big Nerd Ranch Guides)

ptg iOS Programming THE BIG NERD RANCH GUIDE JOE CONWAY & AARON HILLEGASS ptg iOS Programming iOS Programming: The Big Nerd Ranch Guide by Joe Conway and Aaron Hillegass Copyright © 2011 Big Nerd Ranch, Inc. All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recoring, or likewise. For information regarding permissions, contact Big Nerd Ranch, Inc. 154 Krog Street Suite 100 Atlanta, GA 30307 (404) 478-9005 http://www.bignerdranch.com/ Acknowledgments While our names appear on the cover, many people helped make this book a reality. We would like to take this chance to thank them. • The other instructors who teach the iOS Bootcamp fed us with a never-ending stream of suggestions and corrections. They are Scott Ritchie, Brian Hardy, Mikey Ward, Christian Keur, Alex Silverman, and Alex von Below. • Our tireless editor, Susan Loper, took our distracted mumblings and made them into readable prose. • Several technical reviewers helped us find and fix flaws. They are Bill Monk, Mark Miller, and Jonathan Saggau. • Ellie Volckhausen designed the cover. (The photo is of the bottom bracket of a bicycle frame.) • Chris Loper at IntelligentEnglish.com designed and produced the EPUB and Kindle versions. • The amazing team at Pearson Technology Group patiently guided us through the business end of book publishing. The final and most important thanks goes to our students whose questions inspired us to write this book and whose frustrations inspired us to make it clear and comprehensible. ptg iii This page intentionally left blank ptg Table of Contents Introduction .......................................................................................................... ..x.i.ii.... Prerequisites. ................................................................................................ ..x.i.ii. ... What’s Changed in the Second Editio.n..?.. ............................................................ .x.i.ii Our Teaching Philosoph.y. ................................................................................. .x.i.v. . How To Use This Book. .................................................................................... .x..v. .. How This Book Is Organize.d. ............................................................................ x..v. .. Style Choices ................................................................................................ .x..v..ii. . Typographical Convention.s. ............................................................................. x..v..ii. Necessary Hardware and Softwa..r.e.. ................................................................. .x.v..iii 1. A Simple iOS Application. ...................................................................................... .1.. ... Creating an Xcode Projec..t. ................................................................................. .2.. .. Building Interfaces ............................................................................................. .5.. .... Model-View-Controller ..................................................................................... .1..0.. . Declarations. .................................................................................................. .1..3.... Declaring instance variable..s. ...................................................................... .1..3. . Declaring methods.. ................................................................................... .1.4... Making Connections.. ....................................................................................... .1.5.... Setting pointers ....................................................................................... .1..5.. .. Setting targets and action..s. ....................................................................... .1..7.. Summary of connection.s.. ......................................................................... .1..9.. Implementing Methods.. .................................................................................... .2.0.. . ptg Build and Run on the Simulato..r. ....................................................................... .2..1.. Deploying an Application. .................................................................................. .2..3.. . Application Icons ............................................................................................. .2..4.. .. Launch Images ................................................................................................ .2..7.... 2. Objective-C ........................................................................................................ .3..1.. ... Objects .......................................................................................................... .3..1. .... Using Instance s................................................................................................ .3.2.... Creating objects ....................................................................................... .3.2.. .. Sending message..s. .................................................................................. .3..3.. . Destroying objects.. ................................................................................... .3.4... Writing the RandomPossessions To..o..l ................................................................ .3.5.. NSString ................................................................................................. .3.8.. .. NSArray and NSMutableArra.y. ................................................................... 3..9.. Subclassing an Objective-C Cla.s..s. ...................................................................... 4..0.. Instancev ariables. .................................................................................... .4.4.. .. Accessor method.s. ................................................................................... .4..5.. . Instancem ethods ..................................................................................... .4..8.. . Initializers ............................................................................................... .4..9. ... Other initializers and the initializer cha.i.n. ..................................................... .5..2 Class methods ......................................................................................... .5..3. .. Testing your subclas.s.. ............................................................................. .5..5.. Exceptions and the Console Windo..w.. ................................................................ .5..6.. Fast Enumeration ............................................................................................. .5..7.. .. Challenge. ...................................................................................................... .5..8.... v iOS Programming 3. Memory Managemen.t. .......................................................................................... .5..9. .. Memory Management Basic..s. ........................................................................... .5..9.. Managing memory in C.. ........................................................................... .5..9. . Managing memory with object.s.. ................................................................. .6.1.. Reference Counting.. ........................................................................................ .6.1.... Using retain counts.. .................................................................................. .6.1.. . Using autoreleas e..................................................................................... .6..4. .. Accessors and memory managem.e..n.t. .......................................................... .6.7. Implementing dealloc.. .............................................................................. .6..9. . Simplifying accessors with propertie..s.. .......................................................... 7..0. copy and mutableCop.y. ............................................................................ .7..2.. Retain count rules.. ................................................................................... .7..3... For the More Curious: More on Memory Managem.e..n..t .......................................... .7.4 4. Delegation and Core Locatio..n. ............................................................................... .7..7. . Projects, targets, and framewor.k..s. ...................................................................... .7..7. . Core Location ................................................................................................ .7..9.... Receiving updates from CLLocationManag..e.r. ................................................ .8..1 Delegation. .................................................................................................... .8..2.... Protocols. ............................................................................................... .8..3.. .. Delegation, controllers, and memory managem..e..n.t. ........................................ 8..5 Using the Debugge.r. ....................................................................................... .8..6.. . Challenge: Heading.. ........................................................................................ .9.1.. .. For the More Curious: Build Phases, Compiler Errors, and Linker E.r.r.o..r.s. .................. .91 ptg Preprocessing.. ........................................................................................ .9..2. .. Compiling ............................................................................................... .9..3. ... Linking ................................................................................................. .9..4.... 5. MapKit and Text Inpu.t ........................................................................................ .9..7.. .. Object Diagrams .............................................................................................. .9..7.. .. MapKit Framework. ......................................................................................... .9.9.. .. Interface Propertie.s. ........................................................................................ .9..9. .. Being a MapView Delegat.e. ............................................................................ .1..0..2 Using the documentatio.n.. ....................................................................... .1..0..3 Your own MKAnnotation. ......................................................................... .1..0..8 Tagging locations. ................................................................................... .1..1..2. Putting the pieces togeth.e.r. ...................................................................... .1..1..3 Challenge: Annotation Extra.s.. ......................................................................... .1..1..4 Challenge: Reverse Geocodi.n.g.. ....................................................................... 1..1..4 6. Subclassing UIView.. .......................................................................................... .1..1..5. . Creating a Custom View.. ................................................................................. .1.1..6. The drawRect: metho.d. ........................................................................... .1..1.6. Instantiating a UIView. ............................................................................ .1..1.8. Drawing Text and Shadow.s.. ........................................................................... .1..2..0 Using UIScrollView ....................................................................................... .1..2..1. Zooming ...................................................................................................... ..1.2..2.. . Hiding the Status Ba.r. .................................................................................... .1.2..3.. Challenge: Colors.. ......................................................................................... .1..2..5. . For the More Curious: Retain Cycl.e.s.. ............................................................... .1.2..5 For the More Curious: Redrawing View..s.. .......................................................... .1..2.5 vi iOS Programming 7. View Controllers. ............................................................................................... .1..2..7. . View Controllers and XIB Files.. ........................................................................ .1..2.7. Using View Controllers. ................................................................................... .1..2..9. Creating the UITabBarControlle..r. .............................................................. 1..3. 0 Creating view controllers and tab bar ite.m..s.. ............................................... .1..31 Creating views for the view controlle.r.s. ...................................................... .1..35 Appearing and Disappearing View..s.. ................................................................. .1.4..2 The View Controller Lifecycle and Low-Memory Warnin.g.s.. .................................... .1.42 View Controller Subclasses and Templa.t.e..s. ........................................................ .1.4. 6 Challenge: Map Tab.. ..................................................................................... .1..4..7. 8. The Acceleromete.r. ............................................................................................ .1..4.9.. . Setting Up the Acceleromete..r. ......................................................................... .1.4..9. Getting Accelerometer Dat.a.. ........................................................................... .1..5.1. Orientation and Scale of Acceleratio..n.. .............................................................. .1..5.1 Using Accelerometer Dat.a.. ............................................................................. .1..5.1. Smoothing Accelerometer Da.t.a. ....................................................................... .1.5..2. Detecting Shake s............................................................................................ .1..5..3. . Challenge: Changing Color.s.. ........................................................................... .1.5..5. For the More Curious: Filtering and Frequen..c.y.. .................................................. 1..5. 5 For the More Curious: Retina Displa..y. ............................................................... 1..5..6 9. Notification and Rotation.. ................................................................................... .1..5..9. Notification Center. ........................................................................................ .1..5.9.. UIDevice Notifications. .................................................................................... .1..6.0.. ptg Autorotation ................................................................................................. .1..6..1.. Setting autoresizing masks programmatically and bitwise opera..ti.o..n..s. .............. 167 Forcing Landscape Mod.e.. ............................................................................... .1..6..9. Challenge: Proximity Notification.s. .................................................................... .1.7..0 For the More Curious: Overriding Autorotatio..n.. .................................................. .1..70 10. UITableView and UITableViewControlle..r. ............................................................ .1..7.1 Beginning the Homepwner Applicatio.n.. ............................................................. .1..7.1 UITableViewController. .................................................................................... .1.7..2. Subclassing UITableViewControlle..r. ........................................................... .1.7. 3 UITableView’s Data Source.. ............................................................................ .1.7..6. PossessionStore: a singlet.o..n. .................................................................... 1..7..7 Implementing data source metho.d..s. .......................................................... .1..8.0 UITableViewCells .......................................................................................... .1.8..2.. Creating and retrieving UITableViewCel.l.s. .................................................. .1..83 Reusing UITableViewCells.. ....................................................................... .1.8..5 Code Snippet Library.. .................................................................................... .1..8.6.. Challenge: Section.s. ...................................................................................... .1..8..9. 11. Editing UITableView. ........................................................................................ .1.9..1.. Editing Mode ................................................................................................. .1.9..1.. Adding Rows ................................................................................................. .1.9..7.. Deleting Rows ............................................................................................... .1..9..8. . Moving Rows ................................................................................................ .1..9..9.. 12. UINavigationController. ..................................................................................... .2..0.3.. UINavigationController. .................................................................................... .2.0..4. UINavigationBar .................................................................................... .2..0..7. vii iOS Programming An Additional UIViewController. ........................................................................ .2.1..1 Navigating with UINavigationControlle.r. ............................................................. .2.1..7 Pushing view controller.s. ......................................................................... .2.1..7. Passing data between view controll.e.r.s.. ..................................................... .2..1.9 Appearing and disappearing view..s.. ........................................................... .2.2. 1 Challenge: Number Pa.d.. ................................................................................. .2..2.1.. 13. Camera ........................................................................................................... .2.2..3.. . Displaying Images and UIImageVie.w.. ............................................................... .2..2.3 Taking pictures and UIImagePickerControl.le..r. .............................................. .2..26 ImageStore. ........................................................................................... .2..3..1.. Creating and using key.s.. ........................................................................ .2..3..4 Dismissing the keyboar.d. ......................................................................... .2.3..7. Challenge: Removing an Imag..e. ........................................................................ 2..3..8 For the More Curious: Recording Vide..o.. ........................................................... .2..3.8 14. UIPopoverController and Modal View Controlle..r.s. ................................................. .2..41 Universalizing Homepwne.r. ............................................................................. .2..4.2. UIPopoverController. ...................................................................................... .2..4.4.. Modal View Controllers. ................................................................................... 2..4..6. Dismissing modal view controller.s.. ............................................................ .2..4.9 Modal view controller styles.. .................................................................... .2..5.0 Writing a view controller delegate protoc.o..l ................................................. .2..52 Modal view controller transition.s. .............................................................. .2..5.4 15. Saving, Loading, and Multitaskin..g. ...................................................................... .2..5..7 ptg Application Sandbox. ...................................................................................... .2.5..7.. Constructing a file path.. .......................................................................... .2..5.8. Archiving .................................................................................................... .2..6..0... Archiving objects. ................................................................................... .2..6..0. Unarchiving objects. ................................................................................. .2.6..2. Application States, Transitions, and Multitaski.n..g. ................................................ .2..65 Writing to filesystem with NSData.. .................................................................... 2..6..9 More on Low-Memory Warnings.. ...................................................................... .2..7..1 Model-View-Controller-Store Design Patte.r.n.. ..................................................... .2..7.2 Challenge: Archiving Wheream..i ........................................................................ .2.7..2 For The More Curious: Application State Transitio..n.s.. .......................................... .2..72 For the More Curious: Reading and Writing to the filesys.t.e..m.. ................................ 2. 73 For the More Curious: The Application Bund..le.. .................................................. .2..76 16. Subclassing UITableViewCe.l.l. ........................................................................... .2..7.9. Creating HomepwnerItemCe.l.l .......................................................................... .2.8..0. Creating subviews.. .................................................................................. .2.8..1. Laying out subviews. ............................................................................... .2.8..2. Using the custom ce.ll. ............................................................................ .2..8..3 Image Manipulation. ....................................................................................... .2.8..4.. Challenge: Accessory Indicato.r.s. ....................................................................... .2.8..9. Challenge: Shrinking the Main Imag..e. ............................................................... .2..8.9 17. Core Data ....................................................................................................... .2..9..1.. . Object-Relational Mapping.. ............................................................................. .2..9..1 Moving Homepwner to Core Dat.a.. .................................................................... .2..9..1 The model file ....................................................................................... .2..9.2.. viii iOS Programming NSManagedObject and subclass..e..s. .......................................................... .2..9.7 Updating PossessionSto.r.e.. ....................................................................... .3.0..1 Adding AssetTypes to Homepwn.e.r. ........................................................... .3..0.7 More About SQL ............................................................................................ .3.1..1.. Trade-offs of Persistence Mechanis.m..s.. ............................................................. 3..1..4 Challenge: New Asset Type..s. .......................................................................... .3.1..5. Challenge: Assets on the iP.a.d.. ........................................................................ 3..1..5 18. Localization .................................................................................................... .3..1.7.. . Internationalization using NSLoca.le.. ................................................................. .3..1..8 Localizing Resource.s. .................................................................................... .3..1..9. NSLocalizedString and Strings Tabl.e..s. .............................................................. 3..2..2 Challenge: Another Localizatio.n.. ....................................................................... 3..2..5 For the More Curious: NSBundle’s Role in Internationaliza.t.io..n.. ............................. .325 19. Settings .......................................................................................................... .3..2..7. .. Updating Wheream.i. ...................................................................................... .3.2..7.. NSUserDefaults. ............................................................................................. .3..2.9.. For the More Curious: The Settings Applicati.o..n. ................................................. .3..31 20. Touch Events and UIRespond..e.r. ........................................................................ .3.3..3. Touch Events ................................................................................................. .3.3..4.. Creating the TouchTracker Applicatio..n. ............................................................. .3..3.5 Turning Touches into Line.s. ............................................................................ .3..3..7 The Responder Chai.n.. ................................................................................... .3.4..0.. Challenge: Saving and Loadin..g. ........................................................................ .3.4..1 ptg Challenge: Circles.. ........................................................................................ .3..4.1.. For the More Curious: UIContro..l ...................................................................... .3..4..1 21. Instrument s..................................................................................................... .3..4.3.. . The Static Analyze.r ....................................................................................... .3..4.3.. Instruments. ................................................................................................. .3..4..5.. The Allocations Instrumen.t. ....................................................................... .3.4..5 Time Profiler Instrumen.t. ......................................................................... .3.5..3 Xcode Scheme s.............................................................................................. .3..5.5.. Creating a new schem.e.. ......................................................................... .3..5..7 Build Settings ................................................................................................ .3..5..9.. 22. Core Animation Laye.r. ..................................................................................... .3..6..3. Layers and views ............................................................................................ .3..6.3.. Creating a CALayer. ....................................................................................... .3.6..4.. Layer Conten t................................................................................................ .3..6.7.. . Implicitly Animatable Properties.. ....................................................................... .3..6..9 For the More Curious: Programmatically Generating Con.t.e..n..t ................................ .371 For the More Curious: Layers, Bitmaps, and Conte..x..t.s. ........................................ .3..72 Challenge: Dynamic Layer Conte.n..t .................................................................. .3.7..4 23. Controlling Animation with CAAnimation.. ............................................................. .3..7.5 Animation Objects. ........................................................................................ .3..7..5. . Spinning with CABasicAnimation.. ...................................................................... 3..7..8 Timing functions. .................................................................................... .3..8..1. Animation completion. ............................................................................. .3.8..2. Bouncing with a CAKeyframeAnimatio.n. ............................................................ .3..8.3 Challenge: More Animation.. ............................................................................ .3..8.4. ix

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.