viewDidLoad 被调用两次 [英] viewDidLoad is called twice

查看:21
本文介绍了viewDidLoad 被调用两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在视图控制器中的 viewDidLoad 被调用了两次.一次通过 [UIViewController View],第二次通过 [UINib instanciateWithOwner:Options].为什么会这样?可以预防吗?

My viewDidLoad in a view controller is called twice. Once by [UIViewController View] and a second time by [UINib instanciateWithOwner:Options]. Why is this happening? Can it be prevented?

推荐答案

您放入 viewDidLoad 中的任何代码都应该能够多次运行而不会出现任何问题.如果您的代码只需要为您的控制器运行一次,请使用 -awakeFromNib.原因是因为视图控制器的视图可以多次卸载和加载.viewDidLoad 中的代码应该只修改 UI 以反映当前状态.

Any code you put inside of viewDidLoad should be able to run multiple times with out any issues. If you have code that only needs to run once for your controller use -awakeFromNib. The reason is because the view of the view controller can be unloaded and loaded multiple times. The code inside of viewDidLoad should only modify the UI to reflect the current state.

既然我已经解决了这个问题,那么您的特定问题似乎是一个错误.请参阅Ned 的回答.

Now that I got that out of the way, your particular issue looks to be a bug. See Ned's answer.

这篇关于viewDidLoad 被调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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