Hey what's up everyone.
Mark Price you're at death's dot com and we're going to continue forward in our swoosh app.
So far what we've done is we have built out segues so we can swap between screens we talked about a
common error we had to fix it.
And we actually just built all the designs and everything and so in this one let's actually create some
logic with the buttons and learn how to pass data between View controllers which is a very important
thing and we also get lots of questions about that as well too.
And so you get to learn some core foundational stuff stuff and first things first.
Don't let me go ahead and just do some good stuff.
So I'm just going to get ad get commit which you should always be doing in your code.
You don't need to follow this format here with all these different branches but you should always be
using get and always adding and always committing.
You will always use it in your job and pretty much everyone.
Ninety nine percent of companies out there are using get nowadays so really important stuff for Lufa.
All right so get commit dash.
And what we did was we did segues programmatically programmatically.
All right.
Perfect.
So you get.
Check out dash Ashby.
Let's actually push this pushes first get push.
Origin.
Lesson 0 8.
When I push it to the origin it means it's pushing it to you know get help or bit bucket or whatever
the remote origin is.
So the code is there as well to now get check out.
Ashby lesson 0 9.
If you're wondering why the keyboard sounds a little bit different is because I just switch keyboards
to cool razored gaming mechanical keyboard.
And the reason why is Logitech keyboards absolutely suck.
And they always stop working.
And even though they look cool and they're quiet they stop working.
And so these ones are very reliable.
OK.
All right with get out of the way here.
Let's go ahead and open up our project.
All right.
We want this one right here.
App.
Swoosh.
Great So here's our storyboards with our segues.
And of course we can go back and forth between the different view controllers.
And so let's talk about what we want to do.
We've got a screen here so that we could get started.
Go here.
Now there's some logic that needs to happen.
A user is going to select mens womens or coed.
OK.
And what needs to happen is before someone can actually go in and click the next button.
OK.
They should have to select one of these options here.
And then once we get past this screen here we need get past the selected data over here to our skill
Visi so we can then further enhance the choice.
Whether you're a beginner or a baller and then finish and then basically if we had other screens you
would do something with that data.
So what we're going to do is we're going to learn how to store the data in the screen and then how to
pass it on over here.
And we also do some logic here with the next ball and so that it doesn't let you tap it unless there
is the proper Rella data in place so let's go ahead and get started.
First things first let's click this next button let's just disable it by default.
So it can't it cannot be tapped on.
So see this enabled button.
We're going to do is turn it off.
And what we'll do is when one of these options is selected we will then enable this button from code.
OK.
So in order to enable this from code we need to do is open up the assistant editor and make sure that
we have an I.V. outlet available for this button.
So even though we may have an I.V. action for our next tap We're also gonna need an IP outlet so we
can actually grab or reference and make changes to it.
So let's go ahead and control drag from the next button and leave it as an outlet and what we're going
to do is call this next beat like so.
OK.
Very good.
And you're going to notice when we work with ibl you're going to see this week a lot.
OK.
What does weak mean.
Well it kind of means what you think it means you know you the strong and you get the weak.
And in this case when it comes to coding and in time you make a variable of some kind.
It's stored in memory.
Ok has a memory address and then different parts of your program can access the memory they can access
that slot in memory or the item that's there.
Well what happens is when you put an object here in your view controller OK your view controller itself.
OK here this intern here in the Interface Builder this view controller it's actually going to hold a
reference to this button.
So just by virtue of dragging a button on here this view controller the user interface element is going
to hold a reference it's going to point to this button in memory.
OK.
So when we make an outlet we don't want to do that twice.
We don't want our code to point to the same spot in memory and for the Interface Builder file to point
to a memory because that's two different things holding on to it in memory.
And so let's say and let's say that this controller screen goes off the screen and gets unloaded.
You know if the code was still living we wouldn't want to reference it because it's no longer needed
and vice versa.
And it may be hard to understand you don't have to understand it completely right now.
But the important thing to understand is that when you're working with IBM let's make sure your variables
are weak so the UI and the code are not both referencing what we will do is mean and this view controllers
unloaded out of memory.
OK.
It's a reference to this button will go away.
And if this says weak this one will drop off as well too.
Otherwise it would hold onto it and you could have a memory leak.
You could have this button in memory even though the screen is not present to the user and it can no
longer use that spot of memory and therefore your app can't be as powerful as it needs to be so a lot
of information there make your ibl it's weak it's there by default.
OK.
So when the screen loads it will automatically be disabled because we said that in the interface builder
and so the next thing I want to do is let's go ahead and set these Iby actions here.
So we'll set one for Menz.
So changes from outlet to action and we're say on mennes tapped same thing for women's on women's tab.
And that was I already made a mistake or there was an I.V. outlet instead of instead of a b action and
let's go ahead and change it to an action.
And this must say on the women's tab.
And one more for the code.
Changes to an action and say on CO tab perfect what I'm gonna to use right click this league Visi and
the document outline make sure that I get rid of this woman's tact because remember how I accidently
made it an Ivy outlet and then I deleted it from the code.
Remember every time you delete it from the code you have to go over here and delete it as well too.
From the document outline an interface builder that would have in a crash or app and I just saved you
from crashing your app.
OK.
Well it was my app and I'm scratching it but OK.
So this is looking good.
Let's go ahead and close the assistant editor and let's go into league visi.
All right thinking aloud here.
So the idea is a user comes to the screen.
They select an option when they select an option the next button becomes enabled.
OK.
And then then they can move on to the next screen.
So that sounds good to me.
Let's just do some cleanup here.
Cool.
So the information how are we going to store it we're going to make it a very we'll pass it on.
Well what I think we need to do is actually create a model.
All right let's go to the model folder here.
We're going to create a new model here called player.
You're a player in the in the app.
And there's different data that it's going to store.
OK.
So empty file is fine.
Whenever you're creating model files or just data files with with just swift only stuff use a with file
and import foundation.
No need to import it.
And what we're going to do is we're going to call this player.
Okay great.
And what we're can is we're in a create a struct and we're going to call this player which should use
a struct.
And when should use a class will always always always use a struct whenever you can.
OK.
So use a struct when using default swap type strings and things like that.
Use a class when you have to do have to have custom things implemented.
You know your own custom classes so that hold other classes or custom objects like that but a struct
is a value type.
OK.
And so it can be copied over directly versus a class which is an object stored in memory and you can't
copy it.
And so use a struct whenever you can because they're faster much much faster so we're going to save
our much faster than a class so far.
And what do we want to do.
We want to store the league.
So let's just say selected well called desired league go because that's what I haven't named the image
so desired league and we'll make it a string.
So right now it's an empty string.
OK.
Or you know what.
Let's just actually make it an optional.
So we're going to say string optional Ambar and we're going to say the desired leagues and then there
was the skill level so selected skill level and we'll call this a string as well to that's looking good
there.
OK.
So desired leagues like the skill level.
Cool.
What we're going to do is here in our league.
VC OK we're going to create a player will declare at the top.
That's the keyword right there.
Declare is when you declare a variable but not necessarily initialize it.
So what we're going to do is we're going to say bar and then we're going to say player of type player
and let's make this and implicitly unwrapped optional.
And the reason why we want to make it implicit is because we don't want this code to even run unless
there's a player.
OK.
And so we make it an option on what we can do is here in view to load all we have to do is initialized
it we can say player equals player and that's how you initialize a struct with the two parentheses there
like so.
So when you did load occurs and it's going to create a new player.
All right.
And so what we have here is we've got a model that can store data and then eventually we'll pass this
data onto another controller.
But first let's go ahead and handle the logic.
So on men's tapped on woman's tap uncoded tapped What do we need to do.
All right well let's go ahead and write it out so when the men's button is tapped all we need to do
is say player Daut desired league OK say equals minutes.
All right.
That's just a variable I'm creating.
This is something that's going to let us know hey they selected the men's league.
All right and here we can say player desirably equals women's all right in here player desired league
equals coed.
OK.
So far so good.
So when each of these buttons is tapped Let's go ahead and in this struct here let's set the variable
of the desired league whatever it needs to be.
And then of course in each one we want to enable that next button right so we say the next button that
enables is enabled equals let's say true.
OK.
And you know we could copy this like so and like so.
But what very important principle that I teach you that we are violating what Principal redialing we're
violating the principal look at this next but next what next.
And this the reason why we don't want repeat herself is because this could introduce logic errors like
what if a programmer accidentally has or going through doing this three times or maybe 10 times they
said false.
They did autocomplete and like that false.
And now there's a logic error so there's things like that that can happen.
And so what we want to do.
We just want to we want to clean this up a little bit we want to refactor make it mo better.
So what we're going to do is right here I'm going to create a function here and we're going to call
select League.
OK.
And for the parameters we want to pass in the league type so to say League type and this is going to
be of type string.
All right.
And let's make our function here.
I forgot to add the word function.
There we go.
OK.
So there's the parameter and what we'll do here is we will say player desired league equals League type.
OK so we're going to pass in the league type into this function and then it'll set it here and then
waltz then we'll say the next button is enabled equals true.
OK.
And now what we can do is we can say select League Pass in men's K and then in this one something similar.
Select league and pass in winnings.
OK.
And and this one will do the same thing.
So select select league and we'll pass in the word co-head and let's clean this up
school.
And so now what we've done is we've we've clean this up a little bit so the next buttons only being
set once and then these functions are passing in their various data.
And this is what I was telling you about earlier.
I try to never ever ever have logic built into Iby actions.
OK.
Logic's happening here and the actions just trigger what button was pressed.
And how do we need to act accordingly.
OK so what's happened so far is a user has tapped something and the Next button is now enabled.
OK.
One thing I do want to do though is I want to uncomment this just to make sure the next button doesn't
even work without pressing one of these buttons.
Let's go ahead and run the app and see OK our app has it loaded and what we want to do is click get
started.
And so this but should not work.
Remember we wrote code to load the next screen.
So when I clicked this.
Nothing should happen and it's not so that's a good sign.
Now what I want to do is uncomment this here and let's rerun the app.
And what I want to do I guess I didn't need to combine to do it all.
What I want to do now is when I select a a league then it lets me go to the next one so right now I
can't do anything but when I click Menz for instance then we can go ahead and say next and boom there
we have it.
So that's looking really good right there.
Cool.
Making good progress.
So so far so good.
Let's talk about what we've done here is we've created IAB actions for the different buttons and we've
created a function called select league where we pass in the league that was selected and then we dis
enable the next button so they can go forward and then everything works as normal.
We also created a struct here.
OK we started building a model for application and you'll do this.
OK.
We might not have had to made a model meaning here in the welcome box.
OK.
Or excuse me in the league b c we could have made variables here we could have said our desired league
of type you know type of string right.
We could have done that and then one for the skill level and then we could pass those on to the next
controller that starts becoming really nasty when you have multiple variables.
Everything that you want to do in your apps you want to build around models.
And so here's a protip.
OK.
Protip is if you haven't built a model in your app you're doing it wrong.
Every piece of data that you need in your app should either be in a class or a struct it should be grouped
together.
So that's the protip all of the data in your app should always be always always always always be put
into a struct or a class.
OK.
So that's why we created the player.
It's reusable.
It's testable.
It's safer it's cleaner.
No I do not want to code the project Stop.
OK.
So let's go ahead and call this video done.
We've made some good progress here you've written some code started to build out some logic and in the
next video what we'll do is we will learn how to pass data from one view controller to another so they
can then operate on etc..
So that's it for now.
Mark Price at depth slopes dotcom move on forward.