-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2. MVC; iOS, Xcode and Swift Demonstration.srt
8822 lines (7218 loc) · 238 KB
/
2. MVC; iOS, Xcode and Swift Demonstration.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,869
[MUSIC]
(音乐)
2
00:00:04,938 --> 00:00:07,906
Stanford University, >> So
斯坦福大学> >
斯坦福大学
3
00:00:07,974 --> 00:00:12,577
welcome to Stanford CS193P, this is Developing iOS
欢迎来到斯坦福CS193P,这是开发iOS
欢迎参加 2017 年冬季学期斯坦福 CS193P 课程
4
00:00:12,646 --> 00:00:17,115
Applications, winter of 2017, So today I'm gonna give
应用程序,2017年冬天,所以今天我要给
iOS 应用程序开发
5
00:00:17,184 --> 00:00:20,918
another brief set of slides on Model View Controller, this
另一个简单的幻灯片在模型-视图-控制器,这个
今天的幻灯片是关于 MVC,模型——视图——控制器
6
00:00:20,987 --> 00:00:24,355
design methodology that we're gonna use to build all of our
设计方法,我们将使用我们所有的
一个编写所有 iOS 程序都会用到的设计模式
7
00:00:24,424 --> 00:00:27,825
iOS apps, And then I'm gonna continue the demo that we
iOS应用程序,然后继续我们的演示
之后就接着完成星期一开始的
8
00:00:27,894 --> 00:00:31,196
started on Monday, We're gonna actually incorporate MVC into
周一开始,我们会结合MVC
那个程序。我们会把这种设计模式
9
00:00:31,265 --> 00:00:33,931
the calculator, And it's also gonna be an opportunity for
计算器,它也会是一个机会
运用到计算其中。这也是一个大好机会
10
00:00:34,000 --> 00:00:38,870
me to show you a lot of other cool Swift, language features,
我给你很多其他降温迅速、语言特性,
能让我展示 Swift 的许多优点、语法和特性
11
00:00:38,939 --> 00:00:42,373
So what is MVC? Model view controller, what is that?
MVC是什么?
什么是 MVC,模型——视图——控制器呢?
12
00:00:42,442 --> 00:00:46,477
Basically, it starts out being a way that we divide up
基本上,它开始成为我们划分
从大体上看,这是我们把代码
13
00:00:46,546 --> 00:00:51,182
all of the code in our app into three different camps,
所有的代码在我们的应用程序分成三个不同的阵营,
分割成了三个“阵营”
14
00:00:51,251 --> 00:00:54,453
These three different camps are the model camp, the model
这三个不同阵营的阵营的模型,模型
“阵营”的一个是 Model,模型
15
00:00:54,521 --> 00:00:59,057
is the "what" of your app, so what your app is, So,
是你的应用的“什么”,所以你的应用是什么,所以,
模型体现了你的程序“是什么”,也就是你的程序是干什么的
16
00:00:59,126 --> 00:01:02,261
in the calculator case, it's the brains of the calculator,
计算器,计算器的大脑,
用计算器来举例子,那模型就是它的大脑
17
00:01:02,329 --> 00:01:04,496
it's the thing that calculates, tha's the model,
这是计算的东西,那是模型,
负责计算的那个部分就是模型
18
00:01:04,564 --> 00:01:08,500
and tha's a UI independent part, It's what the app is,
和那一个UI的独立部分,它的应用是什么,
和 UI 是没有关系的。仅仅是背后的逻辑
19
00:01:08,569 --> 00:01:10,569
not how it displays or anything like that,
不是它如何显示或类似的东西,
和具体怎么展示出来是没有关系的
20
00:01:10,637 --> 00:01:13,838
How it displays is the controlle's job, So
它显示是怎样controlle的工作,所以呢
决定如何展示的是 Controller,控制器
21
00:01:13,907 --> 00:01:16,508
the controller decides how to take this UI independent
控制器独立决定如何把这个UI
控制器决定如何把这个与 UI 无关的
22
00:01:16,577 --> 00:01:18,610
thing, the model, and display it on your screen and
模型,并将其显示在你的屏幕上
这个模型显示到你的屏幕上
23
00:01:18,679 --> 00:01:21,580
interact with the user, That's what the controller job does,
与用户交互,控制器的工作是什么,
来和用户交互。这就是控制器的作用
24
00:01:21,648 --> 00:01:25,683
It's the, how it displays, Model is the what it is,
它是,它显示,模型是什么,
决定如何展示出来。模型是程序的本体
25
00:01:25,752 --> 00:01:27,352
Controller is how it displays,
控制器是如何显示,
控制器决定如何展示
26
00:01:27,421 --> 00:01:31,723
The view is the minions of the controller, okay?
视图控制器的奴才,好吗?
View,视图,是控制器手下的用人
27
00:01:31,792 --> 00:01:34,726
Things that the controller uses to do its job,
控制器用来做它的工作的事情,
控制器通过视图来实现它的功能
28
00:01:34,795 --> 00:01:39,231
almost always, these things in the view are generic,
几乎总是,这些东西在视图中是通用的,
通常视图都是能通用的
29
00:01:39,299 --> 00:01:42,968
things like UIButton or UIScrollView, okay?
UIButton或UIScrollView,好吗?
比如 UIButton 或者 UIScrollView
30
00:01:43,037 --> 00:01:45,303
Things that come from Apple or somewhere else,
来自苹果或其他地方的事情,
由苹果或是其它平台提供的
31
00:01:45,372 --> 00:01:48,306
even things you write, and you generally try to write them to
即使是你写,你通常试着写
包括你自己编写的。你应该尽量写通用的
32
00:01:48,375 --> 00:01:52,010
be pretty generic reusable UI things, And the controller is
非常通用的可重用的UI,以及控制器
能够重用的 UI 控件。而控制器
33
00:01:52,079 --> 00:01:56,214
trying to use those generic things to build a specific UI,
试图建立一个特定的UI使用这些通用的东西,
则是使用这些通用的控件构成特有的 UI
34
00:01:56,283 --> 00:01:59,551
Now the key to making MVC work is managing the communication
现在使MVC工作的关键是管理沟通
使用 MVC 的关键是组织联系
35
00:01:59,620 --> 00:02:02,220
between these camps because if we don't manage
这些阵营之间,因为如果我们不管理
这几个阵营。如果我们
36
00:02:02,289 --> 00:02:03,488
that communication,
沟通,
没有控制好交互
37
00:02:03,557 --> 00:02:06,224
we might as well just put them all in the big camp,
我们不妨把它们全部在大营地,
而只是把这些代码都堆在一起
38
00:02:06,293 --> 00:02:08,726
And they can all talk to each other however they want, So
他们都能互相交谈但是他们想要的,所以
三个阵营之间都能随便调用的话
39
00:02:08,795 --> 00:02:09,627
if we're gonna have three camps,
如果我们要有三个阵营,
那这分类也就没什么实际意义了
40
00:02:09,696 --> 00:02:11,230
we gotta think about how they communicate,
我们必须考虑他们如何沟通,
所以我们要考虑好三者之间是如何交互的
41
00:02:11,298 --> 00:02:14,799
So I've used this road sign kind of imagery here, okay,
所以我用这个路标的意象,好吧,
我这里用的道路交通标线来表示
42
00:02:14,868 --> 00:02:17,669
the roads, like the road lines in the middle of a road
的道路,就像路行中间的路
就是马路中间的那些线
43
00:02:17,737 --> 00:02:21,406
between them to help you visualize what is allowed
他们之间帮助你想象什么是允许的
让你们能形象地理解
44
00:02:21,475 --> 00:02:22,740
in terms of communication,
在沟通方面,
哪些沟通是被允许的
45
00:02:22,809 --> 00:02:25,677
So, let's talk about every boundary here between all of
所以,让我们来谈谈每一边界之间的所有
让我们来逐个分析阵营之间的交互
46
00:02:25,745 --> 00:02:29,280
these camps, Let's start with the controller talking to
这些营地,先从控制器
我们从控制器访问模型开始
47
00:02:29,349 --> 00:02:31,783
the model, okay, Can the controller talk to
模型,控制器能跟
控制器能和模型通信吗?
48
00:02:31,852 --> 00:02:34,920
the model? Absolutely, it can say anything to the model that
这个模型吗?
肯定是可以的。控制器能够访问
49
00:02:34,989 --> 00:02:38,590
the model publicly allows to be said to itself, So,
模型公开允许说本身,所以,
模型所有公开允许访问的内容
50
00:02:38,658 --> 00:02:41,893
it's a green arrow, It's a dashed white line on the road,
这是一个绿色的箭头,这是一个白色虚线在路上,
所以我用绿箭头。这是白虚线
51
00:02:41,962 --> 00:02:43,428
You can go across there all you want,
你可以在那里所有你想要的,
你可以随便跨过去
52
00:02:43,497 --> 00:02:44,930
And you can see why this is,
你可以看到为什么这是
这是可以理解的
53
00:02:44,998 --> 00:02:48,300
It is the controller job to get that model onto screen,
控制器的工作得到模型在屏幕上,
因为控制器的功能就是把模型展示到屏幕上
54
00:02:48,369 --> 00:02:50,702
It has to be able to talk to it all at once, So, there's no
它能够跟它一次,所以,没有
控制器应该是有全权处理的
55
00:02:50,770 --> 00:02:53,238
restriction of the controller talking to the model, And
控制器与模型的限制,
控制器访问模型是没有限制的
56
00:02:53,307 --> 00:02:56,741
similarly, there's no restriction of the controller
同样的,没有限制的控制器
同样的,也不会限制控制器
57
00:02:56,810 --> 00:02:59,912
talking to the view, because the view are the controller's
说到视图,因为视图控制器
与视图的沟通,因为视图和控制器
58
00:02:59,980 --> 00:03:02,480
minions, Right, so you gotta be able to tell your minions
奴才,对吧,所以你必须能够告诉你的仆从
是从属关系。你肯定要能够和下属沟通
59
00:03:02,549 --> 00:03:04,917
what to do without restriction, Now,
没有什么限制,现在,
告诉它们该做什么。这不应该被限制
60
00:03:04,985 --> 00:03:07,285
these connections between the controller and
这些控制器和之间的连接
这些控制器和视图之间的关连
61
00:03:07,354 --> 00:03:10,255
a view, we call them outlets, And you've already seen one in
一个视图,我们称之为媒体,你已经看过的
叫做 Outlet,出口。你们已经在计算器里见到过了
62
00:03:10,324 --> 00:03:13,058
the calculator, we created an outlet, if you remember,
计算器,我们创建了一个出口,如果你还记得,
如果你还记得的话,我们创建的那个出口
63
00:03:13,127 --> 00:03:15,426
it was called display, right?
它被称为显示,对吧?
就是叫做 display 的那个
64
00:03:15,495 --> 00:03:18,162
It was an instance variable in our controller, Remember that
这是一个实例变量在我们的控制器,记住这一点
控制器的实例变量
65
00:03:18,231 --> 00:03:20,265
class ViewController we saw on Monday,
类ViewController周一我们看到,
星期一看到的那个 ViewController 类
66
00:03:20,333 --> 00:03:24,069
that's the controller of the MVC that we built on Monday,
MVC的控制器,我们建立了周一,
我们周一就是构建了那个控制器
67
00:03:24,138 --> 00:03:26,238
And that display var, was an outlet,
显示变量,是一个出口,
那个变量 display 就是 outlet 出口
68
00:03:26,306 --> 00:03:30,508
and it's just wired up to a UILabel in our view, UILabel
这是连接到UILabel在我们看来,UILabel
和视图里面的 UILabel 关连起来的
69
00:03:30,577 --> 00:03:34,045
is one of the minions for the controller in the view, Okay,
是一个奴才的控制器在视图中,好吧,
那个 UILabel 就是视图中、控制器下属的其中一个
70
00:03:34,114 --> 00:03:38,450
so you've already seen that happen, The model and the view
所以你已经看到这种情况发生,模型和视图
所以你们已经见过这种交互了。模型和视图
71
00:03:38,518 --> 00:03:43,355
can never speak to each other, Double orange line there,
不能说话,双橙线,
它们之间是不能有交互的。双黄线
72
00:03:43,424 --> 00:03:46,892
no crossing either way across here, And this makes perfect
没有交叉穿过,这使得完美
两边都不能跨线。这也是很有道理的
73
00:03:46,961 --> 00:03:50,128
sense because the model is completely UI independent,
因为模型是完全独立于UI,
因为模型是完全和 UI 分开的
74
00:03:50,197 --> 00:03:53,064
and the view is completely UI, That's all the view is,
和观点是完全的UI,所有的观点是,
而视图全部是 UI 相关的
75
00:03:53,133 --> 00:03:54,299
So, they have nothing to talk about,
所以,他们没有谈论,
所以它们并没有共同语言
76
00:03:54,367 --> 00:03:58,170
these two guys, okay? Cuz they're completely, you know,
这两个家伙,好吗?
这两者之间,你知道的
77
00:03:58,238 --> 00:04:00,372
different camps that make no sense to talk to each other,
不同的阵营,相互交谈没有任何意义,
它们的交互完全是天方夜谭
78
00:04:00,440 --> 00:04:03,574
It's up to the controller to manage communication between
由控制器来管理之间的通信
控制器才是负责它们之间交互的
79
00:04:03,643 --> 00:04:08,113
these two, So, never shall you just communicate between those
这两个,所以,从不将之间的沟通
所以永远不要让模型和视图互相调用
80
00:04:08,181 --> 00:04:13,018
two camps, all right? How about the view talking back to
两个阵营?
好吧?那视图反过来,和控制器交互呢?
81
00:04:13,087 --> 00:04:16,288
its controller? Can the view, those minions, can they talk
它的控制器?
视图,作为仆从,能够联系
82
00:04:16,357 --> 00:04:18,890
to its controller? Now this is something you would clearly
其控制器吗?
它的控制器吗?很明显的,这是我们希望有的
83
00:04:18,959 --> 00:04:21,993
want because the minions gotta work with the controller, but
希望因为奴才要处理控制器,但是
因为它们是给控制器干活的
84
00:04:22,062 --> 00:04:23,895
it's a bit of a problem here,
这是一个问题,
但存在一个小问题
85
00:04:23,964 --> 00:04:26,498
Because these things in the view are generic,
因为这些东西在视图中是通用的,
那就是这些控件都是通用的
86
00:04:26,566 --> 00:04:30,035
They're things like UIButton, The class UIButton shipped
他们像UIButton,类UIButton装船
就像 UIButton 那样的
87
00:04:30,104 --> 00:04:33,338
from Apple last year, It knows nothing about a calculator,
从去年的苹果,它一个计算器,一无所知
苹果去年编写的 UIButton 类对我们计算器一无所知
88
00:04:33,407 --> 00:04:37,843
so how can that UIButton talk to a calculator? Well, we can
所以如何UIButton跟计算器吗?
那么 UIButton 要怎么和我们计算器通信呢?
89
00:04:37,911 --> 00:04:41,513
do it, We just have to sort of do it in a blind, meaning
这样做,我们只需要用一个盲人,意思吗
可是可以,不过它是被蒙在鼓里的
90
00:04:41,582 --> 00:04:43,815
we don't know the class of the thing we're talking to,
我们不知道的事情我们说的类,
因为我们并不知道我们联系的究竟是谁
91
00:04:43,884 --> 00:04:47,385
And structured manner, so that we can clearly understand what
和结构化的方式,这样我们就可以清楚地理解
这个通信也是规范化的,才能保证大家都能理解
92
00:04:47,454 --> 00:04:50,189
communication is happening, So you've already seen a blind
通信正在发生,所以你已经看到一个盲人
发送的是什么信息。你已经看到过这样的
93
00:04:50,257 --> 00:04:51,957
structured communication between the view and
结构化视图和之间的沟通
规范化的匿名通讯,在视图
94
00:04:52,025 --> 00:04:54,926
the controller, It's target action, okay, So
控制器,它的目标行动,所以
和控制器之间使用。这叫 target-action,目标对象-操作模式
95
00:04:54,995 --> 00:04:57,529
the controller can drop a target on itself
控制器可以降低目标
控制器可以把自己设置为目标
96
00:04:57,598 --> 00:05:00,665
by basically creating a method with that @IBAction,
通过与这@IBAction基本上创建一个方法,
然后创建个响应对应 @IBAction 操作的方法
97
00:05:00,734 --> 00:05:05,303
remember that from Monday? And then you just control drag and
还记得从周一吗?
还记得我们星期一的时候干的吗?按住 Control 键拖拽
98
00:05:05,372 --> 00:05:08,006
that lets the view UIButton talk,
让视图UIButton说话,
来创建 UIButton 视图的通讯
99
00:05:08,074 --> 00:05:11,509
basically connect up to that target, And now every
基本上连接这一目标,现在每一个
也就是关连那个通讯目标
100
00:05:11,578 --> 00:05:14,279
time that the UIButton wants to talk to the controller,
次UIButton想跟控制器,
现在每次那个 UIButton 想要给控制器发送消息
101
00:05:14,348 --> 00:05:17,382
it's just sending that target action, So that is
只是发送目标动作,这就是
只需要把操作发送给目标对象即可
102
00:05:17,451 --> 00:05:21,920
super simple blind structured communication from the view
超级简单盲目的结构化的沟通从视图
这就是视图使用的标准化匿名通讯机制
103
00:05:21,989 --> 00:05:26,425
back to the controller, But, that's a little too simple for
控制器,但这有点太简单了
并以此来联系控制器。但是有的时候
104
00:05:26,493 --> 00:05:30,128
all the communication that these minions might wanna do,
所有这些奴才的沟通可能要做,
这种方式不足以实现这些下属的需求
105
00:05:30,197 --> 00:05:32,697
For example, sometimes, something in the view wants to
例如,有时候,在视图中想要的东西
比如有时候视图想要
106
00:05:32,766 --> 00:05:34,799
synchronize itself with the controller,
同步控制器本身,
和控制器保持同步
107
00:05:34,868 --> 00:05:37,903
Or it wants to know what the controller intends as it's
也想知道控制器的计划
或了解控制器使用自己想达到的目标
108
00:05:37,972 --> 00:05:41,239
being used, Let's use the example of a scroll view,
正在使用,让我们使用滚动视图的例子,
拿 UIScrollView 举例子
109
00:05:41,308 --> 00:05:43,108
Okay, a scroll view,
好的,滚动视图,
滚动视图
110
00:05:43,177 --> 00:05:44,709
when it's scrolling around,
滚动的时候,
在四处滑动的时候
111
00:05:44,778 --> 00:05:46,444
might wanna ask the controller,
可能想问控制器,
可能会想问控制器
112
00:05:46,513 --> 00:05:48,914
should I allow the user to scroll over here? Or
我应该允许用户滚动呢?
我能让用户滑动到这里吗?
113
00:05:48,982 --> 00:05:51,750
they might just wanna tell the controller, hey, the user
他们可能只是想告诉控制器,嘿,用户
或者想要告诉控制器
114
00:05:51,819 --> 00:05:55,486
scrolled down here, These kind of notifications and
和滚动,这样的通知
用户已经滑动到这里了。这种通知
115
00:05:55,555 --> 00:05:58,957
questions about whether it can do things, they kind of end up
问题是否能做事情,他们最终
和询问能否做某些事情的行为
116
00:05:59,026 --> 00:06:01,760
in things that start with these words I have up here,
的东西从上面这些话我已经开始,
通常都能被这样归类
117
00:06:01,828 --> 00:06:03,996
"will," "should," and "did," Like,
“将”、“应该”和“,”,
“will”(将要),“should”(能否)和“did”(已经)
118
00:06:04,064 --> 00:06:07,399
should I scroll to here? I will scroll right here,
我要滚动到这里吗?
“我应该滑动到这里吗”?“我将要滑动到这里”
119
00:06:07,468 --> 00:06:08,800
if the scroll view was about to scroll, And
如果滚动视图滚动,
如果滚动视图要开始滑动了
120
00:06:08,869 --> 00:06:11,637
then I did scroll down here after the scroll view is done,
我往下滚动视图完成后,
以及完成之后的“我已经滑动到这里了”
121
00:06:11,705 --> 00:06:14,373
So those kinds of communications,
这些类型的通信,
就是这类的通讯
122
00:06:14,441 --> 00:06:16,641
And the way we make that communication work,
和我们进行沟通的方式工作,
我们实现这类沟通的方法
123
00:06:16,710 --> 00:06:19,377
we can't just do this simple one method control drag,
我们不能只做这简单的一个方法控制阻力,
并不只是简单的按 Control 拖拽
124
00:06:19,446 --> 00:06:22,380
instead we use what's called a delegate, And I'm gonna talk
相反,我们使用所谓的代表,我要说话
而是通过 delegate,委托代理
125
00:06:22,449 --> 00:06:25,984
a lot more about a delegate in future lectures, But basically
更多关于一个委托在以后的讲座,但基本上
之后的课程里我会细讲委托模式
126
00:06:26,053 --> 00:06:29,788
the idea of a delegate is, it's a var in the view, so
一个委托的想法是,这是一个var在视图中,
简单来说代理是视图里的一个变量
127
00:06:29,857 --> 00:06:34,192
like a var on scroll view, And it's of a special type; that
像一个var滚动视图,这是一种特殊类型,
比如 UIScrollView 里的一个属性,类型十分特殊
128
00:06:34,261 --> 00:06:37,295
type is called a protocol, I know that a lot of you,
类型被称为一个协议,我知道你们很多人,
叫做 protocol,协议。我知道你们大部分人
129
00:06:37,364 --> 00:06:38,664
even though you have object oriented experience,
即使你有面向对象的经验,
即使有使用面向对象编程的经验
130
00:06:38,732 --> 00:06:40,699
maybe you don't have experience with protocols, But
也许你没有经验的协议,但是
也很有可能没有使用过协议
131
00:06:40,768 --> 00:06:42,000
essentially, a protocol is a way of
从本质上讲,一个协议的一种方式
协议本质上是一种
132
00:06:42,069 --> 00:06:45,804
saying that this type, this thing that I'm talking about,
说这个类型,这个东西,我说的是,
我这里的这个变量
133
00:06:45,873 --> 00:06:49,908
it responds to a certain set of methods, I don't know what
这对一个特定的方法,我不知道
能够响应这些方法
134
00:06:49,977 --> 00:06:52,344
class it is, I don't know if it's a view controller,
类,我不知道这是一个视图控制器,
我不知道它具体是什么类型,也不知道是不是 ViewController
135
00:06:52,413 --> 00:06:53,678
I don't know what it is, but
我不知道那是什么,但是
虽然我不知道具体是什么
136
00:06:53,747 --> 00:06:56,215
I know it's gonna respond to these methods like will
我知道它会回应这些方法等
但是我知道它拥有这些方法,比如
137
00:06:56,283 --> 00:06:58,783
scroll, did scroll, should scroll,
滚动,滚动,滚动,
willScroll, didScroll, shouldScroll
138
00:06:58,852 --> 00:07:01,420
those kinds of methods, That's all I know about it, So
这些方法,这就是我知道的,所以
我所知道的就是它有这类的方法
139
00:07:01,488 --> 00:07:05,423
the controller, it signs up to listen to those methods,
控制器,它报名听这些方法,
所以控制器如果想要监听这些事件
140
00:07:05,492 --> 00:07:08,994
and then it sets itself as the delegate in the view,
然后它集本身为委托在视图中,
就可以把自己设为视图的 delegate
141
00:07:09,062 --> 00:07:10,862
Okay, and we're gonna see this in action, so
好的,我们会看到在行动,
我们还会实际用到的
142
00:07:10,931 --> 00:07:13,097
don't worry about too much now, but that's how we can
现在不要担心太多,但我们可以
所以现在理解不了不用担心
143
00:07:13,166 --> 00:07:15,534
have structured communication where they're agreeing,
结构化的沟通,他们同意,
不过这就是规范通讯的方法
144
00:07:15,602 --> 00:07:18,136
The view, this generic object is agreeing to
看来,这个通用的对象是同意
通用的视图同意
145
00:07:18,205 --> 00:07:20,605
send certain messages to the controller,
某些消息发送到控制器,
给控制器发送一定的信息
146
00:07:20,674 --> 00:07:23,208
not where the controller can be of any type and
并不是任何类型的控制器可以
所以控制器可以是任何类型的
147
00:07:23,277 --> 00:07:26,144
still serve this purpose of controlling its view,
仍然服务于这一目的的控制它的视图,
同时又能实现控制视图的功能
148
00:07:26,213 --> 00:07:30,616
Now there's a special kind of communication between the view
现在有一种特殊的视图之间的沟通
控制器和视图之间还有一种特殊的联系
149
00:07:30,684 --> 00:07:34,686
and the controller that arises because a view cannot own
视图和控制器,是因为不能自己
起因是视图不会包含它所需要的数据
150
00:07:34,755 --> 00:07:38,356
the data it displays, And what does that mean? That means you
数据显示,这是什么意思?
这是什么意思呢?
151
00:07:38,425 --> 00:07:41,460
don't want to have inside your view class, and remember, view
不想在你的视图类,记住,视图
这意味着你不希望在视图
152
00:07:41,528 --> 00:07:43,928
classes are like scroll views, buttons, things like that,
类就像滚动视图、按钮,
也就是比如滚动视图,按钮这些类中
153
00:07:43,997 --> 00:07:46,531
You don't wanna have data in there that is
你不想有数据
你不希望有特定的数据
154
00:07:46,600 --> 00:07:49,701
the data you're displaying, I'll give you an example,
你显示的数据,我将给你一个例子,
也就是要展示的数据。我们举个例子
155
00:07:49,770 --> 00:07:51,870
Let's say your entire iPod music library,
假设你的整个iPod音乐库,
比如你 iPod 里面的音乐库
156
00:07:51,939 --> 00:07:54,640
let's say you have hundreds of thousands of songs in there,
假设你有成千上万的歌曲,
里面有成千上万的歌曲
157
00:07:54,708 --> 00:07:58,410
Okay, so I wanna have a list of them on screen, So
好的,所以我想有一个列表在屏幕上,所以
我希望把它们展示出来
158
00:07:58,479 --> 00:08:02,314
I'm gonna use a generic item in my view called table view,
我将使用一个通用的项目在我看来称为表视图,
因此我用一个通用的 UITableView 来展示
159
00:08:02,383 --> 00:08:04,783
Table view just shows a big long list of something,
表视图只显示一大长串的东西,
表格视图的功能是显示一长串内容
160
00:08:04,852 --> 00:08:08,320
It can be a list a million items long, okay? So
它可以是一个一百万个条目的列表,好吗?
比如一个有一百万项内容的列表
161
00:08:08,388 --> 00:08:11,422
if I really had to take my entire iPod library and
如果我真的要把我整个图书馆和iPod
如果我需要把整个音乐库里的歌曲
162
00:08:11,491 --> 00:08:13,592
put it in the value of a var of
把它放在的var的价值
放到 UITableView 的某个变量中
163
00:08:13,660 --> 00:08:17,696
the table view to display it, that would be ridiculous,
表视图显示它,那将是荒谬的,
让它去展示,那这样做是很荒唐的
164
00:08:17,764 --> 00:08:20,131
Not just from a performance standpoint, but
不仅从性能的角度来看,但是
先不考虑程序的性能
165
00:08:20,200 --> 00:08:22,000
now I have two copies of my iPad, or
现在我有两个我的iPad的副本,或
因为我现在相当于有两个 iPad
166
00:08:22,069 --> 00:08:25,003
my iPod music library, one inside this table view, and
我的iPod音乐库,一个在这个表格视图,
有两个 iPad 的音乐库:一个在表格视图里
167
00:08:25,072 --> 00:08:27,973
one in some database somewhere in my phone, That's bad, where
一个在某些数据库在我的电话,那就糟糕了,
一个在 iPhone 上的某个数据库里。这是不合适的