1
00:00:08,100 --> 00:00:11,210
Hey everybody what's going on this is Caleb with Deb's Lopes dot com.

2
00:00:11,220 --> 00:00:17,370
And in this video I'm going to show you how we can use our ax swift to transmit and send values back

3
00:00:17,370 --> 00:00:22,580
and forth between View controllers just like you might with a Segway or even with protocols.

4
00:00:22,620 --> 00:00:26,300
It's really really cool and actually super easy in our X-Wing.

5
00:00:26,320 --> 00:00:30,490
So let's go ahead and let's pull open our project and let's get started.

6
00:00:30,510 --> 00:00:37,020
So we're first going to create a new view controller and a button on our current view controller right

7
00:00:37,020 --> 00:00:37,800
here.

8
00:00:37,800 --> 00:00:41,040
So let's begin actually by creating that view controller.

9
00:00:41,040 --> 00:00:45,170
Drag it on just like that looks good.

10
00:00:45,180 --> 00:00:48,250
And let's set a background color for this visi.

11
00:00:48,290 --> 00:00:51,830
I like this blue color which if you want to use it you can.

12
00:00:51,840 --> 00:00:54,660
It's 4 7 8 C6.

13
00:00:54,660 --> 00:00:56,390
Feel free to use that.

14
00:00:56,430 --> 00:01:01,650
And what we're going to do next is we're going to go ahead and copy and paste these into this view controller

15
00:01:02,040 --> 00:01:02,980
like so.

16
00:01:03,270 --> 00:01:06,490
And you know while I'm at it I'm going to put him in a stacked view.

17
00:01:06,600 --> 00:01:12,350
I'm going to go ahead and pin it one hundred twenty from the top 20 from the left 20 from the right.

18
00:01:12,510 --> 00:01:18,250
And let's give it a height of 120 then let's give it some spacing of 20.

19
00:01:18,570 --> 00:01:19,730
And that looks good.

20
00:01:19,890 --> 00:01:21,360
So you know what that's green.

21
00:01:21,370 --> 00:01:22,680
Let's change this to be blue.

22
00:01:22,680 --> 00:01:23,500
You don't have to.

23
00:01:23,580 --> 00:01:25,440
Just bothers me that it's not the same.

24
00:01:25,830 --> 00:01:28,470
So anyway that's great.

25
00:01:28,470 --> 00:01:35,340
Now let's go ahead and let's add a button to this view controller let's put it down here below the stack

26
00:01:35,340 --> 00:01:35,950
view.

27
00:01:36,330 --> 00:01:41,970
And let's just go ahead and type a plus sign and add name that looks good.

28
00:01:41,970 --> 00:01:44,560
Let's stretch it out so it fits.

29
00:01:44,700 --> 00:01:45,530
Looks awesome.

30
00:01:45,530 --> 00:01:50,810
Let's change the font and color to custom Avenir next.

31
00:01:50,810 --> 00:01:52,870
You guys know that's my favorite.

32
00:01:52,880 --> 00:01:54,950
And how about Demy bold.

33
00:01:54,950 --> 00:01:55,920
That looks good.

34
00:01:56,120 --> 00:01:58,520
And let's make it a size 20.

35
00:01:58,890 --> 00:02:03,480
Let's change the font color to white and let's give it some constraints to stretch it out.

36
00:02:03,740 --> 00:02:09,980
So we're going to set it to be eight from the top and let's do I don't know how about 30 from the left

37
00:02:09,980 --> 00:02:11,510
30 from the right.

38
00:02:11,540 --> 00:02:11,970
There we go.

39
00:02:11,980 --> 00:02:12,950
Got a nice big button.

40
00:02:12,950 --> 00:02:14,180
Hard to miss.

41
00:02:14,270 --> 00:02:19,780
And now all we need to do is basically link up these to their appropriate controllers.

42
00:02:19,790 --> 00:02:26,120
But this blue Visi doesn't have a view controller yet so let's right click on our X namer click new

43
00:02:26,120 --> 00:02:34,400
file Cocco touch class and let's name this add name Visi K. press next.

44
00:02:34,540 --> 00:02:36,220
Add it to your project.

