Ebook IOS programming The big nerd ranch guide (5th edition) Part 1

316 262 0
Ebook IOS programming  The big nerd ranch guide (5th edition) Part 1

Đ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

(BQ) Part 1 book IOS programming The big nerd ranch guide has contents A simple IOS application, the swift language, views and the view hierarchy, text input and delegation, view controllers, programmatic views, localization, controlling animations,...and other contents.

iOS Programming: The Big Nerd Ranch Guide by Christian Keur and Aaron Hillegass Copyright © 2015 Big Nerd Ranch, LLC 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, recording, or likewise For information regarding permissions, contact Big Nerd Ranch, LLC 200 Arizona Ave NE Atlanta, GA 30307 (770) 817-6373 http://www.bignerdranch.com/ book-comments@bignerdranch.com The 10-gallon hat with propeller logo is a trademark of Big Nerd Ranch, LLC Exclusive worldwide distribution of the English edition of this book by Pearson Technology Group 800 East 96th Street Indianapolis, IN 46240 USA http://www.informit.com The authors and publisher have taken care in writing and printing this book but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein App Store, Apple, Cocoa, Cocoa Touch, Finder, Instruments, iCloud, iPad, iPhone, iPod, iPod touch, iTunes, Keychain, Mac, Mac OS, Multi-Touch, Objective-C, OS X, Quartz, Retina, Safari, and Xcode are trademarks of Apple, Inc., registered in the U.S and other countries 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 the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals ISBN-10 0134389395 ISBN-13 978-0134389394 Fifth edition, first printing, December 2015 Release E.5.1.1 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 First and foremost we would like to thank Joe Conway for his work on the earlier editions of this book He authored the first three editions and contributed greatly to the fourth edition as well Many of the words in this book are still his, and for that, we are very grateful A few people in particular went above and beyond with their help on this book They are Mikey Ward, Juan Pablo Claude, and Chris Morris The other instructors who teach the iOS Bootcamp fed us with a never-ending stream of suggestions and corrections They are Ben Scheirman, Bolot Kerimbaev, Brian Hardy, Chris Morris, JJ Manton, John Gallagher, Jonathan Blocksom, Joseph Dixon, Juan Pablo Claude, Mark Dalrymple, Matt Bezark, Matt Mathias, Mike Zornek, Mikey Ward, Pouria Almassi, Rod Strougo, Scott Ritchie, Step Christopher, Thomas Ward, TJ Usiyan, and Tom Harrington These instructors were often aided by their students in finding book errata, so many thanks are due to all the students who attend the iOS Bootcamp Thanks to all of the employees at Big Nerd Ranch who helped review the book, provided suggestions, and found errata Our tireless editor, Elizabeth Holaday, took our distracted mumblings and made them into readable prose Anna Bentley jumped in to provide proofing 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 print book and 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 Table of Contents Introduction Prerequisites What Has Changed in the Fifth Edition? Our Teaching Philosophy How to Use This Book Using an eBook How This Book Is Organized Style Choices Typographical Conventions Necessary Hardware and Software A Simple iOS Application Creating an Xcode Project Model-View-Controller Designing Quiz Interface Builder Building the Interface Creating view objects Configuring view objects Running on the simulator A brief introduction to Auto Layout Making connections Creating the Model Layer Implementing action methods Loading the first question Building the Finished Application Application Icons Launch Screen The Swift Language Types in Swift Using Standard Types Inferring types Specifying types Literals and subscripting Initializers Properties Instance methods Optionals Subscripting dictionaries Loops and String Interpolation Enumerations and the Switch Statement Enumerations and raw values Exploring Apple’s Swift Documentation Views and the View Hierarchy View Basics The View Hierarchy Creating a New Project Views and Frames Customizing the labels The Auto Layout System Alignment rectangle and layout attributes Constraints Adding constraints in Interface Builder Intrinsic content size Misplaced views Adding more constraints Bronze Challenge: More Auto Layout Practice Text Input and Delegation Text Editing Keyboard attributes Responding to text field changes Dismissing the keyboard Implementing the Temperature Conversion Number formatters Delegation Conforming to a protocol Using a delegate More on protocols Bronze Challenge: Disallow Alphabetic Characters View Controllers The View of a View Controller Setting the Initial View Controller UITabBarController Tab bar items Loaded and Appearing Views Accessing subviews Interacting with View Controllers and Their Views Silver Challenge: Dark Mode For the More Curious: Retina Display Programmatic Views Creating a View Programmatically Programmatic Constraints Anchors Activating constraints Layout guides Margins Explicit constraints Programmatic Controls Bronze Challenge: Another Tab Silver Challenge: User’s Location Gold Challenge: Dropping Pins For the More Curious: NSAutoresizingMaskLayoutConstraint Localization Internationalization Formatters Base internationalization Preparing for localization Localization NSLocalizedString and strings tables Bronze Challenge: Another Localization For the More Curious: NSBundle’s Role in Internationalization For the More Curious: Importing and Exporting as XLIFF Controlling Animations Basic Animations Closures Another Label Animation Completion Animating Constraints Timing Functions Bronze Challenge: Spring Animations Silver Challenge: Layout Guides UITableView and UITableViewController Beginning the Homepwner Application UITableViewController Subclassing UITableViewController Creating the Item Class Custom initializers UITableView’s Data Source Giving the controller access to the store Implementing data source methods UITableViewCells Creating and retrieving UITableViewCells Figure 10.8 Moving a row Note that simply implementing tableView(_:moveRowAtIndexPath:toIndexPath:) caused the reordering controls to appear The UITableView can ask its data source at runtime whether it implements tableView(_:moveRowAtIndexPath:toIndexPath:) If it does, then the table view adds the reordering controls whenever the table view enters editing mode Displaying User Alerts In this section, you are going to learn about user alerts and the different ways of configuring and displaying them User alerts can provide your application with a better user experience, so you will use them fairly often Alerts are often used to warn users that an important action is about to happen and perhaps give them the opportunity to cancel that action When you want to display an alert, you create an instance of UIAlertController with a preferred style The two available styles are UIAlertControllerStyle.ActionSheet and UIAlertControllerStyle.Alert (Figure 10.9) The ActionSheet style is used to present the user with a list of actions from which to choose The Alert type is used to display critical information to require the user to decide how to proceed The distinction may seem subtle, but if the user can back out of a decision or if the action is not critical, then an ActionSheet is probably the best choice Figure 10.9 UIAlertController styles You are going to use a UIAlertController to confirm the deletion of items You will use the ActionSheet style since the purpose of the alert is to confirm or cancel a possibly destructive action Open ItemsViewController.swift and modify tableView(_:commitEditingStyle:forRowAtIndexPath:) to ask the user to confirm or cancel the deletion of an item o​v​e​r​r​i​d​e​ ​f​u​n​c​ ​t​a​b​l​e​V​i​e​w​(​t​a​b​l​e​V​i​e​w​:​ ​U​I​T​a​b​l​e​V​i​e​w​,​ ​ ​ ​ ​ ​ ​ ​ ​c​o​m​m​i​t​E​d​i​t​i​n​g​S​t​y​l​e​ ​e​d​i​t​i​n​g​S​t​y​l​e​:​ ​U​I​T​a​b​l​e​V​i​e​w​C​e​l​l​E​d​i​t​i​n​g​S​t​y​l​e​,​ ​ ​ ​ ​ ​ ​ ​ ​f​o​r​R​o​w​A​t​I​n​d​e​x​P​a​t​h​ ​i​n​d​e​x​P​a​t​h​:​ ​N​S​I​n​d​e​x​P​a​t​h​)​ ​{​ ​ ​ ​ ​/​/​ ​I​f​ ​t​h​e​ ​t​a​b​l​e​ ​v​i​e​w​ ​i​s​ ​a​s​k​i​n​g​ ​t​o​ ​c​o​m​m​i​t​ ​a​ ​d​e​l​e​t​e​ ​c​o​m​m​a​n​d​.​.​.​ ​ ​ ​ ​i​f​ ​e​d​i​t​i​n​g​S​t​y​l​e​ ​=​=​ ​.​D​e​l​e​t​e​ ​{​ ​ ​ ​ ​ ​ ​ ​ ​l​e​t​ ​i​t​e​m​ ​=​ ​i​t​e​m​S​t​o​r​e​.​a​l​l​I​t​e​m​s​[​i​n​d​e​x​P​a​t​h​.​r​o​w​]​ ​ ​ ​ ​ ​ ​ ​ ​l​e​t​ ​t​i​t​l​e​ ​=​ ​"​D​e​l​e​t​e​ ​\​(​i​t​e​m​.​n​a​m​e​)​?​"​ ​ ​ ​ ​ ​ ​ ​ ​l​e​t​ ​m​e​s​s​a​g​e​ ​=​ ​"​A​r​e​ ​y​o​u​ ​s​u​r​e​ ​y​o​u​ ​w​a​n​t​ ​t​o​ ​d​e​l​e​t​e​ ​t​h​i​s​ ​i​t​e​m​?​"​ ​ ​ ​ ​ ​ ​ ​ ​l​e​t​ ​a​c​ ​=​ ​U​I​A​l​e​r​t​C​o​n​t​r​o​l​l​e​r​(​t​i​t​l​e​:​ ​t​i​t​l​e​,​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​m​e​s​s​a​g​e​:​ ​m​e​s​s​a​g​e​,​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​p​r​e​f​e​r​r​e​d​S​t​y​l​e​:​ ​.​A​c​t​i​o​n​S​h​e​e​t​)​ ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​R​e​m​o​v​e​ ​t​h​e​ ​i​t​e​m​ ​f​r​o​m​ ​t​h​e​ ​s​t​o​r​e​ ​ ​ ​ ​ ​ ​ ​ ​i​t​e​m​S​t​o​r​e​.​r​e​m​o​v​e​I​t​e​m​(​i​t​e​m​)​ ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​A​l​s​o​ ​r​e​m​o​v​e​ ​t​h​a​t​ ​r​o​w​ ​f​r​o​m​ ​t​h​e​ ​t​a​b​l​e​ ​v​i​e​w​ ​w​i​t​h​ ​a​n​ ​a​n​i​m​a​t​i​o​n​ ​ ​ ​ ​ ​ ​ ​ ​t​a​b​l​e​V​i​e​w​.​d​e​l​e​t​e​R​o​w​s​A​t​I​n​d​e​x​P​a​t​h​s​(​[​i​n​d​e​x​P​a​t​h​]​,​ ​w​i​t​h​R​o​w​A​n​i​m​a​t​i​o​n​:​ ​.​A​u​t​o​m​a​t​i​c​)​ ​ ​ ​ ​}​ }​ After determining that the user wants to delete an item, you create an instance of UIAlertController with an appropriate title and message describing what action is about to take place Also, you specify the ActionSheet style for the alert The actions that the user can choose from when shown an alert are instances of UIAlertAction, and you can add multiple ones regardless of the alert’s style Actions are added to the UIAlertController using the addAction(_:) method Add the necessary actions to the action sheet in tableView(_:commitEditingStyle:forRowAtIndexPath:) .​.​.​ l​e​t​ ​a​c​ ​=​ ​U​I​A​l​e​r​t​C​o​n​t​r​o​l​l​e​r​(​t​i​t​l​e​:​ ​t​i​t​l​e​,​ ​ ​ ​ ​m​e​s​s​a​g​e​:​ ​m​e​s​s​a​g​e​,​ ​ ​ ​ ​p​r​e​f​e​r​r​e​d​S​t​y​l​e​:​ ​.​A​c​t​i​o​n​S​h​e​e​t​)​ l​e​t​ ​c​a​n​c​e​l​A​c​t​i​o​n​ ​=​ ​U​I​A​l​e​r​t​A​c​t​i​o​n​(​t​i​t​l​e​:​ ​"​C​a​n​c​e​l​"​,​ ​s​t​y​l​e​:​ ​.​C​a​n​c​e​l​,​ ​h​a​n​d​l​e​r​:​ ​n​i​l​)​ a​c​.​a​d​d​A​c​t​i​o​n​(​c​a​n​c​e​l​A​c​t​i​o​n​)​ l​e​t​ ​d​e​l​e​t​e​A​c​t​i​o​n​ ​=​ ​U​I​A​l​e​r​t​A​c​t​i​o​n​(​t​i​t​l​e​:​ ​"​D​e​l​e​t​e​"​,​ ​s​t​y​l​e​:​ ​.​D​e​s​t​r​u​c​t​i​v​e​,​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​h​a​n​d​l​e​r​:​ ​{​ ​(​a​c​t​i​o​n​)​ ​-​>​ ​V​o​i​d​ ​i​n​ ​ ​ ​ ​/​/​ ​R​e​m​o​v​e​ ​t​h​e​ ​i​t​e​m​ ​f​r​o​m​ ​t​h​e​ ​s​t​o​r​e​ ​ ​ ​ ​s​e​l​f​.​i​t​e​m​S​t​o​r​e​.​r​e​m​o​v​e​I​t​e​m​(​i​t​e​m​)​ ​ ​ ​ ​/​/​ ​A​l​s​o​ ​r​e​m​o​v​e​ ​t​h​a​t​ ​r​o​w​ ​f​r​o​m​ ​t​h​e​ ​t​a​b​l​e​ ​v​i​e​w​ ​w​i​t​h​ ​a​n​ ​a​n​i​m​a​t​i​o​n​ ​ ​ ​ ​s​e​l​f​.​t​a​b​l​e​V​i​e​w​.​d​e​l​e​t​e​R​o​w​s​A​t​I​n​d​e​x​P​a​t​h​s​(​[​i​n​d​e​x​P​a​t​h​]​,​ ​w​i​t​h​R​o​w​A​n​i​m​a​t​i​o​n​:​ ​.​A​u​t​o​m​a​t​i​c​)​ }​)​ a​c​.​a​d​d​A​c​t​i​o​n​(​d​e​l​e​t​e​A​c​t​i​o​n​)​ ​.​.​ The first action has a title of “Cancel” and is created using the Cancel style The Cancel style results in text in a standard blue font This action will allow the user to back out of deleting an Item The handler parameter allows a closure to be executed when that action occurs Since no other action is needed, nil is passed as the argument The second action has a title of “Delete” and is created using the Destructive style Since destructive actions should be clearly marked and noticed, the Destructive style results in bright red text If the user selects this action, then the item and the table view cell need to be removed This is all done within the handler closure that is passed to the action’s initializer Now that the actions have been added, the alert controller can be displayed to the user Since UIAlertController is a subclass of UIViewController, you can present it to the user modally A modal view controller takes over the entire screen until it has finished its work To present a view controller modally, you call presentViewController(_:animated:completion:) on the view controller whose view is on the screen The view controller to be presented is passed to it, and this view controller’s view takes over the screen .​.​.​ l​e​t​ ​d​e​l​e​t​e​A​c​t​i​o​n​ ​=​ ​U​I​A​l​e​r​t​A​c​t​i​o​n​(​t​i​t​l​e​:​ ​"​D​e​l​e​t​e​"​,​ ​s​t​y​l​e​:​ ​.​D​e​s​t​r​u​c​t​i​v​e​,​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​h​a​n​d​l​e​r​:​ ​{​ ​(​a​c​t​i​o​n​)​ ​-​>​ ​V​o​i​d​ ​i​n​ ​ ​ ​ ​/​/​ ​R​e​m​o​v​e​ ​t​h​e​ ​i​t​e​m​ ​f​r​o​m​ ​t​h​e​ ​s​t​o​r​e​ ​ ​ ​ ​s​e​l​f​.​i​t​e​m​S​t​o​r​e​.​r​e​m​o​v​e​I​t​e​m​(​i​t​e​m​)​ ​ ​ ​ ​/​/​ ​A​l​s​o​ ​r​e​m​o​v​e​ ​t​h​a​t​ ​r​o​w​ ​f​r​o​m​ ​t​h​e​ ​t​a​b​l​e​ ​v​i​e​w​ ​w​i​t​h​ ​a​n​ ​a​n​i​m​a​t​i​o​n​ ​ ​ ​ ​s​e​l​f​.​t​a​b​l​e​V​i​e​w​.​d​e​l​e​t​e​R​o​w​s​A​t​I​n​d​e​x​P​a​t​h​s​(​[​i​n​d​e​x​P​a​t​h​]​,​ ​w​i​t​h​R​o​w​A​n​i​m​a​t​i​o​n​:​ ​.​A​u​t​o​m​a​t​i​c​)​ }​)​ a​c​.​a​d​d​A​c​t​i​o​n​(​d​e​l​e​t​e​A​c​t​i​o​n​)​ /​/​ ​P​r​e​s​e​n​t​ ​t​h​e​ ​a​l​e​r​t​ ​c​o​n​t​r​o​l​l​e​r​ p​r​e​s​e​n​t​V​i​e​w​C​o​n​t​r​o​l​l​e​r​(​a​c​,​ ​a​n​i​m​a​t​e​d​:​ ​t​r​u​e​,​ ​c​o​m​p​l​e​t​i​o​n​:​ ​n​i​l​)​ ​.​.​ Build and run the application and delete an item An action sheet will be presented for you to confirm the deletion (Figure 10.10) Figure 10.10 Deleting an item Design Patterns A design pattern solves a common software engineering problem Design patterns are not actual snippets of code, but instead are abstract ideas or approaches that you can use in your applications Good design patterns are valuable and powerful tools for any developer The consistent use of design patterns throughout the development process reduces the mental overhead in solving a problem so you can create complex applications more easily and rapidly Here are some of the design patterns that you have already used: Delegation: One object delegates certain responsibilities to another object You used delegation with the UITextField to be informed when the contents of the text field change Data source: A data source is similar to a delegate, but instead of reacting to another object, a data source is responsible for providing data to another object when requested You used the data source pattern with table views: Each table view has a data source that is responsible for, at a minimum, telling the table view how many rows to display and which cell it should display at each index path Model-View-Controller: Each object in your applications fulfills one of three roles Model objects are the data Views display the user interface Controllers provide the glue that ties the models and views together Target-action pairs: One object calls a method on another object when a specific event occurs The target is the object that has a method called on it, and the action is the method being called For example, you used target-action pairs with buttons: when a touch event occurs, a method will be called on another object (often a view controller) Apple is very consistent in its use of these design patterns, and so it is important to understand and recognize them Keep an eye out for these patterns as you continue through this book! Recognizing them will help you learn new classes and frameworks much more easily Bronze Challenge: Renaming the Delete Button When deleting a row, a confirmation button appears labeled Delete Change the label of this button to Remove Silver Challenge: Preventing Reordering Make it so the table view always shows a final row that says No more items! (This part of the challenge is the same as a challenge from the last chapter If you have already done it, you can copy your code from before.) Now, make it so that the final row cannot be moved Gold Challenge: Really Preventing Reordering After completing the silver challenge, you may notice that even though you cannot move the No more items! row itself, you can still drag other rows underneath it Make it so that no matter what, the No more items! row can never be knocked out of the last position Finally, make it undeletable ... permissions, contact Big Nerd Ranch, LLC 200 Arizona Ave NE Atlanta, GA 30307 (770) 817 -6373 http://www.bignerdranch.com/ book-comments@bignerdranch.com The 10 -gallon hat with propeller logo is a trademark of Big Nerd Ranch, LLC... iOS Programming: The Big Nerd Ranch Guide by Christian Keur and Aaron Hillegass Copyright © 2 015 Big Nerd Ranch, LLC All rights reserved Printed in the United States of America... if you think they will), you might consider backing up and reading our Swift Programming: The Big Nerd Ranch Guide How to Use This Book This book is based on the class we teach at Big Nerd Ranch As such, it was designed to

Ngày đăng: 16/05/2017, 10:18

Từ khóa liên quan

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

Tài liệu liên quan