1
00:00:05,460 --> 00:00:10,570
You know what's up everyone mark right here at the slopes dot com and we've done some really cool stuff

2
00:00:10,900 --> 00:00:14,890
in our apps so far we've gotten the user interface built.

3
00:00:14,890 --> 00:00:17,450
We've got segues so you can go back and forth between screens.

4
00:00:17,450 --> 00:00:23,230
We even now got a model built and re-actions tied to our league Visi and we don't let you go onto the

5
00:00:23,230 --> 00:00:25,980
next screen unless you've selected an option which is really cool.

6
00:00:25,990 --> 00:00:28,990
And then the data of course is stored in the player model.

7
00:00:28,990 --> 00:00:30,300
It's a struct.

8
00:00:30,430 --> 00:00:36,910
And so the next thing and possibly last thing we need to do here is actually pass data between View

9
00:00:36,910 --> 00:00:37,510
controllers.

10
00:00:37,510 --> 00:00:42,310
Now this is a topic that a lot of people struggle with and then they've done some really weird things

11
00:00:42,560 --> 00:00:47,250
to get data talking between View controllers are going to make it really easy for you.

12
00:00:47,260 --> 00:00:47,680
OK.

13
00:00:47,680 --> 00:00:52,750
And the function that we're looking for here is already built in your view controller whenever you create

14
00:00:52,750 --> 00:00:56,140
a new one and it's called prepare for Segway.

15
00:00:56,310 --> 00:00:57,010
OK.

16
00:00:57,100 --> 00:00:59,190
And when you read a function by the way.

17
00:00:59,210 --> 00:00:59,690
OK.

18
00:00:59,720 --> 00:01:01,850
When you get the actual function name.

19
00:01:01,900 --> 00:01:02,270
All right.

20
00:01:02,320 --> 00:01:03,340
It's going to look like this.

21
00:01:03,340 --> 00:01:05,700
Prepare for Segway.

22
00:01:05,710 --> 00:01:11,620
So this right here is the name of a parameter and this is the variable name that the parameter is going

23
00:01:11,620 --> 00:01:12,430
to take on.

24
00:01:12,700 --> 00:01:19,240
And so this this and this all kind of compose of the function definition of the function name.

25
00:01:19,240 --> 00:01:24,130
And if you have other subsequent parameters those will also be added to the name of the function and

26
00:01:24,130 --> 00:01:25,530
to show you what I mean.

27
00:01:25,870 --> 00:01:27,640
So this is right here all nice ready for us.

28
00:01:27,640 --> 00:01:31,010
But if I type in the word prepare OK.

29
00:01:31,300 --> 00:01:37,190
Notice how it says prepare for Segway interface builder sender it's all right there boom OK.

30
00:01:37,230 --> 00:01:38,630
So that's the function definition.

31
00:01:38,640 --> 00:01:39,560
OK.

32
00:01:39,660 --> 00:01:46,060
And it's an override function meaning the default view controller that you're inheriting from We'll

33
00:01:46,110 --> 00:01:50,990
call this and we don't need to call super on this or anything like that because we're overwriting it.

34
00:01:51,180 --> 00:01:54,190
Typically what that means is when you are writing and you don't call super.

35
00:01:54,210 --> 00:01:59,000
It means that you don't need the view controller implementation of it you don't need that code to run.

36
00:01:59,040 --> 00:02:01,470
Our code is going to take over from here.

37
00:02:01,470 --> 00:02:03,850
Otherwise super would call that parent code.

38
00:02:04,000 --> 00:02:10,320
OK so what we want to do is we want to see which side we were going to again remember what I said.

39
00:02:10,350 --> 00:02:15,300
I said if you have multiple segues that can be triggered from a single controller you can need to check

40
00:02:15,300 --> 00:02:16,180
which one is which.

41
00:02:16,200 --> 00:02:18,490
And you might pass different data to different segues.

42
00:02:18,510 --> 00:02:19,190
OK.

43
00:02:19,290 --> 00:02:22,770
In our case we only have one so it makes it really nice but we're still going to have to write the same

44
00:02:22,770 --> 00:02:25,440
code we would write if there were multiple segues.

45
00:02:25,560 --> 00:02:27,280
And this is the code.

46
00:02:27,420 --> 00:02:39,820
So if let And let's just say skill B C equals Segway destination as skewl B C.

47
00:02:40,720 --> 00:02:41,060
All right.

48
00:02:41,100 --> 00:02:42,520
Let me let me break it down.

49
00:02:42,610 --> 00:02:42,910
OK.

50
00:02:42,910 --> 00:02:48,710
So what we're saying here is we're doing it if let's sequence and went and flipped sequences.

51
00:02:48,870 --> 00:02:49,560
OK.

52
00:02:49,630 --> 00:02:52,930
Basically it will try and run an operation.