45
00:02:36,220 --> 00:02:42,680
And now we're going to go ahead and enter the storyboard again and set this identity or set the identity

46
00:02:42,710 --> 00:02:43,600
of this view controller.

47
00:02:43,610 --> 00:02:49,680
So select the VC select the identity inspector and change the class to add name visi.

48
00:02:50,000 --> 00:02:53,840
And while we're at it let's go ahead and set up the storyboard ideas well because that's what we're

49
00:02:53,840 --> 00:02:56,870
going to use to instantiate the view controller later.

50
00:02:56,870 --> 00:03:02,820
So copy this paste it into storyboard ID and press enter to lock it in place.

51
00:03:02,840 --> 00:03:07,260
Now let's go ahead and let's set up the appropriate Iby actions that we need.

52
00:03:07,460 --> 00:03:09,700
So click on the green view controller.

53
00:03:09,920 --> 00:03:14,720
Open up the assistant editor and make sure that you're on automatic and view controller.

54
00:03:14,720 --> 00:03:18,310
Now we probably could change this name here but I'm not going to.

55
00:03:18,620 --> 00:03:19,400
Just because.

56
00:03:19,400 --> 00:03:23,660
So let's go ahead and let's all right click and drag and create an IP outlet.

57
00:03:23,870 --> 00:03:27,760
Let's call it add name button.

58
00:03:27,780 --> 00:03:28,400
All right.

59
00:03:28,400 --> 00:03:32,020
And next let's go ahead and click on Add name visi.

60
00:03:32,150 --> 00:03:34,060
And it should switch over there we go.

61
00:03:34,310 --> 00:03:39,920
While we're here let's get rid of did receive memory warning and the commented code there we don't need

62
00:03:40,430 --> 00:03:42,830
this included get rid of that.

63
00:03:43,220 --> 00:03:44,320
Ok cool.

64
00:03:44,630 --> 00:03:46,190
And now let's go ahead and let's.

65
00:03:46,190 --> 00:03:55,670
Right click and drag from this text field and call this new name textfield and connect it then right

66
00:03:55,670 --> 00:04:00,450
click and drag from the button and call this submit button.

67
00:04:00,720 --> 00:04:03,810
Like so go ahead and get rid of that space.

68
00:04:03,830 --> 00:04:11,540
And now we're going to go ahead and set up our other view controller this green one here to present

69
00:04:11,750 --> 00:04:12,920
this blue view controller.

70
00:04:12,920 --> 00:04:18,380
So let's go ahead and let's head into view controller and we're going to create a new function called

71
00:04:18,440 --> 00:04:20,400
phunk bind.

72
00:04:20,780 --> 00:04:29,170
Add name button OK and this is what we're going to do to bind our ADD name button to our view controller.

73
00:04:29,180 --> 00:04:31,920
So go ahead and call add name button.

74
00:04:31,920 --> 00:04:38,280
OK that's the name of our X and since it's a button we can access dot tap k.

75
00:04:38,600 --> 00:04:40,870
So whenever tap is called that's what's going to happen.

76
00:04:40,880 --> 00:04:48,280
Now I want to make sure that I don't accidentally instantiate multiple copies of view of a controller.

77
00:04:48,320 --> 00:04:52,760
Let's say that I tap the button super fast and was somehow able to present four or five instances of

78
00:04:52,760 --> 00:04:59,540
a view controller that would not be good with Eric Swift we can call a function called Dot throttle.

79
00:04:59,930 --> 00:05:00,670
And what that's going.

80
00:05:00,690 --> 00:05:06,500
Oops only Tootsie's throttle and what throttle is going to let us do is it's basically going to prevent

81
00:05:06,500 --> 00:05:08,960
us from putting more input in than we want.

82
00:05:08,960 --> 00:05:15,200
So to call throttle we're going to go ahead and put some parentheses here and we can give it a due time

83
00:05:15,290 --> 00:05:16,090
and a schedule or not.

84
00:05:16,090 --> 00:05:17,600
I've already shown you this.

85
00:05:17,600 --> 00:05:19,410
It's similar to the bounce.

86
00:05:19,670 --> 00:05:25,550
But what it's going to do is it's basically going to ignore input that is duplicate within a certain

