-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy path9. View Controller Lifecycle and Scroll View.srt
7072 lines (5646 loc) · 146 KB
/
9. View Controller Lifecycle and Scroll View.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,937
[MUSIC]
2
00:00:05,005 --> 00:00:08,940
Stanford University.
>> Okay, well,
3
00:00:09,009 --> 00:00:12,444
welcome to Lecture 9 of
Stanford CS193P, fall of 2017.
4
00:00:12,512 --> 00:00:15,080
So we have two
major topics today.
5
00:00:15,149 --> 00:00:18,249
We're gonna talk about
the view controller lifecycle,
6
00:00:18,318 --> 00:00:20,285
which is when your
MVC comes alive and
7
00:00:20,354 --> 00:00:23,655
does all the things it's gonna
do and then dies eventually.
8
00:00:23,724 --> 00:00:27,525
And then we're also gonna talk
about a UIView called scroll
9
00:00:27,594 --> 00:00:31,163
view which allows you to look
at large UIViews by panning
10
00:00:31,232 --> 00:00:34,899
around on them, zooming in
on them, etc., all right.
11
00:00:34,968 --> 00:00:35,500
View controller lifecycle.
12
00:00:35,569 --> 00:00:38,604
So the view controllers
have a lifecycle.
13
00:00:38,672 --> 00:00:41,640
It's essentially marked by
a sequence of messages.
14
00:00:41,708 --> 00:00:44,543
Methods that are invoked
on the UIView Controller.
15
00:00:44,611 --> 00:00:48,246
Why do we care about this at
all? Because at the various
16
00:00:48,315 --> 00:00:50,849
stages of the lifecycle of
the view controller we might
17
00:00:50,918 --> 00:00:54,319
wanna get involved, right,
and do certain things. So
18
00:00:54,388 --> 00:00:57,488
this lifecycle starts with
the creation of your UIView
19
00:00:57,557 --> 00:01:00,993
controller. For your view
controllers that you write,
20
00:01:01,061 --> 00:01:03,161
this is almost always
coming out of a storyboard,
21
00:01:03,230 --> 00:01:05,731
right? You drag something into
storyboard, you put your views
22
00:01:05,799 --> 00:01:08,433
in there. That's how you
know how to create an MVC.
23
00:01:08,502 --> 00:01:11,203
So your lifecycle is gonna
be when your app runs and
24
00:01:11,271 --> 00:01:13,805
you've segued to something or
it's your first view
25
00:01:13,873 --> 00:01:17,876
controller in your storyboard
or what whatever. Now iOS
26
00:01:17,945 --> 00:01:19,911
also has some API which we
haven't learned about yet
27
00:01:19,980 --> 00:01:23,781
that can hand you a view
controller, that you can
28
00:01:23,850 --> 00:01:27,686
then segue to or whatever.
For example, there's a view
29
00:01:27,754 --> 00:01:30,588
controller that lets you take
a picture with the camera.
30
00:01:30,657 --> 00:01:33,424
So, it can come from code as
well, it's not only out of
31
00:01:33,493 --> 00:01:35,827
the storyboard.
But once it's created,
32
00:01:35,896 --> 00:01:38,396
what happens to it? And you've
already actually learned a lot
33
00:01:38,465 --> 00:01:40,598
about the lifecycle
of a view controller.
34
00:01:40,667 --> 00:01:41,866
Of course, the first thing
35
00:01:41,935 --> 00:01:44,903
happens after it's created is
that it gets prepared to be
36
00:01:44,972 --> 00:01:48,840
segued to, right? So if it's
being put on screen because of
37
00:01:48,909 --> 00:01:51,109
segue, the preparation
part happens.
38
00:01:51,178 --> 00:01:54,846
Then after that importantly,
the outlets get set, or
39
00:01:54,914 --> 00:01:58,383
your UI buttons and whatever
those connections get made.
40
00:01:58,452 --> 00:02:00,752
Then your view
controller appears and
41
00:02:00,821 --> 00:02:04,922
possibly disappears on screen,
right? Imagine a split view in
42
00:02:04,991 --> 00:02:08,493
portrait mode, the master
disappears. And then you can
43
00:02:08,562 --> 00:02:10,929
slide it out and then slide
it away so it's appearing and
44
00:02:10,997 --> 00:02:14,700
disappearing, for example.
Also, along the way, as all of
45
00:02:14,768 --> 00:02:17,435
this is happening you might
have geometry changes.
46
00:02:17,504 --> 00:02:19,537
Most notably,
you rotate your device, and
47
00:02:19,606 --> 00:02:22,340
you go from having this tall,
thin rectangle to a shorter,
48
00:02:22,409 --> 00:02:25,777
wider rectangle. So
geometry changes can happen.
49
00:02:25,845 --> 00:02:27,846
They can happen for
other reasons, as well.
50
00:02:27,915 --> 00:02:30,148
Again, in a split view,
you know, your master,
51
00:02:30,217 --> 00:02:32,751
sometimes it's in the little
tall thing on the left.
52
00:02:32,819 --> 00:02:34,786
Sometimes there's a shorter
thing on the left and
53
00:02:34,855 --> 00:02:35,954
the one thing on the right,
54
00:02:36,023 --> 00:02:37,556
it sometimes fills
the whole screen.
55
00:02:37,625 --> 00:02:38,790
Or, when it's in landscape,
56
00:02:38,858 --> 00:02:39,958
it fills just a part
of the screen.
57
00:02:40,027 --> 00:02:42,728
So the geometry is changing,
58
00:02:42,796 --> 00:02:44,963
depending on the situation
of your view controller.
59
00:02:45,032 --> 00:02:47,465
And then lastly, and
by far least importantly,
60
00:02:47,534 --> 00:02:49,067
in a low-memory situations,
61
00:02:49,136 --> 00:02:51,669
you might have your view
controller asked to free up
62
00:02:51,738 --> 00:02:54,238
some memory, we'll talk
about that in a second.
63
00:02:54,307 --> 00:02:57,075
All right, so let's talk about
all the methods that get sent
64
00:02:57,144 --> 00:02:59,644
to your view controller when
all these things happen.
65
00:02:59,713 --> 00:03:02,414
Now, you already know
the first one, viewDidLoad, I
66
00:03:02,483 --> 00:03:06,918
mentioned it in your homework.
It's a really powerful place
67
00:03:06,987 --> 00:03:10,555
to do initialization, because
you're already prepared and
68
00:03:10,624 --> 00:03:14,226
all your outlets are set, so
now you really can go to town.
69
00:03:14,295 --> 00:03:16,528
So it's great for that and
we usually put most
70
00:03:16,596 --> 00:03:19,097
of our initialization in
viewDidLoad with one huge
71
00:03:19,166 --> 00:03:24,302
exception. And it's in red so
don't miss it geometry.
72
00:03:24,371 --> 00:03:28,606
When viewDidLoad runs, your
bounds have not been set yet,
73
00:03:28,675 --> 00:03:32,510
so do not put things in here
that have to do with the size
74
00:03:32,579 --> 00:03:35,880
you are on screen. Because
you have not been fitted to
75
00:03:35,949 --> 00:03:39,751
whatever device you're on,
okay, it's very important.
76
00:03:39,820 --> 00:03:42,453
Very common mistake to put
stuff in here and then you
77
00:03:42,522 --> 00:03:45,023
just wonder why, when you run
it on a different device, your
78
00:03:45,091 --> 00:03:47,893
code doesn't work anymore.
So do not put geometry.
79
00:03:47,961 --> 00:03:49,661
I'm gonna show you where
to put your geometry
80
00:03:49,730 --> 00:03:52,664
changes in a moment here.
So that's viewDidLoad,
81
00:03:52,732 --> 00:03:57,769
a great one. The next
one is viewWillAppear.
82
00:03:57,837 --> 00:04:00,739
Now by the way, notice in all
of these I'm calling super
83
00:04:00,808 --> 00:04:03,775
viewWillAppear or super
viewDidLoad in all of them,
84
00:04:03,844 --> 00:04:06,344
you always do that,
do not forget to do that.
85
00:04:06,413 --> 00:04:09,247
In fact in a demo the other
day, I noticed I forgot to do
86
00:04:09,316 --> 00:04:11,616
it and so we'll fix
that in today's demo.
87
00:04:11,685 --> 00:04:13,485
But, and
I noticed in your homework,
88
00:04:13,554 --> 00:04:16,087
many of you did not call super
viewDidLoad. Now of course,
89
00:04:16,156 --> 00:04:17,689
we didn't talk about
view control lifecycle so
90
00:04:17,758 --> 00:04:18,890
I don't blame you for that.
91
00:04:18,959 --> 00:04:21,425
But from now on,
don't forget to do super,
92
00:04:21,494 --> 00:04:24,863
give your super class a chance
to find out it's appearing or
93
00:04:24,931 --> 00:04:28,333
that it got loaded or
whatever. So viewWillAppear,
94
00:04:28,401 --> 00:04:30,135
what do you do in
viewWillAppear?
95
00:04:30,204 --> 00:04:32,537
viewWillAppear is exactly
what it sounds like,
96
00:04:32,606 --> 00:04:35,707
your view controller's view
is about to appear on screen.
97
00:04:35,776 --> 00:04:38,743
So this is where you can catch
up with stuff that might've
98
00:04:38,812 --> 00:04:42,980
gone on in the world while
you were not on screen. Or
99
00:04:43,049 --> 00:04:44,983
if this is the first time
your coming on screen,
100
00:04:45,052 --> 00:04:47,084
catch up with the state
of the world. So
101
00:04:47,153 --> 00:04:49,988
this is really a place to kind
of load up your view with all
102
00:04:50,057 --> 00:04:52,123
the information from your
model, especially if your
103
00:04:52,191 --> 00:04:54,626
model can change like let's
say your model is a network
104
00:04:54,694 --> 00:04:57,628
database. Other people are
editing it, so it can change.
105
00:04:57,697 --> 00:05:00,465
So here's where you wanna get
up to date, because when your
106
00:05:00,534 --> 00:05:02,501
view is not on screen,
you don't want your view
107
00:05:02,569 --> 00:05:05,804
controller to be doing much
work. It just wants to kinda
108
00:05:05,872 --> 00:05:08,005
sit there quietly, and then
when it's time to come back,
109
00:05:08,074 --> 00:05:10,809
then it does its
little setup in here.
110
00:05:10,878 --> 00:05:12,811
One thing to notice about
viewWillAppear of course,
111
00:05:12,880 --> 00:05:14,011
it can be called repeatedly,
112
00:05:14,080 --> 00:05:15,880
because your view controller
can go away and come back,
113
00:05:15,949 --> 00:05:18,716
and go away and come back.
But viewDidLoad is only ever
114
00:05:18,785 --> 00:05:22,187
called once in the lifetime
of your view controller. It's
115
00:05:22,255 --> 00:05:24,790
just called once after you're
prepared and your outlets
116
00:05:24,858 --> 00:05:26,858
are setup, it gets called
once and that's it. But
117
00:05:26,926 --> 00:05:29,427
viewWillAppear obviously can
be called every time you're
118
00:05:29,496 --> 00:05:34,232
gonna come back on screen. Now
there's also a viewDidAppear.
119
00:05:34,301 --> 00:05:37,002
This is called to you after
you have come on screen.
120
00:05:37,070 --> 00:05:39,137
Now, what would
you wanna do here?
121
00:05:39,205 --> 00:05:41,205
It's a little late to
do things like update
122
00:05:41,274 --> 00:05:43,975
your view from your model here
because these things are on
123
00:05:44,044 --> 00:05:46,577
screen. You don't want things
to come on screen wrong and
124
00:05:46,646 --> 00:05:49,414
then view did appear and
now you've clean them up. But
125
00:05:49,483 --> 00:05:52,050
this is a great place to do
things like start an animation
126
00:05:52,119 --> 00:05:54,085
cuz you can't do that
in viewWillAppear.
127
00:05:54,153 --> 00:05:55,086
You're not on screen,
128
00:05:55,154 --> 00:05:58,623
yeah, right. Start a timer
that does something on screen.
129
00:05:58,692 --> 00:06:02,494
Start observing something in
the world like a GPS locations
130
00:06:02,563 --> 00:06:06,531
or maybe the gyro position
of your phone. Okay, those
131
00:06:06,599 --> 00:06:10,368
are all things you can do once
you're on screen. Okay, so
132
00:06:10,437 --> 00:06:12,770
that's what viewDidAppear
is really good for.
133
00:06:12,839 --> 00:06:15,640
One other thing that you can
do viewDidAppear is maybe
134
00:06:15,709 --> 00:06:19,744
kick off a very expensive
thing. Something you
135
00:06:19,813 --> 00:06:22,079
don't want to kick off
in viewDidLoad because,
136
00:06:22,148 --> 00:06:24,782
when viewDidLoad happens,
your view controller may not
137
00:06:24,851 --> 00:06:27,519
actually appear on screen.
Okay, there's no guarantee
138
00:06:27,588 --> 00:06:30,021
that anyone's ever gonna
actually put you on screen.
139
00:06:30,089 --> 00:06:32,824
So viewDidLoad is not a great,
even veiwWillAppear,
140
00:06:32,893 --> 00:06:35,493
surprisingly, might get
called on you, and then,
141
00:06:35,562 --> 00:06:38,763
you don't actually appear on
screen. So viewDidAppear,
142
00:06:38,832 --> 00:06:41,466
you know you're on screen. So
it's worth it to do something
143
00:06:41,534 --> 00:06:44,336
expensive. Now what would
be something expensive?
144
00:06:44,404 --> 00:06:47,539
Let's say you wanna fetch
a huge image off the network,
145
00:06:47,607 --> 00:06:49,407
okay, almost anytime you're
gonna do something on
146
00:06:49,476 --> 00:06:52,377
the network, it's pretty
expensive. Because on a phone,
147
00:06:52,446 --> 00:06:54,913
maybe you only have a cellular
connection, it's a bad
148
00:06:54,982 --> 00:06:57,515
connection, you're out in
a country road somewhere,
149
00:06:57,584 --> 00:06:59,917
it's barely getting any.
So it could be really quite
150
00:06:59,986 --> 00:07:02,921
expensive. For the same
reason, these expensive
151
00:07:02,989 --> 00:07:06,491
things, we almost always do
them in the background. And
152
00:07:06,560 --> 00:07:08,292
on Wednesday, I'm gonna talk
all about how to put things in
153
00:07:08,361 --> 00:07:12,664
the background. That's because
it is absolutely primary goal,
154
00:07:12,732 --> 00:07:15,300
primary thing you
must do with any
155
00:07:15,368 --> 00:07:20,438
iOS app is you never block
the user interface experience.
156
00:07:20,507 --> 00:07:23,308
Users always have to be
able to touch on things,
157
00:07:23,377 --> 00:07:26,944
swipe things around.
If they swipe or touch and
158
00:07:27,013 --> 00:07:30,348
your app is frozen, they will
not use your app, believe me.
159
00:07:30,417 --> 00:07:32,983
Okay, so that is of primary
importance. And the way we do
160
00:07:33,052 --> 00:07:35,653
that is by putting things that
would block, like waiting for
161
00:07:35,722 --> 00:07:38,723
something on the network, off
the main queue, we call it,
162
00:07:38,792 --> 00:07:41,459
off the main thread into
these background processes.
163
00:07:41,528 --> 00:07:43,294
So we'll talk all about
that on Wednesday, but
164
00:07:43,362 --> 00:07:47,298
viewDidAppear is a great place
to kick those things off.
165
00:07:47,366 --> 00:07:50,334
Because you don't wanna waste
their cellular data usage
166
00:07:50,403 --> 00:07:53,137
fetching an image if you're
not actually gonna appear.
167
00:07:53,206 --> 00:07:55,673
Now what this means though is
since you're kicking this off
168
00:07:55,742 --> 00:07:58,943
in the background, when you've
already appeared on screen,
169
00:07:59,012 --> 00:08:03,614
you have to design your UIs So
that they always work even if
170
00:08:03,683 --> 00:08:07,652
the expensive thing has not
come back yet. Do you see why
171
00:08:07,720 --> 00:08:10,488
that is? That expensive thing
might take ten minutes or
172
00:08:10,557 --> 00:08:13,524
might never come back because
of bad network, for example.
173
00:08:13,593 --> 00:08:16,627
So you have to put place
holders or spinning wheels or
174
00:08:16,696 --> 00:08:18,362
loading, dot, dot, dot or
175
00:08:18,431 --> 00:08:21,999
some animation to shows the
user, yeah, I'm fetching your
176
00:08:22,068 --> 00:08:25,403
thing. I'm working on it. But
your UI still has to be fully
177
00:08:25,472 --> 00:08:27,938
responsive. If they're
in a navigation control,
178
00:08:28,007 --> 00:08:31,309
they have to be able to hit
back and give up. Okay, or
179
00:08:31,378 --> 00:08:34,545
they have to hit a tab at the
bottom and go to a different
180
00:08:34,614 --> 00:08:37,849
tab or whatever they wanna do.
So designing UIs,
181
00:08:37,918 --> 00:08:40,518
they're like that, take
a little bit of adjustment.
182
00:08:40,587 --> 00:08:43,154
So far, you haven't had
to do that probably. So
183
00:08:43,223 --> 00:08:44,822
you're mostly
thinking linearly.
184
00:08:44,891 --> 00:08:47,958
Okay, I am going to put
this image on screen, so
185
00:08:48,027 --> 00:08:50,161
I will go get the image.
Then I will put it on screen.
186
00:08:50,229 --> 00:08:51,929
Well, you can't think like
that. You have to think,
187
00:08:51,998 --> 00:08:54,932
I wanna put this on screen, so
I will put an image displaying
188
00:08:55,001 --> 00:08:58,503
thing on screen that is not
loaded, with a little spinner.
189
00:08:58,571 --> 00:09:00,772
And then, later, and the user
can do whatever they want.
190
00:09:00,841 --> 00:09:02,707
And then later when
the picture arrives,
191
00:09:02,776 --> 00:09:06,411
now I will update my UI to
show that image. Okay, so
192
00:09:06,479 --> 00:09:08,947
it's a little bit kind
of thinking with that
193
00:09:09,016 --> 00:09:11,849
other dimension,
that dimension of time.
194
00:09:11,918 --> 00:09:16,053
Okay, all right, so there's
also view will disappear.
195
00:09:16,122 --> 00:09:18,523
You get this right
before you go away.
196
00:09:18,592 --> 00:09:21,893
This is a great place
to undo what you did
197
00:09:21,961 --> 00:09:25,329
when view did appear.
So if you started a timer or
198
00:09:25,398 --> 00:09:28,833
start some animation or
started watching GPS, or
199
00:09:28,902 --> 00:09:33,037
something like that, this is a
good place to stop doing that.
200
00:09:33,105 --> 00:09:36,875
Because you now know that
you're going to disappear. And
201
00:09:36,943 --> 00:09:39,143
then when you reappear,
you'll turn it back on and
202
00:09:39,212 --> 00:09:42,346
view did appear. So they kinda
work together, these two. And
203