iOS Programming: The Big Nerd Ranch Guide, 3/e (Big Nerd Ranch Guides) (4 page)

Read iOS Programming: The Big Nerd Ranch Guide, 3/e (Big Nerd Ranch Guides) Online

Authors: Aaron Hillegass,Joe Conway

Tags: #COM051370, #Big Nerd Ranch Guides, #iPhone / iPad Programming

BOOK: iOS Programming: The Big Nerd Ranch Guide, 3/e (Big Nerd Ranch Guides)
2.66Mb size Format: txt, pdf, ePub
How This Book Is Organized

In this book, each chapter addresses one or more ideas of iOS development followed by hands-on practice. For more coding practice, we issue challenges towards the end of each chapter. We encourage you to take on at least some of these. They are excellent for firming up the concepts introduced in the chapter and making you a more confident iOS programmer. Finally, most chapters conclude with one or two

For the More Curious

sections that explain certain consequences of the concepts that were introduced earlier.

 

Chapter 1 introduces you to iOS programming as you build and deploy a tiny application. You’ll get your feet wet with
Xcode
and the iOS simulator along with all the steps for creating projects and files. The chapter includes a discussion of Model-View-Controller and how it relates to iOS development.

 

Chapters 2 and 3 provide an overview of Objective-C and memory management. Although you won’t create an iOS application in these two chapters, you will build and debug a tool called
RandomPossessions
to ground you in these concepts.

 

In Chapters 4 and 5, you will learn about the Core Location and MapKit frameworks and create a mapping application called
Whereami
. You will also get plenty of experience with the important design pattern of delegation as well as working with protocols, frameworks, object diagrams, the debugger, and the Apple documentation.

 

Chapters 6 and 7 focus on the iOS user interface with the
Hypnosister
and
HypnoTime
applications. You will get lots of practice working with views and view controllers as well as implementing panning, zooming, and navigating between screens using a tab bar.

 

In Chapter 8, you will create a smaller application named
HeavyRotation
while learning about notifications and how to implement autorotation in an application. You will also use autoresizing to make
HeavyRotation
iPad-friendly.

 

Chapter 9 introduces the largest application in the book –
Homepwner
. (By the way, “Homepwner” is not a typo; you can find the definition of “pwn” at
www.urbandictionary.com
.) This application keeps a record of your possessions in case of fire or other catastrophe.
Homepwner
will take nine chapters total to complete.

 

In Chapters 9, 10, and 15, you will build experience with tables. You will learn about table views, their view controllers, and their data sources. You will learn how to display data in a table, how to allow the user to edit the table, and how to improve the interface.

 

Chapter 11 builds on the navigation experience gained in Chapter 7. You will learn how to use
UINavigationController
, and you will give
Homepwner
a drill-down interface and a navigation bar.

 

In Chapter 12, you’ll learn how to take pictures with the camera and how to display and store images in
Homepwner
. You’ll use
NSDictionary
and
UIImagePickerController
.

 

In Chapter 13, you’ll learn about
UIPopoverController
for the iPad and modal view controllers. In addition, you will make
Homepwner
a universal application – an application that runs natively on both the iPhone and the iPad.

 

Chapter 14 delves into ways to save and load data. In particular, you will archive data in the
Homepwner
application using the
NSCoding
protocol. The chapter also explains the transitions between application states, such as active, background, and suspended.

 

Chapter 16 is an introduction to Core Data. You will change the
Homepwner
application to store and load its data using an
NSManagedObjectContext
.

 

Chapter 17 introduces the concepts and techniques of internationalization and localization. You will learn about
NSLocale
, strings tables, and
NSBundle
as you localize parts of
Homepwner
. This chapter will complete the
Homepwner
application.

 

In Chapter 18, you will use
NSUserDefaults
to save user preferences in a persistent manner.

 

In Chapters 19 and 20, you’ll create a drawing application named
TouchTracker
to learn about touch events. You’ll see how to add multi-touch capability and how to use
UIGestureRecognizer
to respond to particular gestures. You’ll also get experience with the first responder and responder chain concepts and more practice with
NSDictionary
.

 

In Chapter 21, you’ll learn how to use
Instruments
to optimize the performance of your applications. This chapter also includes explanations of
Xcode
schemes and the static analyzer.

 

Chapters 22 and 23 introduce layers and the Core Animation framework with a brief return to the
HypnoTime
application to implement animations. You will learn about implicit animations and animation objects, like
CABasicAnimation
and
CAKeyframeAnimation
.

 

Chapter 24 covers a new feature of iOS for building applications called storyboards. You’ll piece together an application using
UIStoryboard
and learn more about the pros and cons of using storyboards to construct your applications.

 

Chapter 25 ventures into the wide world of web services as you create the
Nerdfeed
application. This application fetches and parses an RSS feed from a server using
NSURLConnection
and
NSXMLParser
.
Nerdfeed
will also display a web page in a
UIWebView
.

 

In Chapter 26, you will learn about
UISplitViewController
and add a split view user interface to
Nerdfeed
to take advantage of the iPad’s larger screen size.

 

Chapter 27 will teach you about the how and why of blocks – an increasingly important feature of the iOS SDK. You’ll create a simple application to prepare for using blocks in
Nerdfeed
in the next chapter.

 

In Chapters 28 and 29, you will change the architecture of the
Nerdfeed
application so that it uses the Model-View-Controller-Store design pattern. You’ll learn about request logic and how to best design an application that communicates with external sources of data.

 

In Chapter 30, you’ll learn how to enable an application to use iCloud to synchronize and back up data across a user’s iOS devices.

 
Style Choices