87
00:05:25,550 --> 00:05:26,120
time frame.

88
00:05:26,130 --> 00:05:29,000
So 0.5 seconds is plenty of time.

89
00:05:29,000 --> 00:05:34,160
That means if I push the button once it's going to lock it out and not pay attention to any other input

90
00:05:34,190 --> 00:05:36,560
until after point five seconds.

91
00:05:36,560 --> 00:05:41,430
Now the scheduler like I always like I showed you is main schedular instance.

92
00:05:41,450 --> 00:05:46,790
OK it's a singleton class so we need to call the instance at the end and main scheduler is basically

93
00:05:46,790 --> 00:05:48,650
just the main dispatch queue.

94
00:05:48,680 --> 00:05:50,300
It's just a handy way of using it.

95
00:05:50,300 --> 00:05:56,270
So now we are throttling our input so we don't accidentally instantiate too many of you controllers.

96
00:05:56,540 --> 00:06:01,430
And now what we can do is call subscribe and that's where we're going to basically call what would happen

97
00:06:01,460 --> 00:06:02,520
in an IB action.

98
00:06:02,530 --> 00:06:09,090
K whatever happens when the button is pressed so called don't subscribe subscribe.

99
00:06:09,160 --> 00:06:14,930
And on next is what we're going to call and inside of the next closure we're going to go ahead and create

100
00:06:14,930 --> 00:06:17,080
an instance of ADD baby seat.

101
00:06:17,120 --> 00:06:27,260
So go ahead and type guard let add name Visi equals storyboard instantiate controller with identifier.

102
00:06:27,260 --> 00:06:31,520
This is how we're going to use the View Controller to create an instance of a particular view controller.

103
00:06:31,520 --> 00:06:39,380
So go ahead and give some quotes there just single quotes and we're going to call it ad name the C because

104
00:06:39,380 --> 00:06:41,150
that's the idea that we just set.

105
00:06:41,240 --> 00:06:42,410
So very cool.

106
00:06:42,420 --> 00:06:47,300
It's yelling at us of course because we're a closure so we're going to go ahead and call self-taught

107
00:06:47,310 --> 00:06:53,710
storyboard because we're in a closure we need to keep a reference to self and the inside of a guard.

108
00:06:53,750 --> 00:06:58,960
We need to create an additional condition in case we can't instantiate this properly.

109
00:06:59,030 --> 00:07:01,430
So instead of crashing we're gonna go ahead and just call.

110
00:07:01,460 --> 00:07:02,510
Else.

111
00:07:02,510 --> 00:07:09,950
And if we can't properly instantiate that we're just going to go ahead and print could not create and

112
00:07:10,160 --> 00:07:11,970
name visi.

113
00:07:12,030 --> 00:07:12,860
All right.

114
00:07:12,860 --> 00:07:16,490
So that's what will happen if we can't instantiate it but we will be able to.

115
00:07:16,490 --> 00:07:20,190
So don't worry now that we have that initialization.

116
00:07:20,300 --> 00:07:21,710
Here's what we can do.

117
00:07:21,800 --> 00:07:25,280
We can create a publish subject do you member publish subjects.

118
00:07:25,340 --> 00:07:32,060
They basically allow us to subscribe and then put in elements and the subscription is going to pay attention

119
00:07:32,060 --> 00:07:35,200
to anything that is added after the subscription.

120
00:07:35,210 --> 00:07:42,380
So in order to actually do this we need to go into ad Daim Visi and create an instance of publish subject.

121
00:07:42,380 --> 00:07:46,030
So I'm going to go ahead and do that beneath these Iby outlets.

122
00:07:46,040 --> 00:07:52,940
I'm going to go ahead and call Let name subject and that's going to be equal to published subject.

123
00:07:53,090 --> 00:08:00,060
Although we have not yet imported the proper frameworks so go ahead and import our acts swift and import.

124
00:08:00,070 --> 00:08:00,400
Whoa.

125
00:08:00,410 --> 00:08:01,310
Hello.

126
00:08:01,700 --> 00:08:03,630
Import our cocoa.

127
00:08:03,890 --> 00:08:06,650
So now we should have access to publish subject.

128
00:08:06,650 --> 00:08:11,720
There it is now of course we're dealing with strings because we are dealing with you know text fields

