Hey everybody what's going on this is Caleb with Deb's Lopes dot com.
And in this video I'm going to show you how we can use our ax swift to transmit and send values back
and forth between View controllers just like you might with a Segway or even with protocols.
It's really really cool and actually super easy in our X-Wing.
So let's go ahead and let's pull open our project and let's get started.
So we're first going to create a new view controller and a button on our current view controller right
here.
So let's begin actually by creating that view controller.
Drag it on just like that looks good.
And let's set a background color for this visi.
I like this blue color which if you want to use it you can.
It's 4 7 8 C6.
Feel free to use that.
And what we're going to do next is we're going to go ahead and copy and paste these into this view controller
like so.
And you know while I'm at it I'm going to put him in a stacked view.
I'm going to go ahead and pin it one hundred twenty from the top 20 from the left 20 from the right.
And let's give it a height of 120 then let's give it some spacing of 20.
And that looks good.
So you know what that's green.
Let's change this to be blue.
You don't have to.
Just bothers me that it's not the same.
So anyway that's great.
Now let's go ahead and let's add a button to this view controller let's put it down here below the stack
view.
And let's just go ahead and type a plus sign and add name that looks good.
Let's stretch it out so it fits.
Looks awesome.
Let's change the font and color to custom Avenir next.
You guys know that's my favorite.
And how about Demy bold.
That looks good.
And let's make it a size 20.
Let's change the font color to white and let's give it some constraints to stretch it out.
So we're going to set it to be eight from the top and let's do I don't know how about 30 from the left
30 from the right.
There we go.
Got a nice big button.
Hard to miss.
And now all we need to do is basically link up these to their appropriate controllers.
But this blue Visi doesn't have a view controller yet so let's right click on our X namer click new
file Cocco touch class and let's name this add name Visi K. press next.
Add it to your project.
And now we're going to go ahead and enter the storyboard again and set this identity or set the identity
of this view controller.
So select the VC select the identity inspector and change the class to add name visi.
And while we're at it let's go ahead and set up the storyboard ideas well because that's what we're
going to use to instantiate the view controller later.
So copy this paste it into storyboard ID and press enter to lock it in place.
Now let's go ahead and let's set up the appropriate Iby actions that we need.
So click on the green view controller.
Open up the assistant editor and make sure that you're on automatic and view controller.
Now we probably could change this name here but I'm not going to.
Just because.
So let's go ahead and let's all right click and drag and create an IP outlet.
Let's call it add name button.
All right.
And next let's go ahead and click on Add name visi.
And it should switch over there we go.
While we're here let's get rid of did receive memory warning and the commented code there we don't need
this included get rid of that.
Ok cool.
And now let's go ahead and let's.
Right click and drag from this text field and call this new name textfield and connect it then right
click and drag from the button and call this submit button.
Like so go ahead and get rid of that space.
And now we're going to go ahead and set up our other view controller this green one here to present
this blue view controller.
So let's go ahead and let's head into view controller and we're going to create a new function called
phunk bind.
Add name button OK and this is what we're going to do to bind our ADD name button to our view controller.
So go ahead and call add name button.
OK that's the name of our X and since it's a button we can access dot tap k.
So whenever tap is called that's what's going to happen.
Now I want to make sure that I don't accidentally instantiate multiple copies of view of a controller.
Let's say that I tap the button super fast and was somehow able to present four or five instances of
a view controller that would not be good with Eric Swift we can call a function called Dot throttle.
And what that's going.
Oops only Tootsie's throttle and what throttle is going to let us do is it's basically going to prevent
us from putting more input in than we want.
So to call throttle we're going to go ahead and put some parentheses here and we can give it a due time
and a schedule or not.
I've already shown you this.
It's similar to the bounce.
But what it's going to do is it's basically going to ignore input that is duplicate within a certain
time frame.
So 0.5 seconds is plenty of time.
That means if I push the button once it's going to lock it out and not pay attention to any other input
until after point five seconds.
Now the scheduler like I always like I showed you is main schedular instance.
OK it's a singleton class so we need to call the instance at the end and main scheduler is basically
just the main dispatch queue.
It's just a handy way of using it.
So now we are throttling our input so we don't accidentally instantiate too many of you controllers.
And now what we can do is call subscribe and that's where we're going to basically call what would happen
in an IB action.
K whatever happens when the button is pressed so called don't subscribe subscribe.
And on next is what we're going to call and inside of the next closure we're going to go ahead and create
an instance of ADD baby seat.
So go ahead and type guard let add name Visi equals storyboard instantiate controller with identifier.
This is how we're going to use the View Controller to create an instance of a particular view controller.
So go ahead and give some quotes there just single quotes and we're going to call it ad name the C because
that's the idea that we just set.
So very cool.
It's yelling at us of course because we're a closure so we're going to go ahead and call self-taught
storyboard because we're in a closure we need to keep a reference to self and the inside of a guard.
We need to create an additional condition in case we can't instantiate this properly.
So instead of crashing we're gonna go ahead and just call.
Else.
And if we can't properly instantiate that we're just going to go ahead and print could not create and
name visi.
All right.
So that's what will happen if we can't instantiate it but we will be able to.
So don't worry now that we have that initialization.
Here's what we can do.
We can create a publish subject do you member publish subjects.
They basically allow us to subscribe and then put in elements and the subscription is going to pay attention
to anything that is added after the subscription.
So in order to actually do this we need to go into ad Daim Visi and create an instance of publish subject.
So I'm going to go ahead and do that beneath these Iby outlets.
I'm going to go ahead and call Let name subject and that's going to be equal to published subject.
Although we have not yet imported the proper frameworks so go ahead and import our acts swift and import.
Whoa.
Hello.
Import our cocoa.
So now we should have access to publish subject.
There it is now of course we're dealing with strings because we are dealing with you know text fields
and labels so this is going to be a published subject of type string.
And we're going to instantiate it from the beginning.
So we're also going to need a Dispose bag because later we're going to subscribe to our submit button
and we need to be able to deallocate the memory.
So go ahead and call Let dispose bag equals dispose bag and instantiate it.
Now we don't need to do anything just yet but we do need to be able to access this name subject because
when we instantiate the view controller we're basically going to link this publish subject to our array
and our other view controller.
Whenever we push in a name from the other view controller it's going to go ahead and push in the new
name with an on next call.
So here's what we're going to do.
We've created an instance of ADD named visi.
Now it's yelling at us and let's figure out why it says guard body may not fall through.
Consider using a return or throw.
Oh that's right I sorry.
So we need to return or break.
And so what we can actually do is we can call fatal error and fatal error has a string property and
basically fatal error is going to return and print an error message at the same time.
OK that's a good way for error handling.
So now what we're going to do in our view controller is we're going to go ahead and access our names
subject.
So now we need to do is access add named Visi like so and try to access our names subject.
But we can't and you know why.
Because we have not actually set up our instance of the controller here to be of type add named visi.
Check it out.
This just returns a view controller so we need to cast this optionally as ADD named visi.
So now if I go in to add named Visi I can push Dot and I can get access to our name subject or I should
be able to maybe I need to build.
Let's try that again.
There we go.
OK.
Now name subject is showing up so if you don't see it just build and that should solve that problem.
So add named Visi that name subject.
And here's what we're going to do.
We're going to go ahead and subscribe to it.
And then whenever a value is added into it we can append that to our array in this view controller.
So go ahead and call subscribe and we're going to call on next.
As you may have expected and whenever a value is passed into this name subject we're going to call self
names array.
OK remember that's our ray that is presenting all the names at the bottom of the view controller self
name xray dot value k we need to access the array and we need to append something from this subject
OK from our published subject.
But we currently don't have a way to access whatever is returned in this closure.
So to do that after the bracket go ahead and call name in OK we've just set up a property.
So whatever value gets passed in at Next we can access it using this variable name.
So we're going to pass in name.
Okay that makes sense.
Our array just got a new name.
Now if this is confusing to you it's OK.
We're going to see how it works and then you'll understand it'll click I promise.
So it's giving me an error.
That's because we have built it will go away.
There we go.
Very cool.
So what we are doing here is we are setting up this view controller to be subscribing to the names subject
in AD named visi.
Then whenever we push something in this value is going to be added to our names array on the previous
controller so we can subscribe on different view controllers it works that way it's very very cool.
Then next after we push add we want to go ahead and call add name Visi dismiss.
OK we want it to go away to show our view controller beneath it.
So go ahead and call.
True and completion will be nil.
All right.
Now it's literally that easy to pass in the values.
But of course we need to call add to dispose bag.
And so we'll do that like so.
And look at that we are one step closer to actually passing values between the view controllers here.
Now I'm just sort of doing some cleanup here to make it look nice.
And now now that we have subscribed.
Oh sorry we're inclosures we need to call self-taught dispose back.
Now that we have successfully subscribed so that when we pass in values we're going to go ahead and
present this view controller because we have our subscription all set up so go ahead and just call self
that present.
And for the view controller to present it's going to be at named Visi animated of course is true and
the completion is nil.
Basically what we're going to do is when we tap the button we're going to throttle the input by point
five seconds.
We create an instance of ADD named Visi and we set up the name subject to have a subscription on this
green view controller so that whenever a value is passed in we append it to the array in the green view
controller then we dismiss the controller.
OK.
But since that won't happen from the get go we added to the Dispose bag at the very end of course and
then we actually present this instance.
So basically this is like a function that is set up whenever we pass in a value to the name subject.
But when the controller first presents there's no values we haven't tapped the button to push in a value
yet.
So we have now set it up to present the proper view controller and dismiss it when we're ready.
Very very cool.
So of course we need to call bind add name function to view did load.
So go ahead and call that.
There we go.
Bind add named function and now the last thing we need to do is to set up our submit button on the blue
view controller which if you remember looks like this.
We're going to set up this button here so that when we push it we push in a value to our name subject
and therefore add a value to the array and dismiss the blue view controller.
So go ahead an inside add named visi.
We're going to create a function down here called phunk bind submit button and here we go.
Let's go ahead and set this up.
So go ahead and call submit button.
Our X-Com tap and we're going to subscribe to it meaning whenever we tap it we're going to do something.
So go ahead and call subscribe on next.
And in the brackets of the closure we're going to go ahead and call self name subject on next and we
can pass in a value.
Now where are we going to get that value.
The textfield.
So let's go ahead and let's do that self.
Got any new name textfield text.
And of course for some Rapide because it's from an optional string of textfield.
And that's it.
We push the button.
The subscription happens.
We pass in the value from the textfield the value gets added to the array.
The view controller gets dismissed and it will show up on the previous view controller we're talking
between New controllers thinks to subjects.
Very very cool.
Now I just realized there could be an issue.
We will basically be able to pass in blank names.
We don't want that so we can say if self dot new name textfield text is not equal to an empty string
whoops we can go ahead and call on next.
So that'll prevent us from actually passing in a blank value.
But you know what guys we are really really close.
Oh sorry I forgot we need to go ahead and call add disposable to and add that to our dispose bag and
told you we'd use it.
Anyway let's go ahead and clean this up.
We're going to go ahead and move this in a little ways like so and we're going to call it from view
to load.
Bind submit button.
Let's go see if it worked.
So build and run.
Open up your simulator and I'm really excited to see if this works.
Guys if this is working the way that we think it is this is very similar to passing values using a protocol
or passing values using a Segway.
So let's go ahead let's pasand some names the way that we have been Kalib submit.
Nice.
How about Bob.
Cool.
OK.
So let's go ahead and let's click this.
It should present the view controller.
OK.
Just like we expected.
And now if I type in a name like I don't know Amy and if I press submit what it should do is it should
pass in value.
And because of that let's go back to our main view controller because we passed a value on.
Next we're going to take that name property pass into our names or re dismiss ad name Visi which is
this one.
And remember if we append a value to names or say how that's working is basically the names array is
being joined with a separator.
So when I press it watch what happens nothing because we forgot something very very important names
array is a variable.
We're expecting it to act like an observable.
But if you remember a variable in and of itself is not an observable.
It's like an observable but it can be used like a regular variable.
We need to cast it as an observable in order for it to actually work the way we want.
So inside you did load We're going to do that.
We're going to call names right.
Which is of type variable.
We're going to call datt as observable.
And now it's an observable we can subscribe to.
We weren't actually successfully subscribing to the changes being passed into name array.
So go ahead and call subscribe now that we can and we're going to call on next like you may have expected.
And inside of Subscribe we're going to go ahead and set up the names label to be whatever gets passed
in when we update names.
Right now we do that in AD name Visi because we call on next we pass in value to the name subject and
check it out when we are in the view controller here when we call on next for the name subject.
We set the value here to append whatever name is passed in.
So what we need to do is we need to basically set it up so that the values returned are actually set
up to show in the textfield which we didn't do yet.
Now we're going to go ahead and set up a property here for the closure and we're just going to say names
in and in this just to denote that it's a closure.
Names is a property that is going to hold the value of whatever was passed in when we subscribed.
So go ahead and call self names label text and we're going to set this to be equal to names which comes
in as an array of string and we can call DOT joined with separator comma and a space.
And now this array is basically going to take the values that we had in the array but we are now able
to pass them in.
Thanks to our observable sequence.
OK so let's go ahead and build and run this.
Let's go check to see if that worked OK.
You know what we're not disposing at though let's go ahead and call our disposable to dispose bag.
It's not mandatory but it definitely can cause memory issues.
If you don't do it.
So let's go ahead and let's extend this a little bit.
Let's build and run it again.
Now that we're properly disposing and we'll see if this updates and works the way we expect.
So here we go let's add in names the normal way Cayla we go Bob Jane.
And now let's click add name.
Let's go ahead and type in Chris and watch what happens when I click submit.
It adds to the bottom view controller because we are setting the label to be equal to the names array
passed in from our names here.
Now we are subscribing to names IRA.
That means whenever something is pushed into names array we get an array back.
OK.
And we can set our label to be equal to that.
Names are joined with the separator.
Now this is only because we are taking it from another view controller check it out.
Down here we create an instance of ADD named VC and the name subject is where we are pushing in names
from that other view controller.
But when we push in a name right we take that singular name and we add it to our names array.
Right now that is why up here this is this is why without this it didn't work because the values pushed
in here are only reflected because we are subscribing to this names array.
Otherwise our submit button here with the label it doesn't know how to interact with that because here
when we push the submit button on the green view controller we are passing in the value just like that.
This basically is what connected the two of you controllers.
We created this as an observable.
And whenever we push in a new value we're setting up the names label to include the newest data.
That's what makes our swift super cool is you can do things like this that you would normally have to
write a bunch of boilerplate code.
But now I can just type in John and add him there or I could do another one Billy and add him there.
OK.
We're doing the same thing with a single asynchronous stream of data.
So so cool.
So guys this app is now finished and then the next section we're going to move on to our capstone project
where we're going to build versey a get hub client using our swift.
Very very cool.
Let's get over to that section now.
Great job with this one guys.