您是否熟悉viewWillAppear,viewDidAppear,viewDidLoad,viewWillDisappear和viewDidDisappear?

在此之前,让我问您viewWillAppearviewDidAppearviewDidLoadviewWillDisappearviewDidDisappear的运行顺序是什么?

如果您知道答案,请关闭此网页并进行游戏。 🙂

因此,编写了一个简单的片段代码来验证这一点:

Example code:

Objective-C代码
适用于Swift爱好者的Swift代码

运行结果:

 2017-04-14 08:18:18.923 TestBlock[3940:264012] viewDidLoad is running 
2017-04-14 08:18:18.924 TestBlock[3940:264012] viewWillAppear is running
2017-04-14 08:18:18.946 TestBlock[3940:264012] viewDidAppear is running
2017-04-14 08:18:22.187 TestBlock[3940:264012] viewWillDisappear is running
2017-04-14 08:18:22.689 TestBlock[3940:264012] viewDidDisappear is running

单击按钮后,您将看到顺序:

  1. viewDidLoad
  2. viewWillAppear
  3. viewDidAppear
  4. viewWillDisappear
  5. viewDidDisappear