129
00:08:11,720 --> 00:08:15,590
and labels so this is going to be a published subject of type string.

130
00:08:15,800 --> 00:08:18,430
And we're going to instantiate it from the beginning.

131
00:08:18,650 --> 00:08:23,960
So we're also going to need a Dispose bag because later we're going to subscribe to our submit button

132
00:08:23,990 --> 00:08:25,880
and we need to be able to deallocate the memory.

133
00:08:25,880 --> 00:08:32,620
So go ahead and call Let dispose bag equals dispose bag and instantiate it.

134
00:08:32,620 --> 00:08:38,180
Now we don't need to do anything just yet but we do need to be able to access this name subject because

135
00:08:38,450 --> 00:08:45,530
when we instantiate the view controller we're basically going to link this publish subject to our array

136
00:08:45,690 --> 00:08:47,420
and our other view controller.

137
00:08:47,420 --> 00:08:51,890
Whenever we push in a name from the other view controller it's going to go ahead and push in the new

138
00:08:51,890 --> 00:08:54,050
name with an on next call.

139
00:08:54,050 --> 00:08:55,490
So here's what we're going to do.

140
00:08:55,490 --> 00:08:58,220
We've created an instance of ADD named visi.

141
00:08:58,220 --> 00:09:03,380
Now it's yelling at us and let's figure out why it says guard body may not fall through.

142
00:09:03,380 --> 00:09:05,560
Consider using a return or throw.

143
00:09:05,580 --> 00:09:07,270
Oh that's right I sorry.

144
00:09:07,400 --> 00:09:10,090
So we need to return or break.

145
00:09:10,160 --> 00:09:16,070
And so what we can actually do is we can call fatal error and fatal error has a string property and

146
00:09:16,070 --> 00:09:20,890
basically fatal error is going to return and print an error message at the same time.

147
00:09:20,920 --> 00:09:23,210
OK that's a good way for error handling.

148
00:09:23,210 --> 00:09:28,670
So now what we're going to do in our view controller is we're going to go ahead and access our names

149
00:09:28,670 --> 00:09:29,120
subject.

150
00:09:29,120 --> 00:09:36,310
So now we need to do is access add named Visi like so and try to access our names subject.

151
00:09:36,430 --> 00:09:38,360
But we can't and you know why.

152
00:09:38,510 --> 00:09:43,960
Because we have not actually set up our instance of the controller here to be of type add named visi.

153
00:09:43,960 --> 00:09:44,730
Check it out.

154
00:09:44,750 --> 00:09:51,580
This just returns a view controller so we need to cast this optionally as ADD named visi.

155
00:09:51,590 --> 00:10:00,020
So now if I go in to add named Visi I can push Dot and I can get access to our name subject or I should

156
00:10:00,020 --> 00:10:01,770
be able to maybe I need to build.

157
00:10:01,850 --> 00:10:03,170
Let's try that again.

158
00:10:03,170 --> 00:10:03,560
There we go.

159
00:10:03,560 --> 00:10:03,840
OK.

160
00:10:03,860 --> 00:10:08,710
Now name subject is showing up so if you don't see it just build and that should solve that problem.

161
00:10:08,810 --> 00:10:12,350
So add named Visi that name subject.

162
00:10:12,350 --> 00:10:13,630
And here's what we're going to do.

163
00:10:13,640 --> 00:10:15,490
We're going to go ahead and subscribe to it.

164
00:10:15,500 --> 00:10:22,010
And then whenever a value is added into it we can append that to our array in this view controller.

165
00:10:22,010 --> 00:10:26,330
So go ahead and call subscribe and we're going to call on next.

166
00:10:26,360 --> 00:10:32,990
As you may have expected and whenever a value is passed into this name subject we're going to call self

167
00:10:33,710 --> 00:10:34,900
names array.

168
00:10:35,120 --> 00:10:39,860
OK remember that's our ray that is presenting all the names at the bottom of the view controller self

169
00:10:40,190 --> 00:10:49,900
name xray dot value k we need to access the array and we need to append something from this subject

170
00:10:49,910 --> 00:10:51,450
OK from our published subject.

