Tip:
Highlight text to annotate it
X
so one of the most common questions i am passed his how do i get higher west to
pop up
one might in other languages be called an alert box for a messagebox well as
you might imagine highways does have a specific object an equivalent for doing
that
i'm going to make a new acquire wes application or just call it alert me of
that is of course a single view hap
for the iphone using automatic reference counting
and here i'm going to write a very straightforward application of just
immediately pops up on the alert message
of course you could use the techniques we've scene
to make this happen on a touch event or anywhere else in your code
but i'm going to make it happen immediately and that will give us two
things one how to make an elective and to how to execute some code as soon as
your application loads
if i go into my view controller implementation file
you've probably already seen
methods that have been created force
is replace holders that allow us to
injected our code at several different points in the life cycle of review
controller there's one down towards the bottom of that
or to rita's ing for interface korean tation
and multiple methods with the word
view in them
view did disappear view will disappear view did appear if you will appear
these methods all defined in the basic you are you you controller class the
word inheriting from here
which means they occurred
in any view controller code we have we just have to decide
whether we want to use any of these or not
was multiple places i could put micah
i'm going to choose to put this in the
view did appear part
i'll leave the call to super view it did appear because i want to make sure that
anne necessary functionality in the
superclass or based or parent class if you prefer is being executed before then
do my custom coz
and after this
i'm going to write you are a bendigo alerts view
going to create it using the standard alag
so to open in square brackets you i love you and i walk which will
allocated that object and return it to us
and then i need to initialize
now rather than do the standard unit method i do have
a couple of other more specialized initializes
the one i'm after is this one
in it with title call on message call on delegate colon cancel button title co uk
other buttons
which doesn't exactly rolled off the tongue but it's useful redhat return and
get that there's a little bit confusing so let me break this apart onto multiple
lines
just so we can take this
one argument at a time
it's a way that we can create the subject
passing in everything that needs to know in order to stitch showing
so rather than creating and then sent lots of properties
we get it started off correctly
so the first remedy in it with title takes an entire strength i can just
use the at sign in the double quotes to pass and the title
we have alert title of course these on separated by comma so don't accidentally
put one in then the next one is another and i string for the message what
actually does the solar box a
to something simple
their laws a problem
next parameter
delegate
now what do we do here
delegation of course is always about object to being able to
pass responsibilities to other objects
and this would allow us
to nominate
another object to take any delegate methods that the you are alert view mike
to decide to take issue
i'm not really interested in that that's about
more advanced uses of this you i alert to you
so what i'm going to write here is just
mill no all lower case
this is the equivalent of
now in some other languages
it's basically saying
this is and empty
object
and what can happen here is that if this is only a few
trying to find a delegate it's just going to send messages to mail
and those old disappear off the face of the planet actually allowed
to send messages to and they'll object
an objective c
i'm going to move on to the next one which is the cancel button title
this really is
text appears on the button that is going to make this you i love you go away
to be something super simple like
okay
and then we have the last argument last parameter which is kind of a little
unusual
we got them strain
dot dot dot com a nil
now this is where we can pass n
what's called other button titles now how many other button swell and could be
one could be to should be noted could be three oh four
what we do here as we just passed
multiple strings
separate them by commerce
but we need to make sure
that we want to be end of it
this is essentially how r
kind of passing an almost an array of strings if we wanted
we mark the end of the array with the word nail
so what if i didn't want any buttons other than the council but never came
runs fine i can do that
i'd just remove it and we end with
if you wanted to do either bonds or you want to work with her
take a look at the
documentation for you i alert few that's gonna set this up
just for a basic color view
however it will not show it
we've created this alert view
the nothing happened chat
we need to explicitly talent
to show
and that's it going to say this and rough
and this code
automatically appear there we go as soon as the view loads
alert title there was a problem
and we make it go way back to the regular part of the application however
there is something that you might notice if i press the home button
an exit out of the application and then go back and say learning
we're not seeing
that'll it few appear again
and that's because
if we believe the ad
using the homebound
it's not actually fully exiting it's not terminating
is going into a background state
and when we reopen it it's not calling this view did appear code again
now if for example what i did with the simulator is i had command queue and
out of this and then granite again
we would be starting from scratch
but we need to be aware of this behavior because this isn't the simulator
behavior this is also what happens on the real device
applications by default don't completely terminate
this is the default
multitasking situation in iowa s
and what it means for us
is that we need to start becoming a bit more aware of the life-cycle have an
application
so that we can understand
all these methods like view did load of you did unload view will appear view did
appear
wherever they happen i'm under what circumstances
so that we can introduce our code in the right place is along the way