53
00:02:52,980 --> 00:02:58,330
OK will try and run the statement in this case it's going to try and assign the destination view controller

54
00:02:58,360 --> 00:03:00,950
into this variable or this constant.

55
00:03:00,950 --> 00:03:02,010
The lead is a constant.

56
00:03:02,110 --> 00:03:02,340
OK.

57
00:03:02,350 --> 00:03:08,140
So it's going to try to do this if it succeeds it will execute inside the code block if it fails then

58
00:03:08,140 --> 00:03:09,730
this statement will fail.

59
00:03:10,090 --> 00:03:10,430
OK.

60
00:03:10,450 --> 00:03:16,770
So if Segway that destination so were the failure could happen is right here.

61
00:03:16,810 --> 00:03:21,930
As the as with the question mark is saying let's grab the Segway destination.

62
00:03:22,040 --> 00:03:28,520
Well if you look at here at this destination by command click it destination is of type you view controller

63
00:03:28,530 --> 00:03:31,100
so it's just a generic view controller.

64
00:03:31,310 --> 00:03:36,650
But our destination that we want to go to is the skill of these you know skilled VC does inherit from

65
00:03:36,650 --> 00:03:37,700
you a view controller.

66
00:03:38,090 --> 00:03:46,490
And so with object oriented programming we can downcast a skill VC or rather a destination view controller

67
00:03:46,490 --> 00:03:48,880
as a skill AVC because it does inherent.

68
00:03:49,010 --> 00:03:53,360
But let's say stewe skill AVC did not inherit from you view controller it was just her own class then

69
00:03:53,360 --> 00:03:56,440
this would fill but it won't in this case.

70
00:03:56,450 --> 00:04:00,890
So what we're saying here is let's grab the destination that we're going to go to so it checks the Segway

71
00:04:01,880 --> 00:04:03,510
for the destination view controller.

72
00:04:03,530 --> 00:04:05,470
So the skill Visi in this case.

73
00:04:05,620 --> 00:04:06,470
OK.

74
00:04:06,560 --> 00:04:08,870
And then it's going to try and grab it.

75
00:04:08,930 --> 00:04:12,350
So it's going to grab the view controller code that exists what's in our storyboard.

76
00:04:12,360 --> 00:04:18,020
So this file right here OK it's going to load it in memory it's going to try and it's going to grab

77
00:04:18,020 --> 00:04:18,350
it.

78
00:04:18,650 --> 00:04:23,960
And of course it'll work because that's what's designed to work and it'll grab this code right here

79
00:04:24,050 --> 00:04:28,160
and it's going to send it back to you and store it here in this variable.

80
00:04:28,510 --> 00:04:29,250
OK.

81
00:04:29,330 --> 00:04:35,300
And to prepare for Segway happens whenever a Segway is triggered this prepare function is going to be

82
00:04:35,300 --> 00:04:40,010
called so it lets you do things before the Segway actually loads on the screen.

83
00:04:40,070 --> 00:04:42,890
And this is where you will pass data between controllers.

84
00:04:42,890 --> 00:04:47,680
So again what we're doing here is we're saying it's the skill we.

85
00:04:47,690 --> 00:04:52,130
So if we can grab it successively then we're going to do something again and we had multiple controllers

86
00:04:52,130 --> 00:04:56,440
that you could choose from let's say there was five different screens you might go to based on the selection.

87
00:04:56,720 --> 00:05:02,900
Then what we would do is we would have like if let you know and then you know else if let And then we

88
00:05:02,900 --> 00:05:05,560
would say you know other VC.

89
00:05:05,560 --> 00:05:09,340
And then we would cast a different Visi and then it would go down until it finds the proper one.

90
00:05:09,350 --> 00:05:10,510
And then we send the data.

91
00:05:10,630 --> 00:05:11,360
OK.

92
00:05:11,690 --> 00:05:15,080
So we're grabbing the next few controller we want to go to.

93
00:05:15,080 --> 00:05:16,940
And if this is confusing to you it's OK.

94
00:05:17,000 --> 00:05:18,780
You're just learning this takes time.

95
00:05:18,830 --> 00:05:22,670
You're going to do this many times over and you can always watch this video again.

96
00:05:22,700 --> 00:05:22,880
OK.

97
00:05:22,880 --> 00:05:27,720
So grab the skill we see and what we want to do we want to pass data into it.

98
00:05:27,800 --> 00:05:34,730
So what we're going to need to do is store a player variable in our schedule VC and we don't have one

99
00:05:34,730 --> 00:05:36,000
yet so let's do that now.

100
00:05:36,280 --> 00:05:45,140
So over here let's go ahead and say var player type player and let's do an pleasantly unwrapped optional.

