Swift 手动调用 ViewDidLoad

huangapple go评论130阅读模式
英文:

Swift Call ViewDidLoad Manually

问题

我有一个关于我的应用程序的问题。我在顶级视图控制器中保存数据,然后返回到主视图控制器。我的问题是,当我关闭顶部视图时,主视图的viewDidLoad方法不会再次调用。由于我在第二个视图中保存了信息,而我的第一个视图在关闭后需要更新,我需要调用viewDidLoad。如果您有任何关于如何做到这一点的想法,我将不胜感激。我已经看到了Objective C的类似问题,但没有看到Swift的。

我尝试寻找答案,但没有找到有用的信息。

英文:

I'm having a problem with my app. I am saving data in the top ViewController and coming back to main ViewController. My problem is that when I dismiss the top view, viewDidLoad for the main view isn't called again. Since I'm saving information in my second view, and my first view needs to update when dismissed, I need viewDidLoad to be called. If you have any ideas on how to do this, any help is appreciated. I've seen the same question for Objective C, but not for swift

I've tried to find answers, but nothing helped

答案1

得分: 1

永远不要尝试手动调用 UIViewController 的生命周期方法。这些方法只应由系统调用。

如果您需要在每次视图控制器出现在屏幕上时执行某些操作,请使用 viewWillAppearviewDidAppear

viewDidLoad 仅在每个 UIViewController 实例的生命周期内调用一次,此后控制器的视图已加载到内存中。

有关 UIViewController 生命周期的更多信息,请参阅了解 iOS UIViewController 生命周期

英文:

You should never, ever try to call UIViewController lifecycle methods manually. Those methods should only be called by the system.

If you need to execute something every time your view controller appears on the screen, use viewWillAppear or viewWillAppear instead.

viewDidLoad is only called 1x for the lifetime of each UIViewController instance, after the controller's view has been loaded to memory.

For more information on UIViewController lifecycle, see Looking to understand the iOS UIViewController lifecycle.

huangapple
  • 本文由 发表于 2023年3月9日 21:44:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定