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