It hey what's going on my code brothers and code sisters we are going to set up detailed Visi so that
when we tap on a cell in storefront B.C. It will pop up detail AVC show all the gorgeous data and put
us one step closer to actually getting these in-app purchases in our apps so let's pull open our project.
Here we go.
And the inside storefront B.S. We need to add one more delegate method from you of you you a collection
view delegate called did select a item at index path and this is called as you could see.
Well you know what I'm going to actually retype that so you can see exactly what the description says
it says tells the delegate that the item at the specific index path was selected.
So in our simulator here let's pull open our project.
This here is index 1 index to index 3 index.
Well I guess 0 1 2 3 4 5.
And so when I select one or select the index one it's going to say hey index one was selected and it
calls that function and when it calls that we can basically set it up to present a certain type of information.
So let's go ahead and call did select item and index path and what we need to do is we need to find
a way to get the information from the cell and handed over to detail AVC.
Now we're going to be using the storyboard in order to present this view controller.
So we need to set up something first and it's easy to overlook so let's do it now.
In Maine that story board we're going to go ahead and select detail visi.
Now we're going to present this using a function that the storyboard has called instantiate View Controller
with identifier and we need to set that identifier.
So of course the class is detail visi.
But we need an identifier for the storyboard.
So we're going to create a storyboard ID of detail Visi just like that and we'll use that when instantiating
our view controller.
So go ahead and head back to storefront Visi and we need to create a cell here.
OK.
Now what we're going to do is we're going to actually use the item the very same way that we did with
our cell we configured ourselves with our items and pulled out the right ones with index path that row.
So when we select an item we're going to use that same selected index path to tell what data to present
on the next screen.
It's pretty handy.
So first we're going to go ahead and create an instance of detail Visi and then we're going to present
that case or whenever we tap on the cell it's going to create an instance of detail OVC and presented.
So I'm also going to use guard lead just so we can be safe just in case we mistype something.
So go ahead and type guard let detail Visi equals storyboard instantiate view controller with identifier.
OK.
And that's where the identifier we just set will go detail B.c.
Make sure you type it correctly.
If that doesn't work we're just gonna go ahead and call.
Else what else.
And we're just going to return meaning if it doesn't work we're going to break out of the function and
not do anything at all.
So detail B C is now instantiated and we have an instance of it which is great but just like the cell
it's not actually of type detail Visi until we optionally cast it.
So afterwards go ahead and type as detailed Visi like so now it's of type detail AVC.
Very very cool.
The only thing we need to do to present it is call present.
Select this function pass in the view controller to present.
Which is detail Visi What's with the lowercase D.
Animated is true and completion is nil we don't care when it completes.
So there that.
If I build and run you'll see that warning goes away.
But we are not giving detailed VC any information.
It doesn't know up from down left from right anything.
So we need to write we need to basically create a way for detail Visi to get information.
So what we're going to do is we're going to go ahead and go into the detail Visi subclass.
You can click on it here and we're going to create a function that will allow us to initialize all the
data before this view controller is even presented.
So above you did load go ahead and call func init data for item with an internal parameter of item of
type item.
Kind of redundant but pretty cool.
Now what we're going to do is we're going to use this item to set all the information that we need and
our view controller.
So go ahead and let's call item image view image and we're going to set that to item.
Hello.
I don't image.
Go ahead an call item name label text and set it to be equal to item name.
Then go ahead and type item price label text and set it to be equal to item price.
OK.
Let's think.
Is there anything else we need to add.
Oh of course we're getting that arrogance so we're going to use string describing describing and we
can pass in our function here or not our function but our price double value.
All right so that's a string now that's wonderful let's think ugly ad views fine buy items.
Button is is fine.
Oh you know what it's not fine because the buy items button currently has a placeholder says buy item
for dollar amount.
So I'm going to actually copy this and I'm going to go ahead and set the title of the buy items button
when we when we load this.
So go ahead and call whip's by item by item button set title and the string is by this item for dollar
amount.
Now where are we going to get that dollar amount.
We can use our item price so go ahead and call item price and the price will go right and just like
that.
Now the control state is not normal.
This is just a regular button.
We're setting it for its normal use.
And guys this is pretty awesome.
We have now set up all of our image views our labels our button is now set up with the price and we
should be good to present.
I don't know about you but I really want to try this.
So let's go ahead and let's head back to a storefront VC and when we selected we need to be able to
pass in an item and we use this property here item to pass in the index path for the specific items
so copy that paste it below.
And now whenever we select an item we're going to get the exact item we select what are we going to
do with it.
So we're going to call detail we see in that data for item pass in the item.
And when we presented all that data will be initialized.
Let's go try this build and run.
Let's go see if this works.
Presenting the data on the View Controller we want this is super cool guys I'm really excited about
this to make this a bit smaller.
There we go.
OK here we go building and running when I select a View Controller it should instantiate the data initialized
it on the other view controller and then present it we get an error or look at that it's crashing and
you know what the reason is we are trying to set up these UI kit elements before they're even available
to us.
So you know we have to do we have to set this up in view to load when the view loads.
So go ahead and actually we're going to cut this and paste it into view load and an initialiser function
gets called before all of that happens.
So what we're going to actually do is we're going to create a variable inside this class called item
and we're going to instantiate that variable with item that we pass in and then we'll use that item
instead to set up the values for these because we are basically asking it to fill this with data when
this doesn't even exist yet.
So I'm going to go ahead and follow the same pattern by making this a public private set variable.
And it's going to be var item type item and we're definitely going to need it.
So I'm going to go ahead and make it non-optional.
Now when we pass this in we're going to call self item whip's self-taught item is going to be equal
to item.
All right so now when we instantiate this view controller it's going to initialize this item in this
variable here.
And you know what I can make this a constant because it's never really mutated Oh no.
It is definitely mutated sorry.
So we're going to go ahead and set the value of this item then we're going to go ahead and use that
to set the values of the UI kit elements as soon as they're created.
So go ahead and build and run.
Let's try this one more time.
That should solve our problem properly initializing the data.
That's kind of a big one.
So let's see how we did select an item and boom there is our our project that's really really cool.
I'm noticing some issues here.
Everything kind of got squished down and you know what I'm going to say that that is due to the fact
that we remove that bottom constraint.
Let's add that back by selecting our stack view.
And let's pin this 8 from the bottom.
No that's good.
All right.
Yep that's good.
Let's build and run.
And let's see if that fixed our issue.
But guys we're passing the right data.
You saw it all there the picture the name the price the View Controller is good to go it looks awesome.
This is great guy so in the next video we're going to actually start setting up some of our in-app purchase
fun.
So Selecta maybe pizza there's pizza 999.
There's our ad and buy this item for 999 hide ads for 99 cents.
So cool.
Oh my goodness I can't even handle this but I'm a little perplexed as to why this view is not properly
showing up like it should be.
Yeah I mean I don't even see it at all unless this is a simulator bug which I don't believe it is
very weird.
I changed the color.
I mean that probably will not have an effect that's even uglier.
Maybe it just needs to be extra ugly.
Ha now it's still going OK.
So what I'm going to do is I'm going to look at my stack here and it is being aligned centered.
So let's go ahead and set it to fill and then I need to change my labels here to be center aligned and
let's build and run it again maybe since I don't have an explicit with it was getting squished in with
the constraints there.
Could be part of the problem.
Let's see if that fixed it.
And there it is.
There's our ad super super cool.
That was the problem.
It was center aligned and it was getting squished into an infinitely small cell going to change it back
to yellow because that's that's even too ugly for what I was intending.
GROSS But anyways guys amazing amazing work.
We are now properly passing values to detail Visi in the next video.
We're going to start by creating our in-app purchase service.
It's a singleton class that's going to handle all of our handy dandy in app purchased stuff.
Let's head over there now.