1
00:00:06,480 --> 00:00:15,390
Ladies and gentlemen Mark pricer slopes dot com and so far we've got our segues loading a new View Controller

2
00:00:15,870 --> 00:00:19,260
and we've done that through the interface builder and we're unwinding them back into the previous view

3
00:00:19,260 --> 00:00:21,170
controller in this video.

4
00:00:21,210 --> 00:00:26,610
You're going to learn how to load a Segway programmatically and it's really not that hard.

5
00:00:26,620 --> 00:00:33,060
What we want to do is from the League of to the skill Visi we want to load the screen from code instead

6
00:00:33,060 --> 00:00:36,790
of control dragging from the button now use case scenario.

7
00:00:36,840 --> 00:00:37,450
OK.

8
00:00:37,830 --> 00:00:39,290
Could I do it from the button Yeah.

9
00:00:39,390 --> 00:00:41,240
And that probably makes more sense just to do it here.

10
00:00:41,250 --> 00:00:43,370
But it's important that you learn to do it programmatically.

11
00:00:43,470 --> 00:00:49,590
And sometimes you may have a job where the company or the boss requires that you do you have certain

12
00:00:49,590 --> 00:00:53,310
things that you do not do an Interface Builder because when you do things that interface builder you

13
00:00:53,310 --> 00:00:54,410
can't really debug them.

14
00:00:54,490 --> 00:00:54,760
OK.

15
00:00:54,780 --> 00:00:59,490
You can't you can't set breakpoints on Interface Builder you can't.

16
00:00:59,490 --> 00:01:01,280
You can have bugs and not know it.

17
00:01:01,290 --> 00:01:05,910
And so some companies actually have coding standards where you don't do certain things.

18
00:01:05,910 --> 00:01:10,920
Interface Builder In fact some companies don't even like using it Interface Builder at all which is

19
00:01:10,920 --> 00:01:12,030
really interesting.

20
00:01:12,030 --> 00:01:17,070
It all depends so it's important to know both ways and also the last thing is you may want to use a

21
00:01:17,070 --> 00:01:21,540
programmatic Segway when you don't know what screen you're going to load for instance on a game.

22
00:01:21,600 --> 00:01:24,150
Let's say based on your high score.

23
00:01:24,360 --> 00:01:27,930
OK you may go to the new level.

24
00:01:28,110 --> 00:01:33,500
Otherwise you go to the previous level when the level you're on ends and you didn't know beforehand

25
00:01:33,510 --> 00:01:36,240
which screen was going to be loaded because it was based on player decisions.

26
00:01:36,240 --> 00:01:41,610
And so there's no way I could drag stuff from Interface Builder to go to one or more screens.

27
00:01:41,610 --> 00:01:45,820
So you have to write it programmatically if this happened in the game or the app load the screen.

28
00:01:45,840 --> 00:01:48,300
Otherwise load the screen and you would have to do that in code.

29
00:01:48,390 --> 00:01:51,130
Ok so you will be doing this all the time.

30
00:01:51,210 --> 00:01:59,090
In fact in the def slopes app OK we probably 90 percent of our segues are all programmatic.

31
00:01:59,140 --> 00:02:02,690
So anyway let's go ahead and do that now.

32
00:02:02,980 --> 00:02:04,370
This is the league visi.

33
00:02:04,390 --> 00:02:12,530
And so what we want to do is when that next button is clicked we want to load a Segway in fact what

34
00:02:12,530 --> 00:02:14,730
we're probably going to do first is create an IB action.

35
00:02:14,730 --> 00:02:20,490
So let's open up the assistant editor make sure it says League AVC on the right hand side over here

36
00:02:20,490 --> 00:02:22,520
which it does in this case here.

37
00:02:22,890 --> 00:02:26,360
And if it doesn't for some reason you can always change it up.

38
00:02:26,360 --> 00:02:27,570
Your is automatic.

39
00:02:27,690 --> 00:02:31,110
You can actually go to manual and then you can actually go and look through all the files until you

40
00:02:31,110 --> 00:02:32,360
find the right view controller.

41
00:02:32,400 --> 00:02:33,850
So you can do that as well too.

42
00:02:34,080 --> 00:02:38,250
And what we're going to do is on this next button we're going to control drag from interface builder

43
00:02:38,610 --> 00:02:41,070
and right above this view did load override function.

44
00:02:41,070 --> 00:02:44,620
We're going to let go change this from an outlet to an action.

45
00:02:44,700 --> 00:02:48,600
OK because we don't want to reference a button we want the button to do something.

46
00:02:49,050 --> 00:02:49,700
OK.

47
00:02:49,950 --> 00:02:54,390
And the event we want touch up inside that's the standard button event when you tap button.

48
00:02:54,390 --> 00:03:00,830
This is what will be called and that's what we want and the name will just say on next tapped.

49
00:03:00,870 --> 00:03:04,180
I like to be explicit in what happened when the buttons tapped.

50
00:03:04,290 --> 00:03:10,800
I typically do not ever have a code that runs that does logic and thinking within an action.

51
00:03:10,800 --> 00:03:12,820
OK because Iby actions are really just an action.

