Thông tin tài liệu
SECOND EDITION
Programming iOS 5
Matt Neuburg
Beijing
•
Cambridge
•
Farnham
•
Köln
•
Sebastopol
•
Tokyo
www.it-ebooks.info
Programming iOS 5, Second Edition
by Matt Neuburg
Revision History for the :
Dec 23, 2011
See http://oreilly.com/catalog/errata.csp?isbn=9781449319342 for release details.
ISBN: 978-1-449-31934-2
1326317995
www.it-ebooks.info
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Part I. Language
1. Just Enough C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Compilation, Statements, and Comments 4
Variable Declaration, Initialization, and Data Types 6
Structs 8
Pointers 10
Arrays 12
Operators 13
Flow Control and Conditions 15
Functions 20
Pointer Parameters and the Address Operator 22
Files 24
The Standard Library 27
More Preprocessor Directives 28
Data Type Qualifiers 29
2. Object-Based Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Objects 31
Messages and Methods 32
Classes and Instances 33
Class Methods 36
Instance Variables 37
The Object-Based Philosophy 38
3. Objective-C Objects and Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
An Instance Reference Is a Pointer 43
Instance References, Initialization, and nil 44
iii
www.it-ebooks.info
Instance References and Assignment 47
Instance References and Memory Management 48
Messages and Methods 50
Sending a Message 50
Declaring a Method 51
Nesting Method Calls 52
No Overloading 53
Parameter Lists 54
Unrecognized Selectors 54
Typecasting and the id Type 56
Messages as Data Type 60
C Functions 61
C Struct Pointers 62
Blocks 63
4. Objective-C Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Class and Superclass 67
Interface and Implementation 69
Header File and Implementation File 71
Class Methods 73
The Secret Life of Classes 74
5. Objective-C Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
How Instances Are Created 77
Ready-Made Instances 77
Instantiation from Scratch 78
Nib-Based Instantiation 81
Polymorphism 82
The Keyword self 84
The Keyword super 87
Instance Variables and Accessors 89
Key–Value Coding 91
Properties 92
How to Write an Initializer 94
Part II. IDE
6. Anatomy of an Xcode Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
New Project 99
The Project Window 101
The Navigator Pane 103
The Utilities Pane 107
iv | Table of Contents
www.it-ebooks.info
The Editor 109
The Project File and Its Dependents 111
The Target 114
Build Phases 114
Build Settings 115
Configurations 117
Schemes and Destinations 118
From Project to App 120
Build Settings 122
Property List Settings 122
Nib Files and Storyboard Files 123
Other Resources 124
Code 126
Frameworks and SDKs 128
7. Nib Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
A Tour of the Nib-Editing Interface 134
The Dock 135
Canvas 136
Inspectors and Libraries 138
Nib Loading and File’s Owner 140
Making and Loading a Nib 142
Outlet Connections 143
More Ways to Create Outlets 148
More About Outlets 150
Action Connections 151
Additional Initialization of Nib-Based Instances 155
8. Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
The Documentation Window 158
Class Documentation Pages 159
Sample Code 163
Other Resources 164
Quick Help 164
Symbols 165
Header Files 165
Internet Resources 166
9. Life Cycle of a Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Choosing a Device Architecture 169
Localization 173
Editing Your Code 174
Autocompletion 175
Table of Contents | v
www.it-ebooks.info
Snippets 176
Live Syntax Checking and Fix-it 177
Navigating Your Code 178
Debugging 180
Caveman Debugging 180
The Xcode Debugger 183
Static Analyzer 188
Clean 189
Running in the Simulator 190
Running on a Device 191
Device Management 195
Version Control 196
Instruments 198
Distribution 202
Ad Hoc Distribution 204
Final App Preparations 205
Icons in the App 206
Other Icons 207
Launch Images 207
Screenshots 208
Property List Settings 209
Submission to the App Store 210
Part III. Cocoa
10. Cocoa Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Subclassing 215
Categories 218
Splitting a Class 219
Private Method Declarations 220
Protocols 221
Optional Methods 226
Some Foundation Classes 227
Useful Structs and Constants 227
NSString and Friends 228
NSDate and Friends 230
NSNumber 230
NSValue 231
NSData 231
Equality and Comparison 232
NSIndexSet 232
NSArray and NSMutableArray 233
vi | Table of Contents
www.it-ebooks.info
NSSet and Friends 234
NSDictionary and NSMutableDictionary 235
NSNull 237
Immutable and Mutable 237
Property Lists 238
The Secret Life of NSObject 238
11. Cocoa Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Reasons for Events 244
Subclassing 244
Notifications 246
Receiving a Built-In Notification 247
Unregistering 249
NSTimer 251
Delegation 251
Data Sources 255
Actions 256
The Responder Chain 261
Deferring Responsibility 261
Nil-Targeted Actions 262
Application Lifetime Events 263
Swamped by Events 268
12. Accessors and Memory Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Accessors 273
Key–Value Coding 275
Memory Management 279
Principles of Cocoa Memory Management 279
The Golden Rules of Memory Management 281
What ARC Is and What It Does 283
How Cocoa Objects Manage Memory 286
Autorelease 288
Memory Management of Instance Variables (Non-ARC) 291
Memory Management of Instance Variables (ARC) 295
Retain Cycles and Weak References 297
Nib Loading and Memory Management 303
Memory Management of Global Variables 305
Memory Management of Pointer-to-Void Context Info 306
Memory Management of CFTypeRefs 308
Properties 310
13.
Data Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Model–View–Controller 317
Table of Contents | vii
www.it-ebooks.info
Instance Visibility 319
Visibility by Instantiation 320
Visibility by Relationship 321
Global Visibility 322
Notifications 323
Key–Value Observing 324
Part IV. Views
14. Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
The Window 333
Subview and Superview 336
Frame 339
Bounds and Center 341
Layout 344
Transform 347
Visibility and Opacity 351
15. Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
UIImage and UIImageView 353
Graphics Contexts 357
UIImage Drawing 361
CGImage Drawing 362
CIFilter and CIImage 365
Drawing a UIView 368
Graphics Context Settings 370
Paths and Drawing 371
Clipping 375
Gradients 376
Colors and Patterns 378
Graphics Context Transforms 380
Shadows 382
Points and Pixels 383
Content Mode 383
16. Layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
View and Layer 388
Layers and Sublayers 390
Manipulating the Layer Hierarchy 391
Positioning a Sublayer 392
CAScrollLayer 393
Layout of Sublayers 394
viii | Table of Contents
www.it-ebooks.info
Drawing in a Layer 394
Content Resizing and Positioning 397
Layers that Draw Themselves 399
Transforms 400
Depth 404
Shadows, Borders, and More 406
Layers and Key–Value Coding 409
17. Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Drawing, Animation, and Threading 412
UIImageView and UIImage Animation 415
View Animation 417
Animation Blocks 417
Modifying an Animation Block 418
Transition Animations 422
Block-Based View Animation 423
Implicit Layer Animation 428
Animation Transactions 429
Media Timing Functions 430
Core Animation 432
CABasicAnimation and Its Inheritance 432
Using a CABasicAnimation 434
Keyframe Animation 437
Making a Property Animatable 438
Grouped Animations 439
Transitions 443
The Animations List 445
Actions 447
What an Action Is 447
The Action Search 448
Hooking Into the Action Search 449
Nonproperty Actions 452
Emitter Layers 453
18. Touches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
Touch Events and Views 462
Receiving Touches 464
Restricting Touches 465
Interpreting Touches 466
Gesture Recognizers 471
Gesture Recognizer Classes 471
Multiple Gesture Recognizers 476
Subclassing Gesture Recognizers 477
Table of Contents | ix
www.it-ebooks.info
Gesture Recognizer Delegate 479
Touch Delivery 481
Hit-Testing 482
Initial Touch Event Delivery 487
Gesture Recognizer and View 488
Touch Exclusion Logic 489
Recognition 490
Touches and the Responder Chain 491
Part V. Interface
19.
View Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
The View Controller Hierarchy 498
View Controller and View Creation 502
Manual View 505
Generic Automatic View 507
View in a Separate Nib 509
Nib-Instantiated View Controller 512
Storyboard-Instantiated View Controller 515
Rotation 517
Initial Orientation 519
Rotation Events 522
Presented View Controller 523
Presented View Animation 528
Presentation Styles 529
Presented Views and Rotation 532
Tab Bar Controllers 534
Tab Bar Items 535
Configuring a Tab Bar Controller 536
Navigation Controllers 538
Bar Button Items 542
Navigation Items 544
Toolbar Items 546
Configuring a Navigation Controller 547
Page View Controller 549
Container View Controllers 552
Storyboards 554
View Controller Lifetime Events 560
View Controller Memory Management 562
20. Scroll Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
Creating a Scroll View 568
x | Table of Contents
www.it-ebooks.info
[...]...Scrolling Paging Tiling Zooming Zooming Programmatically Zooming with Detail Scroll View Delegate Scroll View Touches Scroll View Performance 57 1 57 4 57 5 57 7 57 9 57 9 58 2 58 4 58 9 21 Table Views 59 1 Table View Cells Built-In Cell Styles Custom Cells Table View Data The Three Big Questions Table View Sections Refreshing Table View Data Variable... the book up to date for iOS 5 You, the reader, might be coming to iOS programming for the first time, so this edition assumes no prior knowledge of iOS 4 or any previous version On the other hand, you, like me, could be making the transition from iOS 4 to iOS 5, so this edition lays some special emphasis on features that are new in iOS 5 This emphasis could also be useful to new iOS programmers who are... development process However, for iOS programming, I recommend adoption of Xcode 4, and the first edition of this book assumed that the reader had adopted it Such was the situation in May 2011, when the first edition was formally released, describing how to program iOS 4 Less than five months later, in October 2011, Apple released iOS 5 Some of the features that are new in iOS 5 are dramatic and pervasive,... Rearranging Table Items Dynamic Table Content Table View Menus 59 4 59 5 601 609 610 614 618 619 621 627 628 634 637 638 640 642 643 644 22 Popovers and Split Views 647 Configuring and Displaying a Popover Managing a Popover Dismissing a Popover Popover Segues Automatic Popovers Split Views 649 654 654 658 659 661 23 Text ... before iOS 4.2 have been excised from this edition Here is a case in point, showing my attitude and pedagogical approach with regard to new iOS 5 features in this edition iOS 5 introduces ARC (automatic reference counting), which changes the way in which Objective-C programmers manage object memory so profoundly as to render Objective-C a different language Use of ARC is optional in programming iOS, ... useful to new iOS programmers who are thinking of writing apps that can also run under iOS 4 My goal, however, is not to burden the reader with outdated information The vast majority of devices that could run iOS 4 have probably been updated to iOS 5, and you will probably be right in assuming that there will plenty of iOS 5 users out there, without your having to bother to target earlier systems And from... Reading Files User Defaults File Sharing Document Types Handing Off a Document XML SQLite Image File Formats 8 45 846 847 849 851 851 853 856 862 863 37 Basic Networking 867 HTTP Requests Bonjour Push Notifications Beyond Basic Networking 867 873 8 75 876 38 Threads 877 The Main Thread Why Threading... fundamentals of iOS I love Cocoa and have long wished to write about it, but it is iOS and its popularity that has given me a proximate excuse to do so Indeed, my working title was “Fundamentals of Cocoa Touch Programming. ” Here I have attempted to marshal and expound, in what I hope is a pedagogically helpful and instructive yet ruthlessly Euclidean and logical order, the principles on which sound iOS programming. .. program for iOS, you need some knowledge of the C programming language, for two reasons: • Most of your iOS programming will be in the Objective-C language, and ObjectiveC is a superset of C This means that Objective-C presupposes C; everything that is true of C trickles up to Objective-C A common mistake is to forget that “Objective-C is C” and to neglect a basic understanding of C • Some of the iOS API... 743 Alert View Action Sheet Dialog Alternatives Local Notifications 744 746 750 751 Part VI Some Frameworks 27 Audio 759 System Sounds Audio Session Audio Player Remote Control of Your Sound xii | Table of Contents www.it-ebooks.info 759 760 764 766 Playing Sound in the Background Further Topics in Sound 768 769 28 Video . . . 56 7
Creating a Scroll View 56 8
x | Table of Contents
www.it-ebooks.info
Scrolling 57 1
Paging 57 4
Tiling 57 5
Zooming 57 7
Zooming Programmatically 57 9
Zooming. Controller 51 2
Storyboard-Instantiated View Controller 51 5
Rotation 51 7
Initial Orientation 51 9
Rotation Events 52 2
Presented View Controller 52 3
Presented
Ngày đăng: 23/03/2014, 02:20
Xem thêm: Programming iOS 5 pptx, Programming iOS 5 pptx, Chapter 3. Objective-C Objects and Messages, Chapter 6. Anatomy of an Xcode Project, Chapter 9. Life Cycle of a Project, Chapter 12. Accessors and Memory Management, Chapter 22. Popovers and Split Views, Chapter 25. Controls and Other Views