-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy path2. MVC.srt
6585 lines (5268 loc) · 177 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
本字幕由志愿者义务贡献,采用许可协议
知识共享 署名-非商业性使用-相同方式共享 3.0 美国
2
00:00:04,705 --> 00:00:10,375
Stanford University. >> Welcome to Stanford CS193P,
欢迎参加斯坦福 CS193P 课程
3
00:00:10,444 --> 00:00:15,013
Developing Applications for iOS. This is Lecture Number 2,
iOS 应用程序开发。今天是 2017 年秋季学期
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,
二十分钟的时间讲 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
在开发 iOS 时使用这个设计模式。然后我们把
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.
的方法。一个是这个蓝色的模型(Model)
14
00:00:52,319 --> 00:00:56,521
That's a UI independent set of objects that is
这是不依赖于 UI 的一系列对象,这代表了
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.
那个就属于控制器(Controller)的职责了
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.
然后视图(View)阵营就是控制器的下属
27
00:01:36,063 --> 00:01:41,333
These are very generic UI elements, like UI button,
它们是通用的 UI 组件,比如 UIButton
28
00:01:41,402 --> 00:01:45,104
the UI view controller even UI label.
甚至 UIViewController,还有 UILabel
29
00:01:45,172 --> 00:01:48,173
All those kind of generic things, UI things,
所有这些通用的 UI 组件
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
这就是模型-视图-控制器三个阵营,简称 MVC
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
就是我们的出口(outlet),我们有一个到
57
00:03:17,230 --> 00:03:19,964
count label, and of course we can talk to the flip count
flipCountLabel 的出口,当然我们可以告诉 flipCountLabel
58
00:03:20,033 --> 00:03:23,067
label and say anything we want to it to get it to say what we
说我们想要让它在 UI 上显示
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
其一,模型与 UI 是无关的,而视图
66
00:03:45,058 --> 00:03:47,793
has UI things in it, so there's absolutely no way a UI
只负责显示 UI。所以一个无关 UI 的阵营
67
00:03:47,861 --> 00:03:51,496
independent thing could talk to such UI dependent things
不可能去访问这个只管 UI
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 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
UIButton 不知道有关翻牌
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.
标准化的匿名通讯,那就是 target action
93
00:05:06,540 --> 00:05:09,607
When we Ctrl dragged and created the method touchCard.
当我们按住 control 拖拽,创建了一个方法 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
一个 touchCard 这样的方法,然后 UIButton
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
比如你有更复杂的通用 UI 组件
103
00:05:35,936 --> 00:05:39,103
item like, let's say a scroll view. A scroll view is
比如一个 scroll view,滚动视图能够
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
属于 delegate,代理的一部分。代理就是一个变量
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.
都会以 will, should 或 did 开头
116
00:06:18,778 --> 00:06:21,979
Like, I will scroll to here, should I scroll over here? I
比如我将要(will)滚动到这里,是否能够(should)滚动到那里?
117
00:06:22,048 --> 00:06:26,585
did scroll down to here. Those are classic delegate methods.
我已经(did)滚动到这里了,这些就是典型的代理的方法
118
00:06:26,653 --> 00:06:29,687
And the Controller, using a mechanism called protocols,
然后控制器通过 protocol,协议
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.
我实现了这些 will,should,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
开始使用更加复杂的 UI 对象的时候。另外一个重要的是
129
00:06:56,283 --> 00:06:58,717
to remember in the MVC model is that views,
记住在 MVC 模式里,视图
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
让我们现象一下你展示整个 iPod 音乐库
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
比如列表视图(table view),就是个很长的滚动列表
145
00:07:47,901 --> 00:07:51,269
kind of generic view item, that's in iOS.
这个是 iOS 提供的通用视图组件
146
00:07:51,338 --> 00:07:53,671
When it's scrolling around on all your iPad music things,
当你在来回滑动查看 iPad 音乐库内容的时候
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
可能是很快的 SQL 数据库一类的
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
也是不知道这些数据来源,不知道是不是 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
我们称这种代理为数据源(data source)
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
UI 组件,并不是固定的,取决于
163
00:08:36,182 --> 00:08:39,917
going on in that UI element. So that's the kind of
那个 UI 组件是干什么的。所以这就是
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
我们说控制器在 MVC 里的作用
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
肯定不是直接的,因为模型是与 UI
176
00:09:19,692 --> 00:09:23,094
independent and the controller is fundamentally UI dependant,
无关的,而控制器根本上讲是取决于 UI 的
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
好让任何其他对此感兴趣的 UI
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
之类的。所以它用的是类似于电台的机制。iOS 里
188
00:09:57,230 --> 00:10:01,399
it's called notifications, or KVO, Key Value Observing, and
这叫做通知(notification),或者键值监听(KVO,Key Value Observing)
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 radio station like another UI thing, cuz
控制器的电台,或者某种 UI 相关的东西
194
00:10:19,986 --> 00:10:23,187
a view is fundamental of UI, but even that's pretty rare.
因为视图是 UI 的基础,但即使有的话也是很罕见
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
这类的。这个 MVC,这个模型,视图,控制器的集合
198
00:10:35,168 --> 00:10:39,437
generally only used to control one screen on the iPhone, or
一般只会用来控制 iPhone 上的一个界面
199
00:10:39,506 --> 00:10:42,941
on the iPad. Maybe it can control little sub-place,
或者 iPad 上的一个界面。可能可以控制某个附属的界面
200
00:10:43,009 --> 00:10:46,344
like maybe on an iPad, maybe one MVC controls one space,
比如像在 iPad 上,可能一个 MVC 控制一个界面