iOS 转场时 appear 与 disappear 的调用顺序
最近几天看了好多人的面经,时不时就看到一个问题:
在A页面跳转到B页面的过程中 A 的 viewDidDisappear 方法和 B 的 viewDidAppear 谁先调用?
其实我以前一直理所当然的认为它们的顺序就是 A-viewDidDisappear
--> B-viewDidAppear
,至于为什么会这么想,就是感觉
今天写了个Demo去测试了一下,发现完全不是自己想象的那样,每一种转场(Push,Present,TabBar切换)都是不一样的。
测试
我自定义了两个类,继承自UIViewController,为了方便观察分别叫做A_ViewController
,B_ViewController
,然后重写了一系列 appear
和 disappear
方法。
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"A view will appear %@",self);
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"A view did appear %@",self);
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
NSLog(@"A view will disappear %@",self);
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear: animated];
NSLog(@"A view did disappear %@",self);
}
然后分别用 push, present, 以及 tabbar 的切换来测试,然后查看打印的结果。
Push & Pop
--------------------------PUSH-----------------------------
A view will disappear <A_ViewController: 0x7fa11067a730>
B view will appear <B_ViewController: 0x7fa110516530>
A view did disappear <A_ViewController: 0x7fa11067a730>
B view did appear <B_ViewController: 0x7fa110516530>
--------------------------POP-----------------------------
B view will disappear <B_ViewController: 0x7fa110516530>
A view will appear <A_ViewController: 0x7fa11067a730>
B view did disappear <B_ViewController: 0x7fa110516530>
A view did appear <A_ViewController: 0x7fa11067a730>
在这里的调用顺序是和我之前预想的一样的,也就是
A-willDisappear
--> B-willAppear
--> A-didDisappear
--> B-didAppear
Present & Dismiss
在模态的转场中就有一些不一样的事了。我们知道模态转场有好多种style,其中有两种我们需要特别注意的UIModalPresentationCustom
,UIModalPresentationOverFullScreen
。我们先看一下除了这两种style以外的转场,他们的调用顺序是怎样的。
-----------------------NormalPresent------------------------
A view will disappear <A_ViewController: 0x7fc2a14ac490>
B view will appear <B_ViewController: 0x7fc2a152a880>
B view did appear <B_ViewController: 0x7fc2a152a880>
A view did disappear <A_ViewController: 0x7fc2a14ac490>
-------------------------DISMISS----------------------------
B view will disappear <B_ViewController: 0x7fc2a152a880>
A view will appear <A_ViewController: 0x7fc2a14ac490>
A view did appear <A_ViewController: 0x7fc2a14ac490>
B view did disappear <B_ViewController: 0x7fc2a152a880>
这里其实已经和push的转场不一样了。普通模态的转场的顺序是这样的
A-willDisappear
--> B-willAppear
--> B-didAppear
--> A-didDisappear
再看看前面提到的那两种特殊的模态转场。因为在这两种方式的转场下A的view是不会消失的,所以所有与A有关的 appear 或 disappear 方法都不会被调用
-----------------------CustomPresent------------------------
B view will appear <B_ViewController: 0x7f960249bac0>
B view did appear <B_ViewController: 0x7f960249bac0>
-------------------------DISMISS----------------------------
B view will disappear <B_ViewController: 0x7f960249bac0>
B view did disappear <B_ViewController: 0x7f960249bac0>
所以在这两种style下只有B-willAppear
--> B-didAppear
TabBar切换
TabBar 的切换也是一个很神奇的顺序
B view will appear <B_ViewController: 0x7f88cbed7160>
A view will disappear <A_ViewController: 0x7f88cbed6680>
A view did disappear <A_ViewController: 0x7f88cbed6680>
B view did appear <B_ViewController: 0x7f88cbed7160>
与push不同的是,TabBar下,会先调用 B 的 willAppear,再调用 A 的 willDisappear
所以TabBar的顺序是这样的:
B-willAppear
--> A-willDisappear
--> A-didDisappear
--> B-didAppear
总结
在每一种转场下,appear 与 disappear 都有一些不一样的顺序,一定要分清楚,不能一概而论。
Push: A-willDisappear
-->B-willAppear
-->A-didDisappear
-->B-didAppear
Present: A-willDisappear
-->B-willAppear
-->B-didAppear
-->A-didDisappear
CustomPresent: B-willAppear
-->B-didAppear
TabBar: B-willAppear
-->A-willDisappear
-->A-didDisappear
-->B-didAppear
疑惑
为什么要把这个过程弄的这么复杂?目前还没有想法,如果有知道的朋友,希望告知。