viewDidLoad 实际上是在每次有 segue 转换时调用的

viewDidLoad is in fact called every time there is a segue transition(viewDidLoad 实际上是在每次有 segue 转换时调用的)

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

问题描述

我看过很多关于堆栈溢出的帖子,说控制器的 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.

这根本不是我看到的!我整理了一个简单的测试来强调这一点: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:

  • 何时调用 viewDidLoad?
  • UIViewController viewDidLoad vs. viewWillAppear:什么是分工合理吗?
  • 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.

我错过了什么吗?

推荐答案

我相信 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.

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

本文标题为:viewDidLoad 实际上是在每次有 segue 转换时调用的

基础教程推荐