programming ios 6 3rd edition

1.2K 1.2K 0
programming ios 6 3rd edition

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

www.it-ebooks.info www.it-ebooks.info Matt Neuburg THIRD EDITION Programming iOS 6 www.it-ebooks.info Programming iOS 6, Third Edition by Matt Neuburg Copyright © 2013 Matt Neuburg. 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 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 corporate@oreilly.com. Editor: Rachel Roumeliotis Production Editor: Kristen Borg Proofreader: O’Reilly Production Services Indexer: Matt Neuburg Cover Designer: Randy Comer Interior Designer: David Futato Illustrator: Matt Neuburg March 2013: Third Edition Revision History for the Third Edition: 2013-02-27: First release See http://oreilly.com/catalog/errata.csp?isbn=9781449365769 for release details. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Programming iOS 6, the image of a kingbird, 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 trade‐ mark 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 author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. ISBN: 978-1-449-36576-9 [LSI] www.it-ebooks.info Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Part I. Language 1. Just Enough C. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Compilation, Statements, and Comments 5 Variable Declaration, Initialization, and Data Types 6 Structs 9 Pointers 11 Arrays 14 Operators 15 Flow Control and Conditions 17 Functions 21 Pointer Parameters and the Address Operator 24 Files 26 The Standard Library 30 More Preprocessor Directives 30 Data Type Qualifiers 31 2. Object-Based Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Objects 35 Messages and Methods 36 Classes and Instances 37 Class Methods 40 Instance Variables 41 The Object-Based Philosophy 42 3. Objective-C Objects and Messages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 An Instance Reference Is a Pointer 47 iii www.it-ebooks.info Instance References, Initialization, and nil 49 Instance References and Assignment 51 Instance References and Memory Management 52 Messages and Methods 54 Sending a Message 54 Declaring a Method 55 Nesting Method Calls 56 No Overloading 57 Parameter Lists 58 Unrecognized Selectors 59 Typecasting and the id Type 61 Messages as Data Type 65 C Functions 66 CFTypeRefs 67 Blocks 68 4. Objective-C Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Class and Superclass 75 Interface and Implementation 77 Header File and Implementation File 79 Class Methods 82 The Secret Life of Classes 83 5. Objective-C Instances. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 How Instances Are Created 85 Ready-Made Instances 85 Instantiation from Scratch 87 Nib-Based Instantiation 90 Polymorphism 91 The Keyword self 93 The Keyword super 97 Instance Variables and Accessors 98 Key–Value Coding 100 Properties 102 How to Write an Initializer 104 Part II. IDE 6. Anatomy of an Xcode Project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 New Project 110 The Project Window 112 iv | Table of Contents www.it-ebooks.info The Navigator Pane 113 The Utilities Pane 118 The Editor 119 The Project File and Its Dependents 122 The Target 125 Build Phases 125 Build Settings 126 Configurations 128 Schemes and Destinations 129 From Project to App 131 Build Settings 133 Property List Settings 133 Nib Files and Storyboard Files 134 Other Resources 135 Code 137 Frameworks and SDKs 139 7. Nib Management. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 A Tour of the Nib-Editing Interface 146 The Dock 147 Canvas 148 Inspectors and Libraries 151 Nib Loading and File’s Owner 153 Making and Loading a Nib 154 Outlet Connections 156 More Ways to Create Outlets 161 More About Outlets 164 Action Connections 165 Additional Initialization of Nib-Based Instances 168 8. Documentation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 The Documentation Window 172 Class Documentation Pages 174 Sample Code 177 Other Resources 178 Quick Help 178 Symbols 179 Header Files 180 Internet Resources 180 9. Life Cycle of a Project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Device Architecture and Conditional Code 183 Table of Contents | v www.it-ebooks.info Localization 188 Editing Your Code 192 Autocompletion 193 Snippets 195 Fix-it and Live Syntax Checking 195 Navigating Your Code 196 Debugging 199 Caveman Debugging 199 The Xcode Debugger 202 Unit Testing 208 Static Analyzer 208 Clean 209 Running in the Simulator 210 Running on a Device 212 Profile and Device Management 215 Version Control 216 Instruments 219 Distribution 223 Ad Hoc Distribution 225 Final App Preparations 226 Icons in the App 226 Other Icons 227 Launch Images 228 Screenshots 230 Property List Settings 231 Submission to the App Store 233 Part III. Cocoa 10. Cocoa Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 Subclassing 239 Categories 242 Splitting a Class 243 Class Extensions 244 Protocols 245 Optional Methods 250 Some Foundation Classes 252 Useful Structs and Constants 252 NSString and Friends 253 NSDate and Friends 255 NSNumber 256 vi | Table of Contents www.it-ebooks.info NSValue 257 NSData 257 Equality and Comparison 258 NSIndexSet 258 NSArray and NSMutableArray 259 NSSet and Friends 261 NSDictionary and NSMutableDictionary 262 NSNull 264 Immutable and Mutable 264 Property Lists 265 The Secret Life of NSObject 266 11. Cocoa Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 Reasons for Events 270 Subclassing 270 Notifications 272 Receiving a Built-In Notification 273 Unregistering 275 NSTimer 276 Delegation 277 Data Sources 280 Actions 281 The Responder Chain 283 Deferring Responsibility 284 Nil-Targeted Actions 285 Swamped by Events 286 Delayed Performance 289 Application Lifetime Events 292 12. Accessors and Memory Management. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 Key–Value Coding 298 KVC and Outlets 300 Key Paths and Array Accessors 301 Memory Management 303 Principles of Cocoa Memory Management 303 The Golden Rules of Memory Management 305 What ARC Is and What It Does 307 How Cocoa Objects Manage Memory 310 Autorelease 312 Memory Management of Instance Variables (Non-ARC) 316 Memory Management of Instance Variables (ARC) 320 Retain Cycles and Weak References 321 Table of Contents | vii www.it-ebooks.info Unusual Memory Management Situations 324 Nib Loading and Memory Management 329 Memory Management of Global Variables 330 Memory Management of Pointer-to-Void Context Info 331 Memory Management of CFTypeRefs 332 Properties 335 Property Memory Management Policies 336 Property Declaration Syntax 338 Property Accessor Synthesis 340 Dynamic Accessors 342 13. Data Communication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347 Model–View–Controller 347 Instance Visibility 349 Visibility by Instantiation 350 Visibility by Relationship 351 Global Visibility 352 Notifications 353 Key–Value Observing 354 Part IV. Views 14. Views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363 The Window 364 Subview and Superview 367 Visibility and Opacity 369 Frame 370 Bounds and Center 371 Transform 375 Layout 380 Autoresizing 381 Autolayout 383 Constraints in the Nib 393 Order of Layout Events 398 Autolayout and View Transforms 399 15. Drawing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403 UIImage and UIImageView 403 Graphics Contexts 409 UIImage Drawing 412 CGImage Drawing 414 viii | Table of Contents www.it-ebooks.info [...]... State 583 585 588 592 595 599 60 4 60 5 60 8 60 9 61 0 61 1 61 2 61 4 61 7 61 9 62 2 62 2 62 5 63 0 63 3 63 5 63 7 64 2 64 2 64 6 64 9 65 1 65 3 66 1 20 Scroll Views 66 7 Creating a Scroll View Scrolling Paging Tiling Zooming Zooming Programmatically Zooming with Detail Scroll View Delegate Scroll View Touches 66 7 67 3 67 6 67 7 67 9 68 2 68 2 68 5 68 7 Table of Contents www.it-ebooks.info... purpose of this new edition, then, is to bring 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... program for iOS, you need to speak to iOS Everything you say to iOS will be in accordance with the iOS API (An API, for application programming interface, is a list or specification of things you are allowed to say when communicating.) Therefore, you will need some knowledge of the C programming language, for two reasons: • Most of the iOS API involves the Objective-C language, and most of your iOS programming. .. this; if you’re targeting iOS 5.0, do that; if you’re targeting iOS 4.3, do the other”) I believe I can justify such omission on the grounds that the previous editions of this book exist! If you’re targeting iOS 5, I’ve already described what to do, in the second edition; there’s no need to repeat myself here New iOS 6 features are, of course, both explained and adopted in this edition For ex‐ ample,... 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, de‐ scribing 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,... Layers that Draw Themselves Transforms Depth Shadows, Borders, and More Layer Efficiency Layers and Key–Value Coding 442 444 4 46 4 46 447 448 449 452 455 4 56 460 463 464 466 17 Animation 469 Drawing, Animation, and Threading UIImageView and UIImage Animation View Animation Animation Blocks Modifying an Animation Block Transition Animations... great in printed and PDF form Notes on the Third Edition Shortly after the official release of iOS 6 and Xcode 4.5, I began revising the code ex‐ amples in this book (available at https://github.com/mattneub) to use and demonstrate the new iOS features When that was done, I proceeded to rewrite the book text; while I was doing so, iOS 6. 1 and Xcode 4 .6 were released, so those are the versions that the... Content Table View Menus Collection Views 69 8 69 9 705 708 715 7 16 719 7 26 729 732 737 738 739 745 749 750 752 754 755 7 56 758 22 Popovers and Split Views 767 Configuring and Displaying a Popover Managing a Popover Dismissing a Popover Popovers and Presented Views Popover Segues Automatic Popovers Split Views 769 774 774 778 778 780 782 23 Text ... of the second edition of this book with the first edition, it will help if I first recap the recent history of iOS and Xcode versions At the time I started writing the first edition this book, system versions 3.1.3 (on the iPhone) and 3.2 (on the iPad) were current As I was working on the book, iOS 4 and the iPhone 4 came into being, but iOS 4 didn’t yet run on the iPad Subsequently iOS 4.2 emerged;... Gyroscope Part VII 1030 1035 10 36 1037 1038 1042 Final Topics 36 Persistent Storage 1051 The Sandbox Basic File Operations Saving and Reading Files User Defaults File Sharing Document Types Handing Off a Document The Document Architecture iCloud XML SQLite Core Data Image File Formats 1052 1053 1054 10 56 1058 1059 1 060 1 064 1 068 1070 1077 1078 1084 37 . 66 7 Creating a Scroll View 66 7 Scrolling 67 3 Paging 67 6 Tiling 67 7 Zooming 67 9 Zooming Programmatically 68 2 Zooming with Detail 68 2 Scroll View Delegate 68 5 Scroll View Touches 68 7 Table of Contents. Controllers 61 4 Bar Button Items 61 7 Navigation Items 61 9 Toolbar Items 62 2 Configuring a Navigation Controller 62 2 Page View Controller 62 5 Container View Controllers 63 0 Storyboards 63 3 Segues 63 5 Unwind. www.it-ebooks.info www.it-ebooks.info Matt Neuburg THIRD EDITION Programming iOS 6 www.it-ebooks.info Programming iOS 6, Third Edition by Matt Neuburg Copyright © 2013 Matt Neuburg. All rights

Ngày đăng: 05/05/2014, 12:15

Từ khóa liên quan

Mục lục

  • Copyright

  • Table of Contents

  • Preface

    • Conventions Used in This Book

    • Using Code Examples

    • Safari® Books Online

    • How to Contact Us

    • Acknowledgments for the First Edition

    • Notes on the Second Printing

    • Acknowledgments for the Second Edition

    • Notes on the Second Edition

    • Acknowledgments for the Third Edition

    • Notes on the Third Edition

    • Part I. Language

      • Chapter 1. Just Enough C

        • Compilation, Statements, and Comments

        • Variable Declaration, Initialization, and Data Types

        • Structs

        • Pointers

        • Arrays

        • Operators

        • Flow Control and Conditions

        • Functions

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan