-
Notifications
You must be signed in to change notification settings - Fork 149
/
2. MVC.srt
6549 lines (5232 loc) · 135 KB
/
2. MVC.srt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1
00:00:00,401 --> 00:00:04,636
[MUSIC]
2
00:00:04,705 --> 00:00:10,375
Stanford University.
Welcome to Stanford CS193P,
3
00:00:10,444 --> 00:00:15,013
Developing Applications for
iOS. This is Lecture Number 2,
4
00:00:15,081 --> 00:00:19,451
fall of 2017. So today, I'm
gonna spend the first 15 or
5
00:00:19,520 --> 00:00:22,053
20 minutes talking about
Model-View-Controller,
6
00:00:22,122 --> 00:00:25,790
this design paradigm that I
told you we always have to do
7
00:00:25,859 --> 00:00:29,595
when we develop for iOS.
>> And then, we're gonna apply
8
00:00:29,663 --> 00:00:32,964
Model-View-Controller to
our Concentration app, so
9
00:00:33,033 --> 00:00:35,734
you're gonna get to see
the kinda the concepts first,
10
00:00:35,802 --> 00:00:38,903
then we get to see it
in action. All right,
11
00:00:38,972 --> 00:00:42,841
Model-View-Controller, what
is it? It's essentially a way
12
00:00:42,909 --> 00:00:46,444
we're gonna divide up all
the objects in our system into
13
00:00:46,513 --> 00:00:52,250
three camps. one camp, this
blue camp, is the Model camp.
14
00:00:52,319 --> 00:00:56,521
That's a UI independent
set of objects that is
15
00:00:56,589 --> 00:01:00,959
the what of your app? So for
our Concentration game, it's
16
00:01:01,027 --> 00:01:03,996
the part of our app that knows
how to play Concentration.
17
00:01:04,064 --> 00:01:06,164
It knows how to match cards,
take them away,
18
00:01:06,233 --> 00:01:09,033
it knows when to flip cards,
it knows all that stuff.
19
00:01:09,102 --> 00:01:11,803
It's all the kind of knowledge
about the Concentration
20
00:01:11,872 --> 00:01:15,807
game but nothing about how
it appears on screen. That
21
00:01:15,876 --> 00:01:19,311
is all part of the Controller
camp's responsibility.
22
00:01:19,379 --> 00:01:21,747
So the Controller
camp is the how
23
00:01:23,083 --> 00:01:25,984
your Concentration
game appears on screen.
24
00:01:26,053 --> 00:01:28,587
So Model is what is
your app about, and
25
00:01:28,655 --> 00:01:31,923
the Controller is how
it shows up on screen.
26
00:01:31,992 --> 00:01:35,994
And the View camp is your
Controller's minions.
27
00:01:36,063 --> 00:01:41,333
These are very generic UI
elements, like UI button,
28
00:01:41,402 --> 00:01:45,104
the UI view controller
even UI label.
29
00:01:45,172 --> 00:01:48,173
All those kind of generic
things, UI things,
30
00:01:48,241 --> 00:01:52,644
that the Controller has to
communicate with the Model to
31
00:01:52,712 --> 00:01:58,149
get your game, whatever is
going on your app onto the UI.
32
00:01:58,218 --> 00:02:02,086
So the View is generic
minions of the Controller. So
33
00:02:02,155 --> 00:02:06,524
those are the three camps.
Now, MVC is really all about
34
00:02:06,593 --> 00:02:09,294
managing the communication
between these camps, right?
35
00:02:09,363 --> 00:02:10,628
You put the objects
in these camps, and
36
00:02:10,697 --> 00:02:13,598
they have to kind of obey
certain rules when they talk
37
00:02:13,667 --> 00:02:16,501
to each other. So,
I've drawn road signs up here.
38
00:02:16,570 --> 00:02:19,871
You see the little road signs
that are kind of approximating
39
00:02:19,940 --> 00:02:22,908
what kind of communication is
allowed between the various
40
00:02:22,977 --> 00:02:25,943
camps. And let's look
at it all in detail. So
41
00:02:26,012 --> 00:02:28,079
the Controller,
talking to the Model,
42
00:02:28,148 --> 00:02:31,716
that's a fully dashed white
line going in that direction.
43
00:02:31,785 --> 00:02:35,254
You can cross over anytime
you want, big green arrow.
44
00:02:35,322 --> 00:02:38,022
The Controller can talk to
the Model all it wants.
45
00:02:38,091 --> 00:02:41,393
It has to be able to because
it is the Controller's job to
46
00:02:41,462 --> 00:02:45,563
present this what the thing is
to the user, so it has to be
47
00:02:45,632 --> 00:02:48,433
able to access the Model. So
that's a big old green arrow,
48
00:02:48,502 --> 00:02:51,803
and this is the Controller
talking to the Model.
49
00:02:51,871 --> 00:02:57,609
Pretty much unlimited
communication to the Model's
50
00:02:57,678 --> 00:03:00,913
publicly available
functionality. What about this
51
00:03:00,981 --> 00:03:03,815
direction? Well,
similarly here, its minions,
52
00:03:03,883 --> 00:03:06,584
the Controller has to be able
to control its minions, so
53
00:03:06,653 --> 00:03:09,554
it's a pretty much a wide open
green arrow that way as well,
54
00:03:09,623 --> 00:03:11,689
and you've already
seen a green arrow from
55
00:03:11,758 --> 00:03:14,225
the Controller to the View
in our Concentration.
56
00:03:14,294 --> 00:03:17,162
It was called an outlet.
We had an outlet to the flip
57
00:03:17,230 --> 00:03:19,964
count label, and of course we
can talk to the flip count
58
00:03:20,033 --> 00:03:23,067
label and say anything we want
to it to get it to say what we
59
00:03:23,136 --> 00:03:26,037
want in the UI. That is
the Controller's prerogative.
60
00:03:26,106 --> 00:03:28,673
So you can see the controller
can talk to everybody
61
00:03:28,742 --> 00:03:31,977
pretty much all at once. What
about some of the other kind
62
00:03:32,046 --> 00:03:34,612
of communication?
What about the Model talking
63
00:03:34,681 --> 00:03:39,250
directly to the View? Okay,
that's pretty much impossible.
64
00:03:39,319 --> 00:03:41,153
Why is that impossible?
Two reasons.
65
00:03:41,221 --> 00:03:44,989
One, the Model is UI
independent, and the View only
66
00:03:45,058 --> 00:03:47,793
has UI things in it, so
there's absolutely no way a UI
67
00:03:47,861 --> 00:03:51,496
independent thing could talk
to such UI dependent things
68
00:03:51,564 --> 00:03:54,866
like the View. Another reason
is that these View things
69
00:03:54,935 --> 00:03:58,603
are generic objects, like a
button or a slider. How could
70
00:03:58,672 --> 00:04:01,939
a button have any idea what
a Concentration game is about?
71
00:04:02,008 --> 00:04:04,075
No way. There's just no way it
would know that, it's generic.
72
00:04:04,144 --> 00:04:06,578
So if there's never any
communication between these
73
00:04:06,646 --> 00:04:09,113
two, I never wanna see you
having any communication
74
00:04:09,182 --> 00:04:11,516
between these camps in any of
your homeworks or whatever.
75
00:04:11,585 --> 00:04:13,318
That's why it's a double
yellow line there.
76
00:04:13,387 --> 00:04:16,755
No crossing over. All right,
that's an easy one. What about
77
00:04:16,823 --> 00:04:20,158
the View talking back to the
Controller? This is probably
78
00:04:20,226 --> 00:04:25,029
the most interesting of the
communication pathways here.
79
00:04:25,098 --> 00:04:28,099
The View can speak to its
Controller, of course it,
80
00:04:28,168 --> 00:04:31,602
it kind of has to, like when a
button is clicked or whatever,
81
00:04:31,671 --> 00:04:33,204
but when it communicates,
82
00:04:33,273 --> 00:04:36,608
this communication has to
be blind and structured. It
83
00:04:36,676 --> 00:04:40,145
has to be blind because these
are generic view objects.
84
00:04:40,213 --> 00:04:43,014
The UI Button doesn't know
anything about a Concentration
85
00:04:43,082 --> 00:04:46,050
game controller, so when it's
talking to the Controller,
86
00:04:46,119 --> 00:04:48,252
it doesn't really know that
it's a Concentration game
87
00:04:48,321 --> 00:04:50,488
controller and
it's structured in that
88
00:04:50,557 --> 00:04:52,857
since we're gonna have this
communication going on,
89
00:04:52,926 --> 00:04:56,294
a generic object has to think
a little bit ahead about how
90
00:04:56,363 --> 00:05:00,232
it might wanna communicate
with this Controller object.
91
00:05:00,300 --> 00:05:03,067
So, you already know one
structured, blind way for
92
00:05:03,136 --> 00:05:06,471
your View to communicate,
and that's target action.
93
00:05:06,540 --> 00:05:09,607
When we Ctrl dragged and
created the method touchCard.
94
00:05:09,676 --> 00:05:12,344
That's target action. And all
the Controller has to do is
95
00:05:12,412 --> 00:05:16,081
kinda hang a target on itself.
That's to say, it creates
96
00:05:16,150 --> 00:05:19,518
a method like touchCard.
And then UI button and
97
00:05:19,586 --> 00:05:21,453
other things,
they can get this action, and
98
00:05:21,521 --> 00:05:22,887
every time the button
is pressed,
99
00:05:22,956 --> 00:05:25,723
they just call the target.
This is a very,
100
00:05:25,792 --> 00:05:30,162
very simple kind of blind
structure communication. But
101
00:05:30,230 --> 00:05:33,165
sometimes you need more
complicated communication,
102
00:05:33,233 --> 00:05:35,867
like you have a more
complicated generic view
103
00:05:35,936 --> 00:05:39,103
item like, let's say a scroll
view. A scroll view is
104
00:05:39,172 --> 00:05:42,407
scrolling around on some image
or something like that, and
105
00:05:42,476 --> 00:05:44,409
it might need to
tell the Controller,
106
00:05:44,477 --> 00:05:48,513
I scrolled to the end. Am I
allowed to scroll down here?
107
00:05:48,582 --> 00:05:50,415
Can I scroll vertically or
horizontally?
108
00:05:50,483 --> 00:05:53,084
It kinda wants to talk
to Controller as it's
109
00:05:53,152 --> 00:05:57,255
working to do its job. And
we do that with these kind of
110
00:05:57,324 --> 00:06:00,992
predefined methods that the
scroll view defines as part of
111
00:06:01,061 --> 00:06:06,130
what's called its delegate. So
its delegate is just a var in
112
00:06:06,199 --> 00:06:11,036
scroll view that, and this var
will have some object in it.
113
00:06:11,104 --> 00:06:13,872
And all we know about this
object is that it responds to
114
00:06:13,941 --> 00:06:16,441
a certain number of messages.
Most of these messages
115
00:06:16,510 --> 00:06:18,710
start with the words will,
should, or did.
116
00:06:18,778 --> 00:06:21,979
Like, I will scroll to here,
should I scroll over here? I
117
00:06:22,048 --> 00:06:26,585
did scroll down to here. Those
are classic delegate methods.
118
00:06:26,653 --> 00:06:29,687
And the Controller, using
a mechanism called protocols,
119
00:06:29,756 --> 00:06:31,455
which we'll talk
about next week,
120
00:06:31,524 --> 00:06:33,458
is able to tell
the scroll view,
121
00:06:33,527 --> 00:06:36,528
I'm your delegate, and
all the scroll view will know
122
00:06:36,596 --> 00:06:38,562
is that it implements these
will, should, and did.
123
00:06:38,631 --> 00:06:40,966
It doesn't know anything about
it. It doesn't know its class,
124
00:06:41,034 --> 00:06:43,568
doesn't know that it's has to
do with Concentration game
125
00:06:43,637 --> 00:06:46,170
obviously, it knows nothing.
It just knows that
126
00:06:46,239 --> 00:06:48,506
the Controller will
implement that. So
127
00:06:48,575 --> 00:06:52,043
we'll see delegation in about
two weeks when we start using
128
00:06:52,111 --> 00:06:56,214
more complicated UI objects.
Now, another important thing
129
00:06:56,283 --> 00:06:58,717
to remember in the MVC
model is that views,
130
00:06:58,785 --> 00:07:00,085
these generic things,
131
00:07:00,153 --> 00:07:03,388
cannot own the data they're
displaying. In other words,
132
00:07:03,456 --> 00:07:06,157
they're not going to have
the data they're displaying as
133
00:07:06,225 --> 00:07:09,460
part of their instance
variables. Now why is this?
134
00:07:09,529 --> 00:07:13,064
Well, imagine that the View is
showing your entire iPod music
135
00:07:13,133 --> 00:07:17,268
library. And let's say you
have 50,000 songs in there.
136
00:07:17,337 --> 00:07:20,471
There's no way, it would make
absolutely no sense to have
137
00:07:20,540 --> 00:07:23,475
a list view or something,
some generic view that lists
138
00:07:23,543 --> 00:07:27,345
things, to bring all 50,000
of those things in there. So
139
00:07:27,413 --> 00:07:30,648
instead, it uses this same
kind of protocol mechanism to
140
00:07:30,717 --> 00:07:32,984
have another set of
special messages,
141
00:07:33,052 --> 00:07:36,353
and they are messages like
data, give me the data at, or
142
00:07:36,422 --> 00:07:39,891
how many items are there? And
the Controller implements that
143
00:07:39,960 --> 00:07:43,628
so we can talk to the Model
and get the data for the View.
144
00:07:43,697 --> 00:07:47,832
So, for example, table view,
which is a big scrolling list
145
00:07:47,901 --> 00:07:51,269
kind of generic view item,
that's in iOS.
146
00:07:51,338 --> 00:07:53,671
When it's scrolling around on
all your iPad music things,
147
00:07:53,740 --> 00:07:56,441
it's just asking for the ones
it's currently showing. Right,
148
00:07:56,510 --> 00:07:58,576
there's 50,000.
It's only scrolling around to
149
00:07:58,645 --> 00:08:00,978
showing maybe 10 at a time.
And so it's just asking
150
00:08:01,047 --> 00:08:02,446
the controller give
me the next ten,
151
00:08:02,515 --> 00:08:03,848
give me the ten here, and
152
00:08:03,917 --> 00:08:05,549
the controller turns
around to the model,
153
00:08:05,618 --> 00:08:08,586
which is probably a nice fast
SQL database or something, and
154
00:08:08,655 --> 00:08:11,489
grabbing the data, and handing
it off to the view. Okay, and
155
00:08:11,558 --> 00:08:14,259
this uses the same mechanism
where the table view again
156
00:08:14,327 --> 00:08:16,894
doesn't know anything about
this as being an iPod
157
00:08:16,963 --> 00:08:20,364
music app, it just knows that
it's the data provider, and
158
00:08:20,433 --> 00:08:24,202
we call this kind of delegate
the data source. All right, so
159
00:08:24,271 --> 00:08:26,637
you'll see that as well, and
both data source and delegate,
160
00:08:26,706 --> 00:08:29,207
very similar it's just kind of
a different set of methods,
161
00:08:29,276 --> 00:08:32,076
and these methods are of
course dependent on the kind
162
00:08:32,145 --> 00:08:36,114
of UI element, they're not a
preset list, depends on what's
163
00:08:36,182 --> 00:08:39,917
going on in that UI element.
So that's the kind of
164
00:08:39,986 --> 00:08:42,920
communication the view can
have with the controller,
165
00:08:42,989 --> 00:08:46,625
it's structured, it's kind of
predefined, things like that.
166
00:08:46,693 --> 00:08:49,894
Because of all this
communication going on in this
167
00:08:49,962 --> 00:08:53,164
direction we say the
controller's job in an MVC is
168
00:08:53,232 --> 00:08:56,500
to interpret and format
the models information for
169
00:08:56,569 --> 00:08:59,203
the view.
That's its primary purpose.
170
00:08:59,272 --> 00:09:01,239
It kind of also
goes the other way,
171
00:09:01,307 --> 00:09:04,742
It interprets user interaction
in the view for the model.
172
00:09:04,811 --> 00:09:06,344
It's the interpreter back and
forth.
173
00:09:06,413 --> 00:09:11,382
It's the center of all
communication here.
174
00:09:11,451 --> 00:09:14,585
What about the model? Can the
model talk to its controller?
175
00:09:14,654 --> 00:09:19,623
Obviously not directly.
Because the model is UI
176
00:09:19,692 --> 00:09:23,094
independent and the controller
is fundamentally UI dependant,
177
00:09:23,162 --> 00:09:25,830
so it can't do it directly.
But there is a mechanism for
178
00:09:25,899 --> 00:09:29,167
the model to communicate, for
example, if some data changes
179
00:09:29,235 --> 00:09:32,036
and it wants any UIs that
are interested out there
180
00:09:32,105 --> 00:09:36,007
to update. And the way it does
that is with a model that I
181
00:09:36,076 --> 00:09:39,277
call radio station model,
and the model essentially
182
00:09:39,346 --> 00:09:42,113
starts broadcasting on
a certain known radio station.
183
00:09:42,182 --> 00:09:46,117
And the controller up there
it's just going to tune in and
184
00:09:46,185 --> 00:09:49,186
when it hears oh something's
changed on the models radio
185
00:09:49,255 --> 00:09:51,689
station then it's gonna
use its big green arrow,
186
00:09:51,758 --> 00:09:54,025
to go talk to the model and
get the date of the change, or
187
00:09:54,093 --> 00:09:57,161
whatever. So this is a radio
station model. In iOS
188
00:09:57,230 --> 00:10:01,399
it's called notifications, or
KVO, Key Value Observing, and
189
00:10:01,468 --> 00:10:05,103
we'll talk about those in a
few weeks as well. So there is
190
00:10:05,172 --> 00:10:08,405
a way from the model to kind
of broadcast, wow things
191
00:10:08,474 --> 00:10:13,544
are changing. Now, some people
have asked can a view tune in
192
00:10:13,613 --> 00:10:16,748
to a radio station? A View
could only really tune into
193
00:10:16,816 --> 00:10:19,918
a controller or radio station
like another UI thing, cuz
194
00:10:19,986 --> 00:10:23,187
a view is fundamental of UI,
but even that's pretty rare.
195
00:10:23,256 --> 00:10:24,722
Usually the radio stations
are pretty much for
196
00:10:24,791 --> 00:10:27,892
the model to communicate hey
something's happening in your
197
00:10:27,961 --> 00:10:35,099
data or whatever. This, MVC,
a collection of MVC, is
198
00:10:35,168 --> 00:10:39,437
generally only used to control
one screen on the iPhone, or
199
00:10:39,506 --> 00:10:42,941
on the iPad. Maybe it can
control little sub-place,
200
00:10:43,009 --> 00:10:46,344
like maybe on an iPad, maybe
one MVC controls one space,
201
00:10:46,413 --> 00:10:49,046
and another MVC controls
another place, and