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 转换时调用的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)