52
00:03:12,820 --> 00:03:16,860
What's happening when this when this event happens or how do we handle that.

53
00:03:16,860 --> 00:03:23,640
So I usually make it descriptive like so and we're going to click connect and there is our function

54
00:03:23,640 --> 00:03:23,840
there.

55
00:03:23,850 --> 00:03:27,820
So let's close the assistant editor and let's actually go into the code now.

56
00:03:27,900 --> 00:03:28,830
I just saved.

57
00:03:28,980 --> 00:03:32,090
And here in the league we see we have this new Iby action.

58
00:03:32,240 --> 00:03:33,130
OK.

59
00:03:33,840 --> 00:03:34,730
Make a few spaces here.

60
00:03:34,740 --> 00:03:38,130
And what I'd like to do is I actually don't like to have these appear at the top.

61
00:03:38,240 --> 00:03:42,480
I like my actions down here and I don't need this did receive memory warning because this is a simple

62
00:03:42,480 --> 00:03:42,700
app.

63
00:03:42,710 --> 00:03:44,780
We're not going to have any memory warnings.

64
00:03:44,790 --> 00:03:44,990
OK.

65
00:03:45,000 --> 00:03:48,650
So there's my Iby action on next tapped and sender.

66
00:03:48,750 --> 00:03:50,100
That's the button itself.

67
00:03:50,100 --> 00:03:52,200
They're going to send it to me if I wanted to access it.

68
00:03:52,200 --> 00:03:54,660
Change its name or something like that whatever.

69
00:03:54,760 --> 00:03:56,230
We're not going to use that either.

70
00:03:56,250 --> 00:04:02,660
And so we're going to do to show you controller is right a line of code to do so.

71
00:04:03,180 --> 00:04:04,160
Pretty simple right.

72
00:04:05,200 --> 00:04:12,960
So we're going to type and perform Segway with identifier and ascender.

73
00:04:13,010 --> 00:04:15,880
And for the center we're going to say self and the identifier.

74
00:04:15,890 --> 00:04:16,950
Well what's that.

75
00:04:17,000 --> 00:04:23,230
Well remember how before I told you when we have a controlled drag Segway you don't have to put an identifier.

76
00:04:23,240 --> 00:04:27,100
But if you're doing it programmatically you do so we'll go out and create that identifier.

77
00:04:27,100 --> 00:04:28,740
So back in the main storyboard.

78
00:04:29,000 --> 00:04:31,490
All right let's go ahead and

79
00:04:34,190 --> 00:04:38,850
make our Segway So in this case we're not going to make a segue from the button.

80
00:04:38,890 --> 00:04:39,530
OK.

81
00:04:39,700 --> 00:04:43,960
In this case we're going to make a segue from one view controller to another so on the left hand side

82
00:04:43,960 --> 00:04:47,120
here in the document outline see this league Visi and how it's like to here.

83
00:04:47,140 --> 00:04:49,850
All got to do is control drag from this.

84
00:04:50,020 --> 00:04:50,640
OK.

85
00:04:51,640 --> 00:04:55,290
Over here to this new controller anywhere it doesn't matter it's just selecting the view controller

86
00:04:56,020 --> 00:04:57,270
or going to click show.

87
00:04:57,550 --> 00:05:00,800
So what's happening here is this is no longer a segue from a button.

88
00:05:00,820 --> 00:05:03,090
This is a segue from view controller to view controller.

89
00:05:03,090 --> 00:05:04,470
And anything can trigger it.

90
00:05:04,510 --> 00:05:06,080
So long as it happens in the code.

91
00:05:06,250 --> 00:05:06,960
OK.

92
00:05:06,970 --> 00:05:10,630
So it was different before we would drag from the button itself to the next view controller.

93
00:05:10,630 --> 00:05:15,070
In this case we dragged it from the view controller to the view controller and say let's click this

94
00:05:15,070 --> 00:05:23,060
segue here and click this icon up here and in the identifier I typically make the SEGUI identifier the

95
00:05:23,060 --> 00:05:24,830
name of the screen that it's going to.

96
00:05:24,830 --> 00:05:27,330
So this one is going to the skill visi.

97
00:05:27,500 --> 00:05:30,790
And I'll just call it Segway skill Visi segue.

98
00:05:31,610 --> 00:05:37,820
And then let's go back to our league Visi and the identifier is the one we just entered.

99
00:05:37,820 --> 00:05:38,990
The skill Visy segue.

100
00:05:38,990 --> 00:05:40,040
That's how it identifies it.

101
00:05:40,040 --> 00:05:42,440
So it's saying hey which Segway do you want us to perform.

102
00:05:42,620 --> 00:05:49,640
Because I could have I could have actually multiple segues So if I zoom out here I remember that whole

103
00:05:49,640 --> 00:05:54,170
notion I was telling you about you know pick which screen you want to go to in a game or something.

104
00:05:54,170 --> 00:06:00,080
So if I had another view controller and I mean this is a good example here like for instance let's say

105
00:06:00,080 --> 00:06:03,350
this was the men's screen if so unselected the men's option.