171
00:10:51,500 --> 00:10:57,240
But we currently don't have a way to access whatever is returned in this closure.

172
00:10:57,320 --> 00:11:05,390
So to do that after the bracket go ahead and call name in OK we've just set up a property.

173
00:11:05,420 --> 00:11:11,450
So whatever value gets passed in at Next we can access it using this variable name.

174
00:11:11,480 --> 00:11:14,170
So we're going to pass in name.

175
00:11:14,590 --> 00:11:15,670
Okay that makes sense.

176
00:11:15,680 --> 00:11:17,150
Our array just got a new name.

177
00:11:17,150 --> 00:11:19,750
Now if this is confusing to you it's OK.

178
00:11:19,820 --> 00:11:24,020
We're going to see how it works and then you'll understand it'll click I promise.

179
00:11:24,020 --> 00:11:25,300
So it's giving me an error.

180
00:11:25,310 --> 00:11:27,800
That's because we have built it will go away.

181
00:11:27,800 --> 00:11:28,280
There we go.

182
00:11:28,280 --> 00:11:28,790
Very cool.

183
00:11:28,850 --> 00:11:35,270
So what we are doing here is we are setting up this view controller to be subscribing to the names subject

184
00:11:35,600 --> 00:11:37,080
in AD named visi.

185
00:11:37,160 --> 00:11:43,490
Then whenever we push something in this value is going to be added to our names array on the previous

186
00:11:43,680 --> 00:11:49,200
controller so we can subscribe on different view controllers it works that way it's very very cool.

187
00:11:49,290 --> 00:11:54,820
Then next after we push add we want to go ahead and call add name Visi dismiss.

188
00:11:54,970 --> 00:11:58,440
OK we want it to go away to show our view controller beneath it.

189
00:11:58,470 --> 00:11:59,220
So go ahead and call.

190
00:11:59,220 --> 00:12:02,750
True and completion will be nil.

191
00:12:02,760 --> 00:12:03,450
All right.

192
00:12:03,540 --> 00:12:06,050
Now it's literally that easy to pass in the values.

193
00:12:06,060 --> 00:12:10,190
But of course we need to call add to dispose bag.

194
00:12:10,440 --> 00:12:12,690
And so we'll do that like so.

195
00:12:12,690 --> 00:12:19,180
And look at that we are one step closer to actually passing values between the view controllers here.

196
00:12:19,200 --> 00:12:21,960
Now I'm just sort of doing some cleanup here to make it look nice.

197
00:12:22,050 --> 00:12:24,590
And now now that we have subscribed.

198
00:12:24,630 --> 00:12:29,180
Oh sorry we're inclosures we need to call self-taught dispose back.

199
00:12:29,280 --> 00:12:35,610
Now that we have successfully subscribed so that when we pass in values we're going to go ahead and

200
00:12:35,670 --> 00:12:40,710
present this view controller because we have our subscription all set up so go ahead and just call self

201
00:12:41,300 --> 00:12:42,590
that present.

202
00:12:42,960 --> 00:12:48,510
And for the view controller to present it's going to be at named Visi animated of course is true and

203
00:12:48,510 --> 00:12:50,580
the completion is nil.

204
00:12:50,580 --> 00:12:54,630
Basically what we're going to do is when we tap the button we're going to throttle the input by point

205
00:12:54,630 --> 00:12:55,640
five seconds.

206
00:12:55,650 --> 00:13:01,380
We create an instance of ADD named Visi and we set up the name subject to have a subscription on this

207
00:13:01,380 --> 00:13:08,100
green view controller so that whenever a value is passed in we append it to the array in the green view

208
00:13:08,100 --> 00:13:10,490
controller then we dismiss the controller.

209
00:13:10,590 --> 00:13:11,070
OK.

210
00:13:11,220 --> 00:13:16,650
But since that won't happen from the get go we added to the Dispose bag at the very end of course and

211
00:13:16,650 --> 00:13:19,020
then we actually present this instance.

212
00:13:19,020 --> 00:13:24,140
So basically this is like a function that is set up whenever we pass in a value to the name subject.

213
00:13:24,150 --> 00:13:29,160
But when the controller first presents there's no values we haven't tapped the button to push in a value

