Tip:
Highlight text to annotate it
X
we've seen how to read and sent simple single values and text fields and labels
we're not going to see you have a little bit more complex data sources
we're going to use a control called the pay-per-view which is a great example
of how more complicated user interface view objects
can interact with are covered
now we've seen
one-up reconfigured version of this being used with the date picker but this
time we're going to configured ourselves
so my object library and just going to filter down on the word picker
and instead of the date picker will take the picker view
and i'll position and the bottom of my main view here
and it has some dummy data
what i'm looking at is this picture of you
with one
component
the component in a pic of you is the official name
movable peace
you can think of the component as a column
they can have multiple rows
so if we were looking for example of that day taker this one in this view of
it
has a four components
if i switch that they take it to the countdown timer
two components
with just a date it has three components
each component can hold a vestry different amount of data and it
some going to delete the day picker 'cause we're not using that
we're gonna make a own
and if we're going to define on paper we need to say
how many components exist
and how many roseanne each component what's the content
of each wrote in each component
and we can also say what happens when somebody
chooses between them and selects a row
now you might think
but if you drank one of these pic reviews onto user interface
you could then go to your inspectors say the attributes inspector and start
loading it with data somehow
but you're not going to be able to do that
we're going to have to provide the data for marco
unless all we want is the apple provided a list of cities in california it we
have to do it ourselves
so let's talk about the simple situation in configuring type interview
i want to have a picture that has a straightforward single list of items for
this aft
it will be
umu checker
we can spin it and say whether were ecstatic are happier cheer form this
data
has to come from somewhere
and the way we do it
is by connecting the picker to an object that can be its formal
datasource
and the datasource
has to answer two questions
how many components does this pic i have
and how many rose
does each component half
in this case we'll have one component
and will have
maybe a dozen rose
but we also need to connect the speaker
to a delegate object
and the delegate answers two more questions
once in each role
what happens when any row is actually selected
may be forgiven for thinking that the data source in the delegate could really
be combined into one thing it does feel like that but we do formally described
them as two different pieces
however one of the easiest ways to deal with this
as we will nominate
our current view controller
as both
the datasource
and the delegate
and will put
all the necessary code and that one view controller
and we support the situation just by writing methods with particular names
inside that you control
so back in tax cuts
how do i put this picker control up
to my view controller code bump the same way we hurt anything out there
jump across to the connections inspector we could also do this by right-clicking
and i'll see that the pick your control
has to outlooks listed here the too we just mentioned datasource and delegate
technically
they could actually be connected to
different objects
but i'm going to grab the plus button beside datasource and drag it over to
files owner which represents the link to the view controller dot h dot m files
and i'll do exactly the same with delegate
and i can see in the connections inspector they're both registered as
being hooked up two files owner
now i'm going to jump over
into my header file for that
because if i want to do this properly what i should also do is make that
promise
in my class that we're going to be the delegate and we're going to be the
datasource
we'll talk about using the little angle brackets before
so after you i've you controller
i'm going to put the opening angle bracket
and type in you life
subtyping p_i_ picker view delegate we see that we've got two options here
you i think if you datasource
you i think of you delegate
we actually need both of these it doesn't matter which or dinner in so you
are picketers datasource
comment
you are right
picker view delegate
and be careful of the delegates 'cause they're aren't many of them so you don't
want to select the wrong one
i'm now marking my class i'm saying were promising to support of being both the
data source and the delegate
one of the benefits of mocking it displays if i say this and then had come
and be to build
we're going to get a compiler
over here you might be issues navigator it's telling me there's a some magic
issue
and incomplete implementation
like initially expand these ones here
that said as a method in the protocol were promising to support a protocol but
were not actually supporting it
now i could actually clicked further into this to find a few more details but
we actually know what's going on
cousin my implementation file i said were you i pick your view data source
and where you live picture of your delegate but what is happening
multiple ways to find out
one would be
holding down the option key i'm going to
hover over you i take your view data source and click it we'll get the pop
over when done
and i have two ways of getting to the meaning behind this
i could go to the reference in cv actual
documentation on it
and he would give me the protocol reference for being a datasource what
must i do
i can see there's only actually two tasks
you're both required
this is what i say
how many components do i have number of components in pay-per-view
how many roasters each one half
number of roseanne component
copy these
and paste them into my coat
alternatively another way i could have looked at this
as again by option clicking it and instead of going to the documentation
i could go to the header file
for you i pick your view
act like that
it takes me right here too
the protocol for being there you are a picture of your source
i've actually got coach here
we must provide methods with his signature
i'm going to copy both of these
the number of components in pic of you
and the picker view colon number of roseanne component
copy that
this is an are filed with deep inside the iphone a west framework
i need to go back over
and to my view comptroller dawn m file
and i'm going to paste them inside the implementation
you can put them anywhere
a right now these on isopropyl signature so i need to replace that closing semi
colon with an opening brace
for both of them
and now we have that skeleton functionality
so first return the number of components in this picture of you
return
that's it for step is done
return the number of roseanne component
mom's different i don't actually know quite now
what i'm going to do is something fairly simple i'm going to just create an array
with a certain amount of values in
i want to access that array from a few different places in this class so i'm
going to go ahead and is created as a property
jumping into my header file
of the state property
it'll be non comic and strong
and as a racist
and i'll call it mood list
jump over into the implementation of what i want to make sure that i
synthesize them
and
i want to loaded up with a few values when this application loads
i could do that from a few places but one of the places that will work
is for me to write it here interview did load
mood list which is already defined is in an asteroid
he calls
this is where we created
anna sorry alloc
and i'll use the method called emit with objects
where we can just give it a series of strings for example separated by comma
sd
and just finishing off with a male
here of course they could have unloading some information from aware of service
or from database likes equalizer using core data that's a whole nother
discussion
so i just want something simple
was seen as this array is going to be instantiated as soon as the view has
loaded
that's going to be before
this number of roseanne component is called
and i need to return the number of roses should show
i want to make a manual count but i don't have to you
all i'm going to say is
return
mood list space
however many options are unit will return that number
but were missing something
we had said in our view controller
that we were going to have the data source came to the files on it and we've
done that we've provided the two necessary methods
borough also
the delegate
but what does that mean
once again we can hop into the header
we've seen what it means to be a datasource
but i could also click here and see what it means to be a delegate
and if i have that pic of u delegates elected new reading in quick help
the delegate of the u_n_ pick your view
mustard of this protocol and implement at least some of the methods to provide
the pic of you with the data and needs to construct itself
simon to come down here and click on the picture of your delegate protocol
reference
again the list of tasks that we have we done actually need all of these
the device can we can see things like
setting the dye mentions of the picture of you
the row height or the weight for a particular component
because when you have multiple components they're not all the same
where to take a look at the date picker
months in days in years all have different weights
what we're interested is the setting the content of component rose
i noticed the textarea the methods in this group are marked
we would be able to compile application without providing them but as it says
to use a pic of you you must implement either at the picture of you come on
title furrow colon for component
or the pay-per-view
view furrow
for component reusing view
neither of you have a wonderful sounding method name but the one we want
is this
pay-per-view cologne
title for ocho alone
for component
i'm going to copy the signature of that
including the minus sign
jump back over into my coat
i want to be a might implementation
doesn't matter where i put it but i'm gonna put it after
mike to datasource methods
and pasted and they are
annie my opening and closing braces
this is a delegate method meaning we don't care about
how this is called we just know it will be called
what's going to happen
automatically
is that when that picker component loads
it will ask the datasource how many components supposed to happen it will be
told
it will then past the datasource how many rosary in that particular component
and be given the answer or how many entries are in the rain
and then this delegate method is going to be called
for every single one of those who are we have to provide
the actual correct entry for each correct wrote
this is simpler than it sounds
we're being given several parameters here
a picture of you
a row number
and a component number
the only one i am interested in is the row number because i know we only have
one pick your view
and i know we only have one component
so this method is going to be called passing in zero
three four
all i need to do mistake that parameter of roe and use it
to access the correct piece of the array
some good to have a return statement
the just says return
and then access is my mood list array which is already loaded up with values
whatever object comes from the particular index of rome
save it
build them
build succeeded no complaints
and we have our picker being loaded up
with the information from our any
of course we can take this little further to see how to respond
to these different items actually being selected
was he had to do that in a moment