106
00:06:03,530 --> 00:06:10,520
Well what I could do is I could create another I can control drag another VC to the other you see here

107
00:06:10,670 --> 00:06:11,650
make a segue.

108
00:06:11,840 --> 00:06:14,200
And so let's say that this one right here was the women's.

109
00:06:14,280 --> 00:06:23,570
OK so in this one I could say oh this is the you know the women's are on Guam's women's visi.

110
00:06:24,250 --> 00:06:24,880
OK.

111
00:06:25,100 --> 00:06:30,330
So what we have here is the you know pretend this is the men's Visi the women's and we can have the

112
00:06:30,330 --> 00:06:31,090
COED.

113
00:06:31,470 --> 00:06:35,800
And basically depending on which option was selected we load that screen.

114
00:06:35,940 --> 00:06:36,350
OK.

115
00:06:36,450 --> 00:06:37,140
Pretty cool stuff.

116
00:06:37,140 --> 00:06:38,390
We only need one in this case.

117
00:06:38,390 --> 00:06:41,330
Want to delete this and I'm going to delete this.

118
00:06:41,640 --> 00:06:45,090
OK so we have our one segue here skil Visi segue.

119
00:06:46,110 --> 00:06:49,690
And on next tap whenever it's tapped We just want to perform the segue.

120
00:06:49,830 --> 00:06:51,290
So this should work out of the box.

121
00:06:51,390 --> 00:06:58,770
So again what we've done is we've not direct from the next button ok or the next step button.

122
00:06:58,800 --> 00:07:00,400
I think that's what did this get started.

123
00:07:00,570 --> 00:07:02,790
Yes so we did not draw from this button here this is next.

124
00:07:02,790 --> 00:07:08,280
We just direct from one view controller to another and we can load any segue we want based on the identifier

125
00:07:08,400 --> 00:07:09,270
perform Segway.

126
00:07:09,270 --> 00:07:14,360
So now if I click Next it goes a segue because the next button hits this Iby action.

127
00:07:14,520 --> 00:07:15,140
OK.

128
00:07:15,390 --> 00:07:16,440
And we can perform a Segway.

129
00:07:16,440 --> 00:07:23,160
And if you're still confused you know I could have said you know if you know this is pseudo code it's

130
00:07:23,160 --> 00:07:23,750
not going to work.

131
00:07:23,760 --> 00:07:30,760
But if I said you know if you know men's men's men was selected.

132
00:07:30,900 --> 00:07:32,580
OK.

133
00:07:32,700 --> 00:07:35,050
Like so to those parentheses.

134
00:07:35,190 --> 00:07:37,200
My brain is always between web and Iowa.

135
00:07:37,210 --> 00:07:38,160
There you go.

136
00:07:38,160 --> 00:07:46,060
So if men were selected else you know we could say perform Segway and then we could load the women scream

137
00:07:46,130 --> 00:07:50,850
you know women's identifier and that's how you would write code to make a conditionally based logic

138
00:07:50,850 --> 00:07:52,530
and show different screens.

139
00:07:52,860 --> 00:07:54,580
Of course we don't have those screens.

140
00:07:54,660 --> 00:07:57,890
This is a simpler app but the principles are the same.

141
00:07:57,900 --> 00:08:00,060
So perform Segway with identifier.

142
00:08:00,390 --> 00:08:01,030
OK.

143
00:08:01,050 --> 00:08:03,000
So we're going to call this video done.

144
00:08:03,090 --> 00:08:04,150
That's pretty simple.

145
00:08:04,200 --> 00:08:04,790
OK.

146
00:08:05,010 --> 00:08:12,260
All you have to do to load a Segway or a new controller or perform a Segway is with this command here

147
00:08:12,300 --> 00:08:13,200
performs Segway.

148
00:08:13,200 --> 00:08:16,840
This is the function of the new controller and it knows what to do with it.

149
00:08:16,920 --> 00:08:19,840
And so you just put in the identifier of the Segway.

150
00:08:19,920 --> 00:08:24,840
You always always always have to have a Segway with that identifier attached to something otherwise

151
00:08:24,840 --> 00:08:25,530
it won't work.

152
00:08:25,530 --> 00:08:26,630
You'll get a crash.

153
00:08:27,150 --> 00:08:28,430
But we do have that connected.

154
00:08:28,590 --> 00:08:32,850
And it's going to load it like so so that's programmatically loading segues the other way of course

155
00:08:32,850 --> 00:08:37,920
was in your storyboard just to simply pick a button and drag out into view control when it's tapped

156
00:08:37,920 --> 00:08:43,040
it loads automatically so one was an Interface Builder Segway and one was a programmatic Segway programmatic

157
00:08:43,110 --> 00:08:44,620
Segway was one line of code.

158
00:08:44,730 --> 00:08:45,180
OK.

159
00:08:45,210 --> 00:08:47,730
Still very simple but you have more power that way.

160
00:08:47,730 --> 00:08:48,550
So that's it for now.

161
00:08:48,570 --> 00:08:52,160
Marc price devs Lokes dot com working with segues.