214
00:13:29,160 --> 00:13:29,910
yet.

215
00:13:29,910 --> 00:13:35,670
So we have now set it up to present the proper view controller and dismiss it when we're ready.

216
00:13:35,670 --> 00:13:36,480
Very very cool.

217
00:13:36,480 --> 00:13:40,860
So of course we need to call bind add name function to view did load.

218
00:13:40,860 --> 00:13:42,720
So go ahead and call that.

219
00:13:42,720 --> 00:13:43,310
There we go.

220
00:13:43,320 --> 00:13:49,410
Bind add named function and now the last thing we need to do is to set up our submit button on the blue

221
00:13:49,410 --> 00:13:52,460
view controller which if you remember looks like this.

222
00:13:52,650 --> 00:13:58,530
We're going to set up this button here so that when we push it we push in a value to our name subject

223
00:13:58,530 --> 00:14:03,150
and therefore add a value to the array and dismiss the blue view controller.

224
00:14:03,150 --> 00:14:06,320
So go ahead an inside add named visi.

225
00:14:06,510 --> 00:14:14,400
We're going to create a function down here called phunk bind submit button and here we go.

226
00:14:14,400 --> 00:14:15,860
Let's go ahead and set this up.

227
00:14:15,870 --> 00:14:18,240
So go ahead and call submit button.

228
00:14:18,300 --> 00:14:23,670
Our X-Com tap and we're going to subscribe to it meaning whenever we tap it we're going to do something.

229
00:14:23,670 --> 00:14:29,020
So go ahead and call subscribe on next.

230
00:14:29,260 --> 00:14:36,450
And in the brackets of the closure we're going to go ahead and call self name subject on next and we

231
00:14:36,450 --> 00:14:37,610
can pass in a value.

232
00:14:37,620 --> 00:14:40,000
Now where are we going to get that value.

233
00:14:40,170 --> 00:14:41,050
The textfield.

234
00:14:41,220 --> 00:14:44,230
So let's go ahead and let's do that self.

235
00:14:44,250 --> 00:14:47,540
Got any new name textfield text.

236
00:14:47,580 --> 00:14:51,940
And of course for some Rapide because it's from an optional string of textfield.

237
00:14:52,380 --> 00:14:53,520
And that's it.

238
00:14:53,520 --> 00:14:54,390
We push the button.

239
00:14:54,390 --> 00:14:55,750
The subscription happens.

240
00:14:55,910 --> 00:15:00,630
We pass in the value from the textfield the value gets added to the array.

241
00:15:00,780 --> 00:15:05,160
The view controller gets dismissed and it will show up on the previous view controller we're talking

242
00:15:05,430 --> 00:15:08,340
between New controllers thinks to subjects.

243
00:15:08,340 --> 00:15:09,670
Very very cool.

244
00:15:09,670 --> 00:15:12,720
Now I just realized there could be an issue.

245
00:15:12,810 --> 00:15:15,350
We will basically be able to pass in blank names.

246
00:15:15,360 --> 00:15:25,800
We don't want that so we can say if self dot new name textfield text is not equal to an empty string

247
00:15:26,400 --> 00:15:29,170
whoops we can go ahead and call on next.

248
00:15:29,170 --> 00:15:32,240
So that'll prevent us from actually passing in a blank value.

249
00:15:32,430 --> 00:15:36,110
But you know what guys we are really really close.

250
00:15:36,300 --> 00:15:41,280
Oh sorry I forgot we need to go ahead and call add disposable to and add that to our dispose bag and

251
00:15:41,280 --> 00:15:42,460
told you we'd use it.

252
00:15:42,750 --> 00:15:44,770
Anyway let's go ahead and clean this up.

253
00:15:44,790 --> 00:15:51,000
We're going to go ahead and move this in a little ways like so and we're going to call it from view

254
00:15:51,000 --> 00:15:51,550
to load.

255
00:15:51,570 --> 00:15:53,360
Bind submit button.

256
00:15:53,670 --> 00:15:54,810
Let's go see if it worked.

257
00:15:54,820 --> 00:15:56,460
So build and run.

258
00:15:56,460 --> 00:16:00,600
Open up your simulator and I'm really excited to see if this works.

