Professional Documents
Culture Documents
Now
lets
connect
the
label
to
code.
First,
open
ViewController.h
and
type:
#import <UIKit/UIKit.h>
-(IBAction) sayHello:(id)sender;
@end
This
@property
statement
is
kind
of
technical,
but
to
make
iPhone
apps
you
dont
have
to
know
that
much
to
get
started,
just
put
your
statements
like
above
when
you
want
to
access
a
user
interface
object
like
a
label
in
code.
We
can
briefly
describe
it
here
but
dont
get
hung
up
on
it
for
now.
Read
these
articles
for
some
discussion:
http://stackoverflow.com/questions/9859719/xcode-property-attributes-
nonatomic-copy-strong-weak
http://www.cocoawithlove.com/2008/08/in-defense-of-objective-c-20-
properties.html
Now
open
the
ViewController.m
file.
When
you
create
a
property,
you
will
need
to
add
a
corresponding
synthesize
statement
so
that
getter
and
setter
methods
are
automatically
created
to
read
and
write
to
the
objects
properties:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize mylabel;
-(IBAction) sayHello:(id)sender{
NSLog(@"Hello World");
}
Now
we
can
change
the
code
in
the
sayHello
Action
to
change
the
label
on
the
screen.
This
is
done
by
writing:
-(IBAction) sayHello:(id)sender{
NSString *message = [[NSString alloc] initWithString:@"Hello
World!"];
mylabel.text = message;
}
Next,
save
your
files
then
return
to
the
XIB
file.
Click
on
Files
owner,
then
look
at
the
Connections
Inspector
for
Outlets:
Notice
the
mylabel
object
we
created
in
our
code
is
displayed
here.
Click
on
the
circle
on
the
far
right
of
mylable
and
hold
the
mouse
button
down,
then
drag
it
to
the
label
on
your
View.
Now
when
we
run
the
code
we
see
upon
a
button
click:
mylabel.text = message;
[message release];
}
Were
not
going
to
use
the
label
to
display
the
message,
so
we
delete
the
line
referencing
the
label
(in
fact,
if
you
want
you
can
delete
the
synthesize
directive,
along
with
the
variable
declaration
and
property
declaration,
although
its
not
strictly
necessary
to
get
rid
of
all
of
that).
Now
our
code
looks
like
this:
-(IBAction)sayHello:(id)sender{
[message release];
We
can
allocate
the
memory
for
the
variable
alert
in
basically
the
same
way
we
did
for
the
string
variable.
But
this
time
of
course,
we
use
the
class
UIAlertView
to
allocate
the
memory:
UIAlertView *alert = [[UIAlertView alloc]
Next,
we
can
initialize
it
by
using
initWithTitle.
Following
this
keyword
we
specify
the
title,
message,
and
buttons.
Titles
and
messages
can
be
input
as
string
literals
or
using
variables.
Since
we
already
have
a
string
called
message
in
our
method
we
can
go
ahead
and
use
that.
We
will
use
a
string
literal
for
the
title,
that
way
you
can
see
how
to
do
it
both
ways
in
one
step.
OK
that
might
be
a
little
confusing,
so
here
is
how
we
initialize
it:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"My
Program Message!" message:message
delegate:self cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
Now,
we
need
to
call
the
show
method.
Whenever
you
need
to
call
a
method
for
some
object,
enclose
it
in
square
brackets.
So
our
instance
variable
is
called
alert
and
we
call
its
show
method
by
writing:
[alert show];
At
that
point,
were
all
done
with
the
UIAlertView.
If
we
did
not
select
Automatic
Reference
Counting
when
creating
the
app,
we
can
release
the
memory:
[alert release];
However,
if
you
created
your
app
using
Automatic
Reference
counting
or
ARC,
this
line
of
code
is
not
necessary.
In
fact
including
it
will
generate
an
error.
Lets
go
ahead
and
run
in
the
simulator,
and
tap
the
button.
This
is
what
we
get,
youre
standard
Hello
World
message
box:
Now
suppose
that
you
wanted
to
display
two
buttons
called
Yes
and
No.
Well
change
the
message
and
display
the
message
box
with
two
buttons
by
changing
the
code
like
so:
-(IBAction)sayHello:(id)sender{
[message release];
}
We
added
a
second
button
by
setting
otherButtonTitles
to
something
other
than
nil.
Now
when
we
run,
this
is
what
we
get:
To
add
a
third
button,
we
can
simply
add
to
our
list
of
other
button
Titles.
However,
be
aware
that
the
cancel
button
appears
at
the
bottom
of
a
stacked
list.
In
other
words
if
we
type:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"My
Program Message!" message:message
delegate:self cancelButtonTitle:@"Yes"
otherButtonTitles:@"No",@"Maybe So", nil];
[alert show];
[alert release];
This
is
what
we
see:
It
might
make
more
sense
to
arrange
things
as
Yes-No-Maybe
So.
So
we
will
set
the
cancel
button
to
Maybe
So.
Then
we
will
put
Yes
and
No
for
the
other
button
titles:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"My
Program Message!" message:message
Lesson
Summary
In
this
lesson
we
learned
how
to
create
a
UIAlertView
and
display
it
to
the
user,
with
one
or
more
buttons.
In
the
next
lesson,
we
will
learn
how
to
determine
which
button
the
user
tapped,
and
how
to
record
events
and
data
for
debugging
purposes
in
xcode.