viewDidLoad is in fact called every time there is a segue transition(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 转换时调用的
基础教程推荐
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01