Not come back again this is the last lesson of this group of lessons which deal with view controllers
on segues and passing data or so on and so forth.
So in this listen we're going to pass some information between one view controller to the other.
Let's see how we could do that.
I'm going to go into ex-cult and disclose this application.
I'm going to make a new application called passing data.
I never learned whether it is data or data.
It seems like a lot of people just call it data data.
So just up you can go live if you like too.
So for me it is for the most part data.
So here I have one view controller understand we're going to do things an easier way.
I'm going to add another view controller on this new view controller is going to have a slightly different
background colors such as this one.
And I have a new class for the new class for it is called Pinkard View Controller of the type you view
controller course and now most of the files are here the pink ones are now going to go into my storyboard
select this control outlet go to the identity and call it Change view control.
Perfect.
So the next thing is I'm actually going to go ahead in here and I'm going to ride from my view controller
into the pink one and say man he take a show and my segment is called the identifier for it is called
to say a id you could call it anything you would call it.
Pete I would I'd rather give it more meaningful names.
I did multiple endos and I removed that.
So here's a chance to see it once again.
So identifier to ping.
Say today Heidi.
All right.
So I've got that.
So here's what they want to happen.
Here's what they want to pass essentially.
I want to have a text field in here so I have this textfield.
So whenever I go to this very first few controller or of view controller or main or view controller
whenever I come in here and I enter something in this text Vindt a y to be passed into the into the
pink one.
So go ahead and have this textfield in here.
And then I have a button and this obviously is going to take us to that pink view controller.
So I'm going to modify it a little bit with a different background color.
It says go to Penkridge And then what's happening in the pink one.
I have one label that is going to show us what we receive from the other side.
So the label is going to show us whatever information we have received from the white one.
So here that's my label.
Let's call it the receive.
That's all.
That's just the text in that it doesn't mean anything.
So I'm going to change his color maybe like that so we can see and of course I need a back then.
So I'm going to add a button in.
Say back.
OK.
So that's going back and maybe it's by gone is a different color.
So now I've got everything that I need.
So all I have to do is I have to add the functions and also the outlets for them.
So let's go to our assistant.
I'm going to go to the View Controller up here in the view Control read that h I'm going to drag this
one I'm going to call this one data off text the.
So that's my daughter textfield and of course I have to have a function.
This one says go to pink action.
And I remember the type has to be action to go to a peeing action.
Perfect.
I've got that taking care of in here.
I need to how my pinky contorted that age.
I'm in it.
I'm going to get rid of everyone I know I don't need.
But they do need however is this one of them is this label.
I'm going to call it.
I actually received that on the label.
So it's the doctors that receive the connect.
And I need a function that is very simple.
We have done multiple times before and that one says go back home action.
And I remember it's connection is of that type action.
So that's about that.
All right.
So now I have to go on and write the code that is going to help me pass that.
So let's go to our editor.
I'm going to go to the control.
I am in unity of everyone I don't need and the only thing I need to do is here in the GO took action.
I'm going to get on this train.
This call is to send a string.
It is going to be on my textfield whatever I call it takes effort.
The ad was called takes the fill the text so I'm first going to get the data that my user has entered
and then I'm going to have to say self perform segue with identifier say with identifier and then identify
what they want to use is.
So the segue I want to call I remember it was called the two pink Sigge way.
I did.
And my sense there is going to be this information to send a stream.
Right.
And I oh I have made a terrible mistake stuff.
All right.
So now what's happening is I'm saying perform segue with this identifier and send there is this information
right now.
Let's see how we're going to use this information.
Now this information cannot be used in here simply because they have to somehow go to our next viewer
controller which in this case is the pink one.
So let's go to our Pinku controller.
I know this is going to seem a little bit confusing right now but I'm going to come back to it in a
second.
So in my pink entourage I'm going to do this I'm going to say there is a property in here property so
I'm defining a new property of the type and a string and that property in a string is called receive
data.
So there is a property called and it's a string receive the data.
And here is what happens every time our view loads.
So you might pinch your controller you did the Lord OK.
I'm going to go ahead and say every time that you loads do this say received Les dot text is going to
be received the data that they received a string was that the name I gave it received.
Dr. Yes received that that received that received that yes.
So here's what's happening.
All right.
Once again off the top.
Right.
We are saying every time the controller loads in the view the Lord make that label textile you showed
this guy received the data.
Is this guy received that is actually a property in our Pinheiro you controller.
It is not that age which means it's also in our.
And now we have to somehow connect this received data to the value that we have in a sense there from
our segue.
So let's see how are we going to do that.
I'm actually going to quickly clean everything up every everyone I don't need.
So in my dad's age I need everything in my view contorted that I don't need to view that lot in my opinion
you can tell at that age.
I need everybody who is there that's the label that's the action for going back and that's the property
we just declared and the viewpoint that I'm with I'm going to do is I'm going to change this.
Go back home to something that we know for a while and I dismiss you.
Controller are animated.
True.
Do nothing to completion.
So that's a simple as that.
So Harvey going to connect this received data outside this received data to the value that we have in
the sender.
For that we need a separate method and that method is a member of the UI kit.
It's all.
It always exists within all of view controllers and that is called that prepare for Segway.
So let's see what this one does.
It says if you are going to run a Segway let's do some preparation for it.
All right.
So what are those preparations the preparations that we need.
I'm going to write them in comments is to make sure they're received receive that off from Pyncheon
vu controller has the same information as the sender which sender it doesn't matter because the sender
I understand are going to be identical because when we call the code pink I cautioned the sender is
going to send the right information in to here.
These are just names of course.
So first thing in the prepare for C-grade which is builtin function from view controllers we are saying
when you're preparing for a sick they make sure they received data from Pinkerton is the same as the
sender.
So as this guy which in this case is called a Sender.
So how are we going to do that.
The first thing is we need to get access for pink view controller to do that.
I'm going to go ahead and say import ping view controller.
So now we have access to our Pinku controller within our view controller.
So now I'm going to say let's check the Segway.
So because remember you might have multiple segues you know your main a storyboard might have a Segway
that goes from the White One to the pink one and then from the right one you might go to a green one
and so on and so forth.
So I'm going to say if I get sick of a dot identifier is equal to a string and the string I'm looking
for is this guy.
If this is the same way that you're going through in this particular example in the app I'm making this
is completely meaningless simply because I only have one the segue in in here in here.
But in real life example on for the sake of doing things the right way you want to check for this.
You want to say if say great is this Segway then let's get access to the view controller interview controllers.
Let's call it this ping in stance.
You can call it anything is a Segway destination view controller.
So if you are saying make an make an instance of ping controller call it to this Pinkins stance and
make that to be or a sign that to be the destination of Segway What's the destination of Segway.
Obviously in this case is this guy this outlet in R R R A storyboard.
This outfit itself is connected to a class called Pink fuel controller.
So when I go back in here I'm going to say well if you did not then say this Pinkins That's dot receive
the data.
That's that is going to be send.
Now since we are not sure whether the sender is an assisting or not it's of the type I.D. It helps if
we cast it and I'm going to say and a strong scent.
Now let's run this up once and then I'm going to rewrite this bit of the code once again.
So it kind of you know recaps everything.
I feel like this might be a little bit confusing for the first time.
So let's run the app now and if it all works we should be able to get to see whatever we need on the
other side.
So in here I'm going to go ahead and say hello.
I go to pink and here it says hello pinky.
If I go back and say hello are not too blue and go to pink it says hello not blue.
Now let's quickly see what we have done in our view control of this one the one we are going from.
We are getting access to our paying View Controller.
So that's one part.
If you're getting that one and then we are saying that when you're preparing to move to the sick bay
first check if this is the right segue or not for now because I'm trying to simplify things I'm going
to say let's drop that whole thing simply because I know this is the right segue is only one of them
in here anyway.
So I'm going to say when you're preparing for the segue.
Make it that the Sigmond destination will be assigned to something of the type Ping's you controller
because that's what we know we need.
So we need to think through a controller and then I'm going to say from this stance of fate Let's get
to receive data and assign a value to it.
Forget this whole send there for a second.
Let's say we just assign a value my name to it.
It means every time that we go through the signal we call it Eimear Let's see
so whether I have something in there or not when I go it's as if you know whether I have some random
characters in there it's still going to say the same thing but what we wanted to do is you want to get
that information.
And the last example what they did is I got it in here when I was pressing the button and I was passing
it into the center for the preparation.
Let's simplify that a little bit as well.
Even though it's not the best practice and you will soon see why.
But let's simplify it a little bit.
I'm going to say when you want to go to the pink don't do any of these things and the center is now
as it has always been.
When you are preparing find your Pinkins stance and Pinkins stands received that may it what ever textfield
we have dot text.
So in the preparation set that they are all correctly and then in my pink one of said whenever this
view Laud's get the label in its text show whatever data we have to see if that's on that again.
So now I'm going to say here we see here I'm going to stay home.
Vissi see.
So that data is getting passed correctly between the two controllers.
So that's about that there isn't a lot of coding yet on most of it is kind of you know trivial.
We have a text field we have an action in our data and we have it perform say that we have seen and
then you know you are paying through a controller.
We have a property we have a label and we have a go by code.
The only thing that's really new when you're is this little part preparing for a segue.
And when you prepare for a sick day you get the destination viewpoint to read that you have your assignment
to one instance of the same type person that understands you essentially call whatever you have to call.
In this case being received data in one of the assignments that you guys have to do is to see if data
is actually not an unnecessary thing.
It's actually an array.
So you have to feed an array into it in your prepare for a break.
So let's keep it up to here.
Like I said before this for lessons this one was the fourth one of them are quite important and you
will have to deal with this on and on and on throughout whatever development you do.
So if you know if you have any questions let me know if you other doubts let me try to clear them for
you and see you in the next lesson.