Professional Documents
Culture Documents
When you buy an ebook through oreilly.com, you get lifetime access to the book, and
whenever possible we provide it to you in four, DRM-free file formatsPDF, .epub,
Kindle-compatible .mobi, and Android .apk ebookthat you can use on the devices of
your choice. Our ebook files are fully searchable and you can cut-and-paste and print
them. We also alert you when weve updated the files with corrections and additions.
Vinny
Series Creators:
Editor:
Courtney Nash
Cover Designer:
Karen Montgomery
Production Editor:
Holly Bauer
Indexer:
Julie Hawks
Proofreader:
Nancy Reinhardt
Page Viewers:
Printing History:
Nick
The OReilly logo is a registered trademark of OReilly Media, Inc. The Head First series designations,
Head First iPhone and iPad Development, and related trade dress are trademarks of OReilly 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 OReilly Media, Inc., was aware of a trademark
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 the authors assume no
responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
No PCs were harmed in the making of this book.
ISBN: 978-1-449-38782-2
[M]
1 getting started
e...
er
Your App h
Chapter 1
Code and
test it in
Xcode.
Come up
with a
great idea!
Chapter 1
Use iTunes
Connect for
the approval
process.
This is the st
You can chooseattous bar.
but unless youre hide it,
game, you should writing a
probably
leave it.
This is a button.
and
Press the buttownill
the label textl you what
change to tel
to do.
What
should
I do?
Lets call
it iDecide.
is
The iPhone scpirexenels
320 x 480 earlier,
for 3GS and 0 x
iPhone 4 is 64
960.
each
UIs for hem. It
G
r
u
o
p
uild t
tch u
Well ske ion before we b school, but
applicatem strangely oldof the best
may se aper are some this.
pen & p ou can use for
tools y
Register as a developer at
http://developer.apple.com/ios.
Chapter 1
You will
onto yourprobably want to d
using it a Dockwere goingrag it
lot.
to be
based
Select a vnie. wapplicatio
e,
If you click on each project ptyp
fill
hel
l
wil
e
the description her
you in on some details.
As we go through the book, well use different types of projects and discuss why youd
choose one over another for each app. For iDecide, we have just one screen (or view), so
start with the View-based Application and give it the Product Name iDecide. Leave
the Device Family as iPhone and uncheck the box for Include Unit Tests. In the
final dialog box, uncheck the box for Create Local Git Repository.
Chapter 1
main
Supporting Files include yourwid
e
ntio
lica
app
function and an
and
a,
dat
es,
header file, pictur
d
other data that your app will nee
to run.
Frameworks shows a list of the libraries youre using.
10
Chapter 1
includes
The toolbar se
tting
options for building and
breakpoints, application,
running yournd showing
searching, a file listings.
warnings and
Geek Bits
Frameworks are what libraries are called in Objective-C.
UIKit, CoreGraphics, and Foundation are loaded by default,
but well show you how to add more later.
11
Xcode up close
12
Chapter 1
UI
Well be using IGnterface
Editor and mean the
Builder to
same thing.
Do this!
To edit the view files in Xcode, youll need to open up the .xib file and
change some settings in the workspace.
13
interface building
Files Owner
First
Responder
Chapter 1
Drag the
rectangular button
onto the view.
The initial size of the
button will be small,
so resize it to be a bit
bigger. Just grab the
corners of the button
and pull.
titles
iOS buttons supporexttra label,
an
without using t app, were
but for our firs the twogoing to go with ch to keep
component approa pler.
things a little sim
15
test drive
Test Drive
Now, save your work. To build the file, make
sure that the scheme selected is the iPhone 4.x
Simulator, then press the Build and Run button
here.
Welcom
e to
the iPho
ne
Simulato
r!
16
Chapter 1
There are some differences between using the Simulator and your iPhone. For
starters, shaking and rotating your Mac wont accomplish anything, so those
dont work in the Simulator. To approximate rotation and check landscape
and portrait views, there are some commands under the Hardware
menu. You also have limited gesture support, CPU and memory usage
dont represent reality, and hardware capabilities like tilt sensors (or the
accelerometer or gyroscope) dont exist at all.
Even with these issues, testing on the simulator is just so much quicker and
easier than a real device that youll find you use it for major portions of your
development. You can always start with the Simulator and then move to a
real device as your application (or hardware needs) mature.
17
understanding iDecide
OK, so the button exists, but it doesnt
do anything. It needs a little code, right?
iDecides logic
Any controls you create need a method that
Interface Builder can use to connect the control
to behaviors specified in the implementation file.
ribes the
The .xib file descnf
ured it in
button as you co . ig
Interface Builder
s a method
This line declareressed that
called buttonP er will recognize
Interface Buildllback. -(IBAction)
as a possible ca
buttonPressed:(id)
sender;
iDecideViewController.xib
Button
-(IBAction)
buttonPressed:(id)
sender
iDecideViewController.h
e files
Youll likely notice all throller
names have ViewContthat for
in them. Dont sweat in a bit.
now, well explain that
do buttony kinds of
things
}
iDecideViewController.m
18
Chapter 1
19
no dumb questions
Q:
A:
Q:
A:
Q:
A:
Q:
A:
20
Chapter 1
Q:
A:
Q:
A:
Q:
A:
Below is the code for when the button gets tapped. Add the bolded code to the
iDecideViewController.h and iDecideViewController.m files. We are creating three
things: the UILabel property, the IBAction to respond to the button press, and the
IBOutlet to change the label when the button is pressed.
#import <UIKit/UIKit.h>
@interface iDecideViewController :
UIViewController {
UILabel *decisionText_;
}
label
We need to change thane sw
er,
r
text to provide ou t to
tle
so we need an IBOu e label
be able to get to thamework
control that the fr .
will build from our nib
ll be called
Heres the action thateswised.
when the button is pr
#import iDecideViewController.h
@implementation iDecideViewController
@synthesize decisionText=decisionText_;
-(IBAction)buttonPressed:(id)sender
{
iDecideViewController.h
ion of
This is the implementtsatca
lled
the method that ge essed.
when the button is pr
[decisionText_ release];
[super dealloc];
}
iDecideViewController.m
21
sharpen solution
Heres the code from before in the context of the full files for
iDecideViewController.h and iDecideViewController.m.
#import <UIKit/UIKit.h>
@interface iDecideViewController :
UIViewController {
UILabel *decisionText_;
}
iDecideViewController.h
#import iDecideViewController.h
@implementation iDecideViewController
@synthesize
decisionText=decisionText_;
-(IBAction)buttonPressed:(id)
sender
{
decisionText_.text=@Go for it!;
of what youll
This code is typical
. Theres a
see in a header file
new IBOutlet
declaration of the
property for
and IBAction and a
our UILabel.
- (void)dealloc {
iDecideViewController.m
[decisionText_ release];
[super dealloc];
}
22
Chapter 1
mory management.
The release call is for me
nce counting for
Objective-C uses refere
l talk more about
memory management (wel
to be released to
this in a bit) and needs
free up the memory.
View
the
A component represenurts user will
GUI element that yo button.
interact with, like a sembled
Generally, it will be aseditor,
with the Xcode GUI code, too.
but it can be built in
Component
Model
The Mode
data your lapcontains the
which for iO p needs,
datasource tS apps is a
with the dat hat works
plists, images, abases,
information or general
will need. that your app
Datasource
Controller
Delegate
that
ntains the logicion
co
e
at
eg
el
d
he
T
w of informat
controls the floController. It saves and
hence its our ation and orchestrates
displays inform en when. iDecide follows
which view is seour delegate is the
this patternontroller, which explains
iDecideViewC the file names youve
quite a few of
seen so far.
23
iDecideViewController
in a nib
Our view is described ilt
with
bu
we
(.xib file) that
mplate
te
e
Th
.
er
Interface Build
this
framework we used loaspdslays it.
nib at startup and di
24
Chapter 1
Test Drive
Youve got your IBAction and IBOutlets set up, so build and run the code again. Try
clicking on the button in the simulator and see if it works.
Nothing
happens!
25
hooking up
What happened?
The Objective-C that we wrote is all set to handle things when the button is
pressed, but the view hasnt been set up to connect the button to that code.
We need to use the GUI editor to hook up our button to the buttonPressed
method we just wrote. Then when the .xib file is loaded by the framework,
it will connect the button object it creates with our code.
-(IBAction)
buttonPressed:(id)
sender;
iDecideViewController.xib
Button
-(IBAction)
buttonPressed:(id)
sender
{
do buttony things
}
iDecideViewController.m
26
Chapter 1
iDecideViewController.h
27
essages
All IBActionqmuired
take one re he sender of
- (IBAction) buttonPressed:(id)sender;
argument: t This is the
This is the name of the method
the message. triggered
IB = Interface
tha
t
wil
l
get
called. The name can be anything,
element that
Builder
but
the
me
tho
d the action.
must have one argument of type
(id).
Now we need to pick the event that should trigger this method. If you right-click
on the button in the editor, youll see a list of events it could dispatch. We want the
TouchUpInside event.
l of the
This list showsheal button
events that t get into
can send. Well events later
the different
in the book.
Most of these
sound like what events
they are.
28
Chapter 1
Test Drive
Now that everything is hooked up, its ready to run. Make sure you save everything and
then build and run.
29
test drive
Test Drive
Get a message here!
Click here!
30
Chapter 1
It works!
31
no dumb questions
Q:
A:
Q:
A:
32
Chapter 1
Q:
A:
Q:
A:
Q:
A:
Q:
A:
Q:
A:
Item
Description
IBOutlet
Functions of Xcode
MVC
IBAction
Application resources
33
SOlUTion
34
Item
Description
IBOutlet
Functions of Xcode
MVC
IBAction
Application resources
Chapter 1
Pool Puzzle
If we put t
learned earlioergether what we
app works wit about how the
about the codh the diagram
button press, e for the
of which files you get an idea
do what.
Control
Datasource
Hint: We rea
needed a datllay havent
source yet...
Delegate
iDecideViewDataSource.m
iDecideViewController.m
iDecideViewController.xib
IBAction
MainWindow.xib
iDecideViewController.h
button
35
iDecideViewController.xib
MainWindow.xib
Control
Datasource
iDecideViewController.m
Delegate
iDecideViewController.h
iDecideViewDataSource.m
IBAction
36
Chapter 1
button
iOS cross
Bend your brain around
some of
the new
Untitled
Puzzle
terminology we used in this chapter.
Header Info 1
Header Info 2
etc...
1
2
3
4
9
10
11
12
13
Across
Down
37
Untitled Puzzle
Header Info 1
Header Info 2
etc...
1
2
3
I
4
T
X
E
C
12
I
7
T
H
O
10
O
11
D
N
E
S
E
U
W O
B
R
I
L
13
38
A
6
I
O
I
U
5
R M A
A M E
Across
Down
Chapter 1
39
oreilly.com