259
00:16:00,600 --> 00:16:06,990
Guys if this is working the way that we think it is this is very similar to passing values using a protocol

260
00:16:07,320 --> 00:16:11,430
or passing values using a Segway.

261
00:16:11,430 --> 00:16:16,230
So let's go ahead let's pasand some names the way that we have been Kalib submit.

262
00:16:16,380 --> 00:16:16,800
Nice.

263
00:16:16,800 --> 00:16:18,240
How about Bob.

264
00:16:18,510 --> 00:16:18,790
Cool.

265
00:16:18,810 --> 00:16:19,050
OK.

266
00:16:19,050 --> 00:16:20,530
So let's go ahead and let's click this.

267
00:16:20,520 --> 00:16:22,200
It should present the view controller.

268
00:16:22,390 --> 00:16:22,590
OK.

269
00:16:22,590 --> 00:16:24,150
Just like we expected.

270
00:16:24,180 --> 00:16:33,690
And now if I type in a name like I don't know Amy and if I press submit what it should do is it should

271
00:16:33,690 --> 00:16:34,870
pass in value.

272
00:16:35,250 --> 00:16:40,110
And because of that let's go back to our main view controller because we passed a value on.

273
00:16:40,110 --> 00:16:46,650
Next we're going to take that name property pass into our names or re dismiss ad name Visi which is

274
00:16:46,650 --> 00:16:47,360
this one.

275
00:16:47,640 --> 00:16:54,450
And remember if we append a value to names or say how that's working is basically the names array is

276
00:16:54,630 --> 00:16:56,400
being joined with a separator.

277
00:16:56,400 --> 00:17:03,910
So when I press it watch what happens nothing because we forgot something very very important names

278
00:17:03,930 --> 00:17:05,010
array is a variable.

279
00:17:05,010 --> 00:17:07,030
We're expecting it to act like an observable.

280
00:17:07,060 --> 00:17:11,570
But if you remember a variable in and of itself is not an observable.

281
00:17:11,640 --> 00:17:16,020
It's like an observable but it can be used like a regular variable.

282
00:17:16,050 --> 00:17:19,830
We need to cast it as an observable in order for it to actually work the way we want.

283
00:17:19,830 --> 00:17:22,670
So inside you did load We're going to do that.

284
00:17:22,680 --> 00:17:24,490
We're going to call names right.

285
00:17:24,780 --> 00:17:26,460
Which is of type variable.

286
00:17:26,490 --> 00:17:29,040
We're going to call datt as observable.

287
00:17:29,040 --> 00:17:32,130
And now it's an observable we can subscribe to.

288
00:17:32,190 --> 00:17:36,930
We weren't actually successfully subscribing to the changes being passed into name array.

289
00:17:36,930 --> 00:17:43,740
So go ahead and call subscribe now that we can and we're going to call on next like you may have expected.

290
00:17:44,310 --> 00:17:51,660
And inside of Subscribe we're going to go ahead and set up the names label to be whatever gets passed

291
00:17:51,660 --> 00:17:53,380
in when we update names.

292
00:17:53,390 --> 00:18:00,000
Right now we do that in AD name Visi because we call on next we pass in value to the name subject and

293
00:18:00,000 --> 00:18:04,970
check it out when we are in the view controller here when we call on next for the name subject.

294
00:18:04,980 --> 00:18:08,810
We set the value here to append whatever name is passed in.

295
00:18:08,970 --> 00:18:15,390
So what we need to do is we need to basically set it up so that the values returned are actually set

296
00:18:15,390 --> 00:18:18,120
up to show in the textfield which we didn't do yet.

297
00:18:18,120 --> 00:18:23,100
Now we're going to go ahead and set up a property here for the closure and we're just going to say names

298
00:18:24,150 --> 00:18:28,310
in and in this just to denote that it's a closure.

299
00:18:28,320 --> 00:18:33,670
Names is a property that is going to hold the value of whatever was passed in when we subscribed.

300
00:18:33,840 --> 00:18:41,460
So go ahead and call self names label text and we're going to set this to be equal to names which comes

301
00:18:41,460 --> 00:18:48,630
in as an array of string and we can call DOT joined with separator comma and a space.

