Lots of ladies and gentlemen Mark pricer slopes dot com and so far we've got our segues loading a new
View Controller and we've done that through the interface builder and we are unwinding them back into
the previous view controller in this video.
You're going to learn how to load a Segway programmatically and it's really not that hard.
What we want to do is from the League of PC to the skill Visi we want to load the screen from code instead
of control dragging from the button now use case scenario.
OK.
Could I do it from the button Yeah.
And that probably makes more sense just to do it here.
But it's important that you learn to do it programmatically.
And sometimes you may have a job where the company or the boss requires that you do you have certain
things that you do not do in Interface Builder because when you do things that interface between you
can't really debug them.
OK.
You can't you can't set breakpoints on Interface Builder you can't.
You can have bugs and not know it.
And so some companies actually have coding standards where you don't do certain things.
Interface Builder In fact some companies don't even like using it Interface Builder at all which is
really interesting.
It all depends so it's important to know both ways and also the last thing is you may want to use a
programmatic Segway when you don't know which screen you're going to load for instance on a game.
Let's say based on your high score OK you may go to the new level.
Otherwise you go to the previous level when the level you're on ends and you didn't know beforehand
which screen was going to be loaded because it was based on player decisions.
And so there's no way I could drag stuff from Interface Builder to go to one or more screens.
So you have to write it programmatically if this happened in the game or the app load the screen.
Otherwise load the screen and you would have to do that in code.
Ok so you will be doing this all the time.
In fact in the def slopes app OK we probably 90 percent of our segues are all programmatic.
So anyway let's go ahead and do that now so this is the league visi.
And so what we want to do is when that next button is clicked we want to load a Segway in fact what
we're probably going to do first is create an IB action.
So let's open up the assistant editor make sure it says League AVC on the right hand side over here
which it does in this case here.
And if it doesn't for some reason you can always change it appears the OS is automatic.
You can actually go to manual and then you can actually go and look through all the files until you
find the right view controller.
So you can do that as well too.
And what we're going to do is this next button we're going to control drag from interface builder and
right above this view did load override function.
We're going to let go change this from an outlet to an action.
OK because we don't want to reference a button we want the button to do something.
OK.
And the event we want touch up inside that's the standard button event when you tap button.
This is what will be called and that's what we want and the name will just say on next tapped.
I like to be explicit in what happened when the buttons tapped.
I typically do not ever have like code that runs that does logic and thinking within an action.
OK because Iby actions are really just an action of what's happening when this when this event happens
or how do we handle that.
So I usually make it descriptive like so and we're going to click connect and there's our function there.
So let's close the assistant editor and let's actually go into the code now.
I just saved.
And here in the league see we have this new IAB action.
OK.
Make a few spaces here.
And what I'd like to do is I actually don't like to have these up here at the top.
I like my reactions down here and I don't need this did receive memory warning because this is a simple
app and we're not going to have any memory warnings.
OK.
So there's my Iby action on next tapped and sender.
That's the button itself.
They're going to send it to me if I wanted to access it.
Change its name or something like that whatever.
We're not going to use that either.
And so we're going to do to show you controller is right a line of code to do so.
Pretty simple right.
So we're going to type and perform Segway with identifier and ascender.
And for the Senate we're going to say self and the identifier.
Well what's that.
Well remember how before I told you when we have a controlled drag segue you don't have to put an identifier.
But if you're doing it programmatically you do so will go out and create that identifier.
So back in the main storyboard.
All right let's go ahead and
make our segue so in this case we're not going to make a segue from the button.
OK.
In this case we're going to make a segue from one view controller to another so on the left hand side
here in the document outlined see this league Visi and how it's like that here.
All you got to do is control drag from this.
OK.
Over here to this new controller anywhere it doesn't matter it's just selecting the view controller
or going to click show.
So what's happening here is this is no longer a segue from a button.
This is a segue from view controller to view controller.
And anything can trigger it.
So long as it happens in the code.
OK.
So it was different before we were dragged from the button itself to the next View Controller.
In this case we dragged it from the view controller to the View Controller.
And so it's clicked this segue here and click this icon up here and in the identifier I typically make
the SEGUI identifier the name of the screen that it's going to.
So this one is going to the skill visi.
And I'll just call it Segway skill see segue.
And then let's go over back to our league Visi and the identifier is the one we just entered.
The skill Visi segue.
That's how it identifies it.
So it's saying hey which Segway do you want us to perform.
Because I could have I could have actually multiple Segway So if I zoom out here I remember that whole
notion I was telling you about you know pick which screen you want to go to in a game or something.
So if I had another view controller and I mean this is a good example here like for instance let's say
this was the men's screen if so selected the men's option.
Well what I could do is I could create another I can control drag another VC to the other you see here
make a segue.
And so let's say that this one right here was the women's.
OK so in this one I could say oh this is the you know the women's are on Guam's women's visi.
OK.
So what we have here is the you know pretend this is the men's Visi the women's and we can have the
COED.
And basically depending on which option was selected we load that screen.
OK.
Pretty cool stuff.
We only need one in this case.
Want to delete this and I'm going to delete this.
OK so we have our one segue here skill Visi segue.
And our next tap whenever it's tap We just want to perform the segue.
So this should work out of the box.
So again what we've done is we've not direct from the next button ok or the next step button.
I think that's what did this get started.
Yes so we did not draw from this button here this is next.
We just dragged from one view controller to another and we can load any Segway we want based on the
identifier performs Segway.
So now if I click NEXT it loads a segway because the next button hits this Iby action.
OK.
And we can perform Segway.
And if you're still confused you know I could have said you know if you know this is pseudo code it's
not going to work.
But if I said you know if you know mennes men's man was selected.
OK.
So to get to those parentheses.
My brain is always between web in Iowa.
There you go.
So if men was selected else you know we could say perform Segway.
And then we could load the you know the women's screen you know women's identifier and that's how you
would write code to make a conditionally based logic and show different screens.
Of course we don't have those screens.
This is a simpler app but the principles are the same.
So perform segue with identifier.
OK.
So we're going to call this video done.
That's pretty simple.
OK.
All you have to do to load a Segway or a view controller or perform a Segway is with this command here
perform Segway.
This is the function of the controller and it knows what to do with it.
And so you just put in the identifier of the Segway.
You always always always have to have a Segway with that identifier attached to something.
Otherwise it won't work.
You'll get a crash.
But we do have that connected and it's going to load it like so so that's programmatically loading segues
the other way of course was in your storyboard just to simply pick a button and drag on to view controller
when it's tapped it loads automatically so that one was an Interface Builder Segway and one was a programmatic
Segway programmatic Segway it was one line of code.
OK.
Still very simple but you have more power that way.
So that's it for now.
Marc price adead slopes dot com work and segues.