This book contains a lot of code. We have attempted to make that code and the designs behind it exemplary. We have done our best to follow the idioms of the community, but at times we have wandered from what you might see in Apple’s sample code or code you might find in other books. You may not understand these points now, but it is best that we spell them out before you commit to reading this book:

 
  • There is an alternative syntax for calling accessor methods known as
    dot-notation
    . In this book, we will explain dot-notation, but we will not use it. For us and for most beginners, dot-notation tends to obfuscate what is really happening.
 
  • In our subclasses of
    UIViewController
    , we always change the designated initializer to
    init
    . It is our opinion that the creator of the instance should not need to know the name of the XIB file that the view controller uses, or even if it has a XIB file at all.
 
  • We will always create view controllers programmatically. Some programmers will instantiate view controllers inside XIB files. We’ve found this practice leads to projects that are difficult to comprehend and debug.
 
  • We will nearly always start a project with the simplest template project: the empty application. The boilerplate code in the other template projects doesn’t follow the rules that precede this one, so we think they make a poor basis upon which to build.

We believe that following these rules makes our code easier to understand and easier to maintain. After you have worked through this book (where you
will
do it our way), you should try breaking the rules to see if we’re wrong.

 
Typographical Conventions

To make this book easier to read, certain items appear in certain fonts. Class names, method names, and function names appear in a bold, fixed-width font. Class names start with capital letters, and method names start with lowercase letters. In this book, method and function names will be formatted the same for simplicity’s sake. For example,

In the
loadView
method of the
RexViewController
class, use the
NSLog
function to print the value to the console.

 

Variables, constants, types, and file names appear in a fixed-width font but are not bold. So you’ll see,

The variable
fido
will be of type
float
. Initialize it to
M_PI
.

 

Menu choices and buttons appear in the default font in a slight reduced size. For example,

Open
Xcode
and select
New Project...
from the
File
menu. Select
Window-based Application
and then click
Choose...
.

 

All code blocks will be in a fixed-width font. Code that you need to type in is always bold. For example, in the following code, you would type in everything but the first and last lines. (Those lines are already in the code and appear here to let you know where to add the new stuff.)

 
@interface QuizAppDelegate : NSObject {
    
int currentQuestionIndex;
    // The model objects
    NSMutableArray *questions;
    NSMutableArray *answers;
    // The view objects
    IBOutlet UILabel *questionField;
    IBOutlet UILabel *answerField;
    UIWindow *window;
}

Code you should delete is struck through:

 
    
int currentQuestionIndex;
 
Necessary Hardware and Software

You can only develop iOS apps on an Intel Mac. You will need to download Apple’s iOS SDK, which includes
Xcode
(Apple’s Integrated Development Environment), the iOS simulator, and other development tools.

 

You should join Apple’s iOS Developer Program, which costs $99/year, for three reasons:

 
  • Downloading the latest developer tools is free for members.
 
  • Only signed apps will run on a device, and only members can sign apps. If you want to test your app on your device, you will need to join.
 
  • You can’t put an app in the store until you are a member.

If you are going to take the time to work through this entire book, membership in the iOS Developer Program is, without question, worth the cost. Go to
http://developer.apple.com/programs/ios/
to join.

 

What about iOS devices? Most of the applications you will develop in the first half of the book are for the iPhone, but you will be able to run them on an iPad. On the iPad screen, iPhone applications appear in an iPhone-sized window. Not a compelling use of the iPad, but that’s okay when you’re starting with iOS. In these first chapters, you’ll be focused on learning the fundamentals of the iOS SDK, and these are the same across iOS devices. Later in the book, we’ll look at some iPad-only options and how to make applications run natively on both iOS device families.

 

Excited yet? Good. Let’s get started.

 
1
A Simple iOS Application

In this chapter, you are going to write your first iOS application. You probably won’t understand everything that you are doing, and you may feel stupid just going through the motions. But going through the motions is enough for now. Mimicry is a powerful form of learning; it is how you learned to speak, and it is how you’ll start iOS programming. As you become more capable, you can experiment and challenge yourself to do creative things on the platform. For now, just do what we show you. The details will be explained in later chapters.

 

When you are writing an iOS application, you must answer two basic questions:

 
  • How do I get my objects created and configured properly? (Example:

    I want a button here entitled
    Show Estimate
    .

    )
 
  • How do I deal with user interaction? (Example:

    When the user presses the button, I want this piece of code to be executed.

    )

Most of this book is dedicated to answering these questions.

 

When an iOS application starts, it puts a
view
on the screen. You can think of this view as the background on which everything else appears: buttons, labels, etc. Buttons and labels are also views. In fact, anything that can appear to the user is a view.

 

The iOS SDK is an object-oriented library, and views are represented by objects. Each view is an instance of one of several subclasses of the
UIView
class. For example, a button is an instance of
UIButton
, which is a subclass of
UIView
. (We will discuss objects, instances, and classes in detail in
Chapter 2
.)

 

For your first iOS application, you will visually create and configure your view objects. This application, called
Quiz
, will show a user a question and then reveal the answer when the user presses a button. Pressing another button will show a new question (
Figure 1.1
).

 

Figure 1.1  Your first application: Quiz

 

Other books

Paint the Town Dead by Nancy Haddock
Rebel Betty by Michaels, Carla
Dead Iron by Devon Monk
Climbing Up to Glory by Wilbert L. Jenkins
On My Knees by Stone, Ciana
Whispering Bones by Vetere, Rita
Castle Rouge by Carole Nelson Douglas