-
Notifications
You must be signed in to change notification settings - Fork 149
/
7. Multiple MVCs, Timer, and Animation.srt
7418 lines (5924 loc) · 153 KB
/
7. Multiple MVCs, Timer, and Animation.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,736
[MUSIC]
2
00:00:04,805 --> 00:00:09,374
>> Stanford University.
>> Hey, well,
3
00:00:09,443 --> 00:00:14,012
welcome to Lecture number 7,
Stanford CS193P, Fall 2017. So
4
00:00:14,080 --> 00:00:19,351
our primary topic today is
combining MVCs to make bigger,
5
00:00:19,420 --> 00:00:22,153
more powerful applications,
obviously. And
6
00:00:22,222 --> 00:00:24,890
then I'm gonna do a big demo
that shows you how to do that.
7
00:00:24,959 --> 00:00:27,559
We're gonna do it with
Concentration, we're gonna add
8
00:00:27,627 --> 00:00:30,929
an MVC into that app. Then
I'll come back to the slides,
9
00:00:30,998 --> 00:00:33,865
and talk I'll about timer and
choose classes, some of you
10
00:00:33,934 --> 00:00:37,169
used in the extra credit of
the homework assignments. And
11
00:00:37,237 --> 00:00:39,004
then I'm gonna dive into
the next topic and try and
12
00:00:39,073 --> 00:00:41,373
get as far as I can
which is animation.
13
00:00:41,442 --> 00:00:43,275
Which will be our
topic on Wednesday and
14
00:00:43,343 --> 00:00:45,376
I'll get to much more detail,
do a big demo.
15
00:00:45,445 --> 00:00:47,512
But I wanna kinda get in as
far as I can today just so
16
00:00:47,581 --> 00:00:50,916
we have more time for
a demo on Wednesday.
17
00:00:50,984 --> 00:00:54,486
Alright so combining MVCs,
remember this slide from when
18
00:00:54,555 --> 00:00:58,190
I talked about MVC. This is
the MVCs working together, and
19
00:00:58,259 --> 00:01:01,193
we saw that when we have
MVCs working together,
20
00:01:01,261 --> 00:01:04,862
these other MVCs. Let me get
my thing under control here,
21
00:01:04,931 --> 00:01:08,967
well, can't do it. Okay, these
other MVCs down in the corner,
22
00:01:09,036 --> 00:01:12,170
they serve as part of the view
of another MVC. That's
23
00:01:12,239 --> 00:01:16,608
the way MVC's relate. An MVC
is a view, part of a view
24
00:01:16,677 --> 00:01:19,811
of another class. And that has
some ramifications for MVC,
25
00:01:19,880 --> 00:01:22,781
most notably it has to have
blind structured communication
26
00:01:22,849 --> 00:01:26,618
back to the other MVC.
But that's the way we
27
00:01:26,687 --> 00:01:29,154
structure it, and that keeps
everything well contained and
28
00:01:29,222 --> 00:01:32,523
understandable. So we're gonna
talk about how we do that,
29
00:01:32,592 --> 00:01:36,595
how we make MVCs that are
the view of other MVCs. So,
30
00:01:36,663 --> 00:01:40,065
combining MVCs we do
this using some special
31
00:01:40,133 --> 00:01:44,569
controllers that iOS provides
whose view is other MVCs.
32
00:01:44,638 --> 00:01:48,339
Now you could write your own
special MVC whose view is
33
00:01:48,408 --> 00:01:52,544
other MVCs. But 99% of the
time you're gonna use one of
34
00:01:52,612 --> 00:01:55,980
the three that iOS provides.
And the three that iOS
35
00:01:56,049 --> 00:01:59,418
provides are TabBarController,
SplitViewController, and
36
00:01:59,486 --> 00:02:02,520
navigation controller.
Now there are other ways to
37
00:02:02,589 --> 00:02:05,157
combine MVCs we'll talk about
later in the quarter but
38
00:02:05,225 --> 00:02:07,859
this is the primary way that
we're gonna do it with these
39
00:02:07,928 --> 00:02:11,229
three MVC's. Those three here,
TabBar, SplitView, and
40
00:02:11,298 --> 00:02:15,567
NavigationController, they are
the controllers of MVCs. So
41
00:02:15,636 --> 00:02:17,002
let's look at them one by one.
42
00:02:17,070 --> 00:02:18,870
TabBar is by far
the simplest one,
43
00:02:18,939 --> 00:02:22,240
you've seen this
a million times, it's got
44
00:02:22,309 --> 00:02:24,475
some tabs along the bottom
there. You see those tabs,
45
00:02:24,544 --> 00:02:27,179
like a little orange thing and
the gray one's next to it. And
46
00:02:27,247 --> 00:02:31,049
each of those tabs, when you
press it, shows another MVC.
47
00:02:31,117 --> 00:02:34,519
So you can already see that
the TabBarController's view
48
00:02:34,588 --> 00:02:39,257
includes the buttons at
the bottom and another MVC. So
49
00:02:39,326 --> 00:02:42,227
this is what we mean by
the view of this tab bar
50
00:02:42,295 --> 00:02:46,498
controller MVC gives another
MVC. And the icon and
51
00:02:46,567 --> 00:02:48,566
the title that appears
down in the TabBar,
52
00:02:48,635 --> 00:02:51,570
that's determined by
which MVC is showing.
53
00:02:51,638 --> 00:02:55,240
In specific UI view controller
has this bar TabBar item.
54
00:02:55,308 --> 00:02:58,709
And the TabBar controller,
when it shows the MVC as part
55
00:02:58,778 --> 00:03:02,280
of its view, it looks at that
so it knows what the title and
56
00:03:02,349 --> 00:03:05,917
icon to display down at the
bottom there. This is all very
57
00:03:05,986 --> 00:03:10,722
object oriented in that sense.
So, for example here we've got
58
00:03:10,791 --> 00:03:13,658
some sort of health
oriented API here and
59
00:03:13,727 --> 00:03:16,961
we've got the four tabs at the
bottom. By the way, if there's
60
00:03:17,030 --> 00:03:21,300
more than four tabs, more than
four MVCs, it can show that,
61
00:03:21,368 --> 00:03:24,469
actually, it'll show five.
And if they're six, then
62
00:03:24,538 --> 00:03:27,439
the fifth one becomes a little
more button and you click it.
63
00:03:27,508 --> 00:03:31,243
And iOS provides an API or UI,
rather, for the user to pick
64
00:03:31,311 --> 00:03:33,044
which tabs they want to be
there at the bottom, or
65
00:03:33,113 --> 00:03:35,480
to pick some other tab that's
not showing. Generally though,
66
00:03:35,549 --> 00:03:39,384
we recommend no more than five
MVCs in a TabBarController.
67
00:03:39,452 --> 00:03:41,720
That's kinda just
pretty good UI,
68
00:03:41,789 --> 00:03:43,154
It just starts to get
a little complicated for
69
00:03:43,223 --> 00:03:46,291
the user if there's more
than that. So, in this case,
70
00:03:46,360 --> 00:03:50,395
this whole thing is five MVCs,
the TabBarController itself,
71
00:03:50,463 --> 00:03:53,131
and the four MVCs that
serve as its view and
72
00:03:53,200 --> 00:03:55,934
you just switch between
them by clicking.
73
00:03:56,002 --> 00:04:00,805
So this is by far the easiest
Of the MVCs of MVCs,
74
00:04:00,874 --> 00:04:04,008
all right? Next, let's talk
about SplitViewController,
75
00:04:04,077 --> 00:04:06,911
probably the second simplest
one. It's only two MVCs in its
76
00:04:06,980 --> 00:04:10,348
view, and it puts them side by
side with a very thin line in
77
00:04:10,417 --> 00:04:13,751
between them. So, here's
a little calculator app,
78
00:04:13,820 --> 00:04:16,454
It's got the calculator on the
left and then it's got maybe
79
00:04:16,523 --> 00:04:19,790
a graph of what's in the
calculator on the right. And
80
00:04:19,859 --> 00:04:23,628
we call this MVC that's on the
left, and it's usually smaller
81
00:04:23,697 --> 00:04:27,132
like this, the Master in
the SplitViewController, and
82
00:04:27,200 --> 00:04:30,134
we call the one on
the right the Detail. And
83
00:04:30,203 --> 00:04:32,471
we call them master detail cuz
usually what you do on the one
84
00:04:32,539 --> 00:04:34,672
on the left affects what
happens on the right. It's
85
00:04:34,741 --> 00:04:37,942
the master, the detail is kind
of little bit of a slave. And,
86
00:04:38,011 --> 00:04:42,614
of course, this is on iPad. It
also works on iPhone Pluses,
87
00:04:42,682 --> 00:04:47,084
but non-plus iPhones. This
little side-by-side mechanism.
88
00:04:47,153 --> 00:04:48,486
You can, of course,
rotate your iPad.
89
00:04:48,555 --> 00:04:51,089
And when you rotate your
iPad into portrait, now
90
00:04:51,157 --> 00:04:55,026
it'll only show the detail,
except for you can slide out
91
00:04:55,094 --> 00:04:57,528
with your finger from
the left and see the Master,
92
00:04:57,597 --> 00:05:01,899
then slide it back. Now on
an iPhone Plus in portrait,
93
00:05:01,968 --> 00:05:04,403
it acts like a normal iPhone,
it doesn't have the slide out.
94
00:05:04,471 --> 00:05:06,904
Even if an iPhone Plus is
not quite wide enough to
95
00:05:06,973 --> 00:05:11,810
slide that thing out. So,
SplitViewController, super,
96
00:05:11,878 --> 00:05:14,646
super easy. And then there's
NavigationController.
97
00:05:14,715 --> 00:05:17,282
This is probably the most
flexible, most powerful of
98
00:05:17,350 --> 00:05:20,652
the MVC of MVCs, you've seen
this a million times in IOS
99
00:05:20,720 --> 00:05:23,855
apps. You can think of
a NavigationController MVC,
100
00:05:23,924 --> 00:05:28,025
as a deck of cards. And
each card is an MVC. And what
101
00:05:28,094 --> 00:05:31,829
the navigation controller does
is let you put an MVC on top,
102
00:05:31,898 --> 00:05:34,499
obscuring all the ones below.
And then if you hit the back
103
00:05:34,568 --> 00:05:37,201
button, it throws that one
away, and shows you the one
104
00:05:37,270 --> 00:05:39,337
that was there before. And
you can keep going back, and
105
00:05:39,406 --> 00:05:40,871
it keeps showing the cards
off the top, and
106
00:05:40,940 --> 00:05:44,009
you put new cards back on. So
it's like a card stack, where
107
00:05:44,077 --> 00:05:48,512
the stack are MVCs. And you
can see that the navigation
108
00:05:48,581 --> 00:05:51,583
controller as part of its view
it does actually draw a little
109
00:05:51,652 --> 00:05:54,319
bit. At the top it draws
a little title there, right?
110
00:05:54,388 --> 00:05:57,155
The settings, okay, that's
not part of the MVC that's
111
00:05:57,224 --> 00:06:00,458
showing right there. But the
content that are in that title
112
00:06:00,527 --> 00:06:03,562
are driven by whatever MVCs
on top of the card stack,
113
00:06:03,630 --> 00:06:06,965
the one that's showing.
So that's called settings
114
00:06:07,034 --> 00:06:09,734
because the MVC that's there
is the settings MVC, so
115
00:06:09,803 --> 00:06:13,605
it's being driven by what's
there. And each MVC,
116
00:06:13,674 --> 00:06:16,708
similar with the tab bar item
and the tab bar controller,
117
00:06:16,776 --> 00:06:21,012
there's a var in UI Controller
called navigationItem. And
118
00:06:21,081 --> 00:06:24,649
that contains things like the
title and other buttons that
119
00:06:24,717 --> 00:06:27,252
you can put in the title bar,
all kinds of stuff.
120
00:06:27,320 --> 00:06:28,953
You're gonna wanna go look
at the documentation for
121
00:06:29,022 --> 00:06:29,754
navigationItem and
122
00:06:29,823 --> 00:06:32,657
see what kind of things
the navigation controller can
123
00:06:32,725 --> 00:06:37,596
do for you when your MVC
is on top of the stack. So
124
00:06:37,664 --> 00:06:40,732
if I then click on something,
this is a table view here,
125
00:06:40,800 --> 00:06:44,001
we'll talk about table views
next week or the week after.
126
00:06:44,070 --> 00:06:47,639
If I click on something here
then a new MVC slides in,
127
00:06:47,708 --> 00:06:52,477
animates in on top. Now there
are two cards on this stack.
128
00:06:52,546 --> 00:06:54,179
All settings one
which is now behind,
129
00:06:54,247 --> 00:06:57,715
we can't see it, and now this
new one on top of course.
130
00:06:57,784 --> 00:06:59,618
By the way you can
also have tool bar
131
00:06:59,686 --> 00:07:02,353
items on the bottom using
the tool bar items bar of view
132
00:07:02,422 --> 00:07:04,288
controller, so those two
of our items would come and
133
00:07:04,357 --> 00:07:07,626
go as new ones come
on the stack. But
134
00:07:07,694 --> 00:07:09,961
also notice that when we
have two things on there,
135
00:07:10,030 --> 00:07:11,863
a little back button
appears in the upper left,
136
00:07:11,932 --> 00:07:15,600
automatically. It even has
the name of the one that's
137
00:07:15,668 --> 00:07:18,903
lower down on the stack of
cards. And if you press that,
138
00:07:18,972 --> 00:07:20,805
of course it's gonna
throw this card away, and
139
00:07:20,873 --> 00:07:23,842
we'll be back at the other
card. But lets imagine I click
140
00:07:23,910 --> 00:07:25,409
on something in
this one like this
141
00:07:25,478 --> 00:07:27,679
accessibility thing.
It puts a new card on there,
142
00:07:27,747 --> 00:07:30,781
that's the accessibility card.
Maybe I click on something
143
00:07:30,850 --> 00:07:33,718
inside the accessibility card
like larger text, and it puts
144
00:07:33,786 --> 00:07:37,388
another one on. Now we have
four MVCs on the the stack.
145
00:07:37,457 --> 00:07:39,156
So there's a total
of five MVCs here.
146
00:07:39,225 --> 00:07:41,926
The navigation controller
itself which is an MVC, and
147
00:07:41,995 --> 00:07:46,197
these four MVCs which are part
of its view. And of course we
148
00:07:46,266 --> 00:07:48,333
know we can backup if we
pres the back button and
149
00:07:48,401 --> 00:07:50,902
it throws that button away,
and it actually does throws
150
00:07:50,971 --> 00:07:53,237
the card away.
Throws it out of the heap.
151
00:07:53,306 --> 00:07:55,773
So it's complete throw away,
this card now gets thrown
152
00:07:55,842 --> 00:07:58,242
away, we are just now seeing
the card on the underneath
153
00:07:58,311 --> 00:08:01,212
until we go all the way
the back, To the beginning and
154
00:08:01,281 --> 00:08:03,180
now we're seeing
the root card, the root
155
00:08:03,249 --> 00:08:06,150
view controller, this is
called a navigationController.
156
00:08:06,219 --> 00:08:08,385
All right, so everybody knows
about navigationController,
157
00:08:08,454 --> 00:08:09,787
you've seen it everywhere.
158
00:08:09,856 --> 00:08:12,089
Here's how navigational
controller's working behind
159
00:08:12,158 --> 00:08:15,527
the scenes in terms of looking
at it from an MVC standpoint.
160
00:08:15,596 --> 00:08:18,462
So let's say I have a MVC
that's shown there,
161
00:08:18,531 --> 00:08:21,198
and I have more API or
more UI than I can fit on
162
00:08:21,267 --> 00:08:25,369
my screen, so I need to got to
another screen to do more UI.
163
00:08:25,438 --> 00:08:27,905
That's the little thing in the
corner right there. Well, to
164
00:08:27,974 --> 00:08:31,142
do that I have to make another
MVC that will control that UI.
165
00:08:31,210 --> 00:08:34,078
And then when I make my
navigationController
166
00:08:34,147 --> 00:08:37,015
to have them share the screen,
the navigationController comes
167
00:08:37,083 --> 00:08:39,684
up and this is what its UI
looks like. But as soon
168
00:08:39,752 --> 00:08:42,587
as you set a very important
var in navigationController,
169
00:08:42,656 --> 00:08:45,690
call it rootViewController
to be some MVC,
170
00:08:45,759 --> 00:08:51,162
then its view now includes
the view of that MVC. And
171
00:08:51,231 --> 00:08:54,298
then, if you interact
with some button or
172
00:08:54,367 --> 00:08:56,534
something that's in
that MVC's view,
173
00:08:56,603 --> 00:09:00,472
it can move to this other MVC,
push it on the stack.
174
00:09:00,540 --> 00:09:02,173
Now the other MVC is still
there, it's just kind of
175
00:09:02,242 --> 00:09:05,644
behind the scenes because it's
lower on the stack, and a back
176
00:09:05,712 --> 00:09:07,378
button will automatically
appear as we saw.
177
00:09:07,447 --> 00:09:10,882
When we click this back we
move back to the other one,
178
00:09:10,951 --> 00:09:14,285
and notice that the MVC that
we had originally added
179
00:09:14,354 --> 00:09:17,522
on there is gone. So it's
very important to understand
180
00:09:17,591 --> 00:09:19,056
as we use
the navigationController,
181
00:09:19,125 --> 00:09:21,392
usually when we put things on
we're creating a new one and
182
00:09:21,461 --> 00:09:23,461
when we take things off
we're throwing it away.
183
00:09:23,530 --> 00:09:27,164
That is the normal operation
of a navigationController. It
184
00:09:27,233 --> 00:09:30,669
can be used in other ways but
that is it's normal operation.
185
00:09:31,971 --> 00:09:36,341
Okay, so those are the three
kinds of MVCs of MVCs.
186
00:09:36,409 --> 00:09:40,444
How do you get at them? Okay,
it's really easy actually.
187
00:09:40,513 --> 00:09:44,015
Each of those three different
kinds of MVCs has a var called
188
00:09:44,084 --> 00:09:46,951
viewControllers, it's just an
array of view controller. So
189
00:09:47,020 --> 00:09:49,854
for a tab bar it's just
the view controllers one, two,
190
00:09:49,923 --> 00:09:52,089
three, four in order I
think from left to right,
191
00:09:52,158 --> 00:09:54,426
the tabs. If it's
a split-view controller,
192
00:09:54,494 --> 00:09:57,128
then view controller
sub 0 is the master and
193
00:09:57,197 --> 00:09:59,697
view controller sub
1 is the detail.
194
00:09:59,766 --> 00:10:01,632
Or sometimes we say the first
thing in the array is
195
00:10:01,701 --> 00:10:04,469
the master and the last thing
in the array is the detail.
196
00:10:04,538 --> 00:10:06,170
And if it's
a navigationController, then
197
00:10:06,239 --> 00:10:08,806
viewController sub 0 is the
rootViewController, and all of
198
00:10:08,875 --> 00:10:12,744
these cards lined on top
are the other viewControllers
199
00:10:12,812 --> 00:10:15,079
in the array in order. So
that's how you can get at all
200
00:10:15,147 --> 00:10:18,449
the viewControllers that are
in one of the MVCs of MVCs.
201
00:10:18,518 --> 00:10:22,020
Now, how do you get at
the MVC of MVCs itself?