实际上,每次有segue转换时都会调用viewDidLoad [英] viewDidLoad is in fact called every time there is a segue transition

查看:97
本文介绍了实际上,每次有segue转换时都会调用viewDidLoad的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到很多关于堆栈溢出的帖子说明控制器的viewDidLoad方法只在第一次访问控制器时被调用,并不一定每次都被调用,但总是至少一次。

I have seen a lot of posts on stack overflow stating that the viewDidLoad method of controllers is only called the first time the controller is accessed and not necessarily every time but always at least once.

这不是我所看到的!我整理了一个简单的测试来强调这一点:$ b​​ $ b https://github.com/imuz/ViewDidLoadTest

This is not what I am seeing at all! I put together a simple test to highlight this: https://github.com/imuz/ViewDidLoadTest

似乎导航控制器segues和模态视图始终调用viewDidLoad。唯一没有被调用的时间是在标签之间切换。

It seems for navigation controller segues and modal views viewDidLoad is always called. The only time it is not called is when switching between tabs.

viewDidLoad的每个解释我都会发现与此相矛盾:

Every explanation of viewDidLoad I can find contradicts this:

  • When is viewDidLoad called?
  • UIViewController viewDidLoad vs. viewWillAppear: What is the proper division of labor?
  • http://www.manning-sandbox.com/thread.jspa?threadID=41506

苹果自己的文档表明只有在内存不足时才会卸载视图。

And apples own documentation indicate that a view is only unloaded when memory is low.

我目前正在viewDidLoad中进行初始化,并假设每次segue转换都会调用它。

I am currently doing initialisation in viewDidLoad making the assumption that it is called with every segue transition.

我在这里遗漏了什么吗?

Am I missing something here?

推荐答案

我相信Apple文档描述了视图控制器未被释放的情况。如果您使用segue,那么您将导致新目标控制器的实例化,并且作为新对象,它需要加载视图。

I believe the Apple documentation is describing a situation where the view controller is not being deallocated. If you use a segue, then you are causing the instantiation of a new destination controller and, being a new object, it needs to load a view.

在基于xib的情况下应用程序,我有时会缓存一个我知道可能经常重复使用的控制器对象。在这些情况下,他们在必须加载视图时表现与文档保持一致。

In xib-based apps, I have sometimes cached a controller object that I knew I might re-use frequently. In those cases, they behaved in keeping with the documentation in terms of when a view had to be loaded.

编辑:
在阅读您包含的链接时,我认为他们没有任何矛盾。他们也在谈论在视图控制器对象的生命周期中发生的事情。

On reading the links you included, I don't see any contradiction in them. They, too, are talking about things that happen during the lifespan of a view controller object.

这篇关于实际上,每次有segue转换时都会调用viewDidLoad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