101
00:05:45,170 --> 00:05:51,030
What that means is no matter what we promise we promise that there's going to be code in there.

102
00:05:51,030 --> 00:05:52,320
It's not going to be nil.

103
00:05:52,340 --> 00:05:53,280
OK.

104
00:05:53,300 --> 00:05:55,030
And so that's what we're promising.

105
00:05:55,040 --> 00:06:02,330
And so what we're going to do is we're going to go over here and we are going to say skill Visi player

106
00:06:02,480 --> 00:06:07,790
the one that we just created OK and not save the file.

107
00:06:14,140 --> 00:06:18,610
Skill it up player there we go.

108
00:06:18,610 --> 00:06:19,360
Now it's popping up.

109
00:06:19,390 --> 00:06:19,860
OK.

110
00:06:20,020 --> 00:06:23,670
So skill it up player equals player.

111
00:06:24,040 --> 00:06:25,450
You're like what's going on here.

112
00:06:25,480 --> 00:06:27,240
These names of the same.

113
00:06:27,250 --> 00:06:31,390
Well we are referring to the player that we saved and this new controller here.

114
00:06:31,420 --> 00:06:31,720
OK.

115
00:06:31,730 --> 00:06:33,620
This is here in league visi.

116
00:06:33,640 --> 00:06:40,180
We've set the player here and what we're doing is we are just passing this player from the screen onto

117
00:06:40,180 --> 00:06:41,410
the player on the screen.

118
00:06:41,410 --> 00:06:46,420
So literally this is where the handoff comes you know like footballs being handed off UK same exact

119
00:06:46,420 --> 00:06:49,880
thing we're handing off the data from the player on the league.

120
00:06:50,290 --> 00:06:53,890
And we're storing it in the player in the skill of the sea.

121
00:06:53,890 --> 00:07:01,690
Now something really important to understand when you are in prepare for Segway this will always always

122
00:07:02,170 --> 00:07:09,550
always always be called before you did load is called on the destination view controller.

123
00:07:09,550 --> 00:07:19,180
So remember this very important prepare for Segway is always called before you did load on the destination

124
00:07:19,180 --> 00:07:19,760
view controller.

125
00:07:19,780 --> 00:07:26,650
So what you can do is you can always guarantee that data will be available in view to load if you initialized

126
00:07:26,650 --> 00:07:26,740
it.

127
00:07:26,740 --> 00:07:35,610
So if I was to do something like this player datt desired League.

128
00:07:36,590 --> 00:07:40,650
OK if I was to do that it will not be Neal.

129
00:07:40,700 --> 00:07:42,420
It can't be OK.

130
00:07:42,500 --> 00:07:46,280
And it says expression implicitly coerced from string.

131
00:07:46,280 --> 00:07:53,740
Now here in our player model it's because we have them as optionals And you know probably here we should

132
00:07:53,750 --> 00:07:58,280
have these as implicitly unwrap the optionals as well too because we have to have data in order for

133
00:07:58,870 --> 00:08:02,400
this to happen for this APTA run and this can be very confusing.

134
00:08:02,400 --> 00:08:02,960
OK.

135
00:08:03,260 --> 00:08:07,770
Do I make it optional do I make it implicitly unwrapped optional so remember this.

136
00:08:07,800 --> 00:08:09,990
OK here is the rule.

137
00:08:10,460 --> 00:08:17,590
If you can promise that data is going to be there I mean your app must have data in order to run.

138
00:08:17,690 --> 00:08:20,590
Then you can make it an implicitly unwrapped optional.

139
00:08:20,900 --> 00:08:24,900
If you don't know whether data is going to be there or not and that's part of your design.

140
00:08:25,050 --> 00:08:25,400
OK.

141
00:08:25,430 --> 00:08:27,510
It must be an optional.

142
00:08:27,660 --> 00:08:32,600
OK so if you are promised data is going to be there because your app can't run without it you can assume

143
00:08:32,600 --> 00:08:35,000
it's going to be there and make it implicitly unwrapped.

144
00:08:35,240 --> 00:08:38,960
If there was no data in here and you tried to access it yes it would crash but you're promising that

145
00:08:38,960 --> 00:08:43,580
there's going to be data there whereas the optional saying well might application design means that

146
00:08:43,580 --> 00:08:44,300
there might not be data.

147
00:08:44,300 --> 00:08:46,130
Maybe it's optional field who knows.

148
00:08:46,130 --> 00:08:47,840
So that's the rule.

149
00:08:47,840 --> 00:08:49,300
And again it could be confusing.

150
00:08:49,310 --> 00:08:51,620
It takes lots of practice but you'll get it.

151
00:08:51,620 --> 00:08:57,670
So this morning should go away and there we go.

152
00:08:57,670 --> 00:09:01,300
And so what we're doing is we're going to print this here so let's just go ahead and run our app and