302
00:18:48,870 --> 00:18:56,190
And now this array is basically going to take the values that we had in the array but we are now able

303
00:18:56,190 --> 00:18:57,580
to pass them in.

304
00:18:57,630 --> 00:19:00,220
Thanks to our observable sequence.

305
00:19:00,220 --> 00:19:02,610
OK so let's go ahead and build and run this.

306
00:19:02,730 --> 00:19:05,090
Let's go check to see if that worked OK.

307
00:19:05,440 --> 00:19:11,260
You know what we're not disposing at though let's go ahead and call our disposable to dispose bag.

308
00:19:11,340 --> 00:19:14,460
It's not mandatory but it definitely can cause memory issues.

309
00:19:14,460 --> 00:19:15,900
If you don't do it.

310
00:19:15,900 --> 00:19:18,020
So let's go ahead and let's extend this a little bit.

311
00:19:18,030 --> 00:19:19,610
Let's build and run it again.

312
00:19:19,680 --> 00:19:25,080
Now that we're properly disposing and we'll see if this updates and works the way we expect.

313
00:19:25,080 --> 00:19:33,530
So here we go let's add in names the normal way Cayla we go Bob Jane.

314
00:19:34,020 --> 00:19:35,930
And now let's click add name.

315
00:19:36,150 --> 00:19:42,180
Let's go ahead and type in Chris and watch what happens when I click submit.

316
00:19:42,180 --> 00:19:48,660
It adds to the bottom view controller because we are setting the label to be equal to the names array

317
00:19:48,960 --> 00:19:51,520
passed in from our names here.

318
00:19:51,540 --> 00:19:54,420
Now we are subscribing to names IRA.

319
00:19:54,510 --> 00:20:00,020
That means whenever something is pushed into names array we get an array back.

320
00:20:00,030 --> 00:20:00,610
OK.

321
00:20:00,750 --> 00:20:03,630
And we can set our label to be equal to that.

322
00:20:03,630 --> 00:20:05,550
Names are joined with the separator.

323
00:20:05,550 --> 00:20:10,290
Now this is only because we are taking it from another view controller check it out.

324
00:20:10,290 --> 00:20:17,430
Down here we create an instance of ADD named VC and the name subject is where we are pushing in names

325
00:20:17,520 --> 00:20:19,110
from that other view controller.

326
00:20:19,110 --> 00:20:26,590
But when we push in a name right we take that singular name and we add it to our names array.

327
00:20:26,880 --> 00:20:35,220
Right now that is why up here this is this is why without this it didn't work because the values pushed

328
00:20:35,220 --> 00:20:40,110
in here are only reflected because we are subscribing to this names array.

329
00:20:40,110 --> 00:20:46,530
Otherwise our submit button here with the label it doesn't know how to interact with that because here

330
00:20:46,740 --> 00:20:52,390
when we push the submit button on the green view controller we are passing in the value just like that.

331
00:20:52,410 --> 00:20:55,740
This basically is what connected the two of you controllers.

332
00:20:55,740 --> 00:20:57,660
We created this as an observable.

333
00:20:57,840 --> 00:21:02,690
And whenever we push in a new value we're setting up the names label to include the newest data.

334
00:21:02,700 --> 00:21:06,840
That's what makes our swift super cool is you can do things like this that you would normally have to

335
00:21:06,840 --> 00:21:08,920
write a bunch of boilerplate code.

336
00:21:09,090 --> 00:21:15,880
But now I can just type in John and add him there or I could do another one Billy and add him there.

337
00:21:15,990 --> 00:21:16,950
OK.

338
00:21:16,950 --> 00:21:20,650
We're doing the same thing with a single asynchronous stream of data.

339
00:21:20,730 --> 00:21:21,810
So so cool.

340
00:21:21,810 --> 00:21:27,240
So guys this app is now finished and then the next section we're going to move on to our capstone project

341
00:21:27,300 --> 00:21:31,590
where we're going to build versey a get hub client using our swift.

342
00:21:31,590 --> 00:21:32,660
Very very cool.

343
00:21:32,670 --> 00:21:34,020
Let's get over to that section now.

344
00:21:34,020 --> 00:21:36,370
Great job with this one guys.