153
00:09:01,300 --> 00:09:03,360
see if it actually prints as expected.

154
00:09:04,160 --> 00:09:08,470
So what should happen is we're going to select the desired League.

155
00:09:08,510 --> 00:09:08,920
OK.

156
00:09:08,960 --> 00:09:12,470
We're going to go to the next screen and when the next screen loads we print it should show the desired

157
00:09:12,470 --> 00:09:13,930
league that we selected.

158
00:09:13,970 --> 00:09:16,070
Hypothetically if we wrote the code correctly

159
00:09:24,200 --> 00:09:24,570
OK.

160
00:09:24,660 --> 00:09:26,500
So get started.

161
00:09:26,790 --> 00:09:28,720
Let's go ahead and select the women's league.

162
00:09:28,830 --> 00:09:34,990
And when I click Next I should see women's printed here on the screen and sure enough it was.

163
00:09:35,130 --> 00:09:35,560
OK.

164
00:09:35,580 --> 00:09:37,610
So are you with me so far.

165
00:09:37,610 --> 00:09:40,710
What happened was we triggered her segue.

166
00:09:40,750 --> 00:09:43,010
So right here.

167
00:09:43,030 --> 00:09:44,280
See this performs segue.

168
00:09:44,320 --> 00:09:46,300
We triggered that from the next button right.

169
00:09:46,360 --> 00:09:48,670
And we said let's load the skill AVC So this is called.

170
00:09:48,970 --> 00:09:52,440
And then the very next thing that's called is prepare for segue.

171
00:09:52,480 --> 00:09:57,760
And this is where we pass data from this view controller into the next using this code here and we set

172
00:09:57,760 --> 00:09:59,660
the data that we need.

173
00:09:59,850 --> 00:10:03,510
And it doesn't have to be in this very well we could call the function that sets it in a variable but

174
00:10:03,510 --> 00:10:10,020
somehow some way we need to set data on the destination view controller then the destination view controller

175
00:10:10,020 --> 00:10:14,470
which was the skill AVC in view to load our data is already ready to go.

176
00:10:14,520 --> 00:10:16,530
And you could print it and use it accordingly.

177
00:10:16,530 --> 00:10:19,620
So we've kind of just built the app right.

178
00:10:19,650 --> 00:10:22,410
What we've done is we built an app that has three screens.

179
00:10:22,420 --> 00:10:27,780
We've learned how to set data from some filt we selected a field we stored in a model and then we pass

180
00:10:27,780 --> 00:10:32,390
that data model between View Controller so we can use it and you could continue to do this.

181
00:10:32,460 --> 00:10:36,780
We could call another performed from Segway and another prepare and pass it on to the next one and pass

182
00:10:36,780 --> 00:10:38,820
it on to the next one and then the next one.

183
00:10:38,820 --> 00:10:42,680
Now if you're thinking that's a lot of work do we really want to pass data from view control of your

184
00:10:42,690 --> 00:10:43,800
controller.

185
00:10:43,800 --> 00:10:48,300
And I would say no later on you're going to learn how to work with some different design patterns like

186
00:10:48,300 --> 00:10:51,990
the singleton pattern where you have a central source that holds all your data.

187
00:10:51,990 --> 00:10:54,060
And that's a lot nicer than passing things through.

188
00:10:54,060 --> 00:10:56,240
But this is a very common practice as well too.

189
00:10:56,310 --> 00:11:02,300
And this is in the Apple documentation we've done everything exactly as Apple teaches in their documentation

190
00:11:02,340 --> 00:11:05,430
and it's very useful So that's it for now.

191
00:11:05,430 --> 00:11:07,080
This is really good stuff.

192
00:11:07,080 --> 00:11:11,730
Core foundational things that you need to know everything that I've taught you on the swish app.

193
00:11:11,910 --> 00:11:12,960
You have to know it now.

194
00:11:12,960 --> 00:11:17,010
And if you ever want to get a job in industry you have to know everything we've done in this app by

195
00:11:17,010 --> 00:11:17,390
heart.

196
00:11:17,480 --> 00:11:18,040
OK.

197
00:11:18,120 --> 00:11:21,480
There's nothing overly complex but the things that we have learned here you have to know by heart in

198
00:11:21,480 --> 00:11:25,770
order to get a job if you don't if you don't know these things that are in this lesson you're not ready

199
00:11:25,770 --> 00:11:30,190
for a job yet so you're going to need to master these very basic principles because this is what you're

200
00:11:30,190 --> 00:11:32,610
going to doing most of the time in your app development.

201
00:11:32,610 --> 00:11:33,960
So that's it for now.

202
00:11:33,960 --> 00:11:36,470
Marc price at Dev slopes dot com.

203
00:11:36,510 --> 00:11:37,820
Movin on and forward.
