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

查看:484
本文介绍了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.

现在我已经知道了顺便说一句,你的特定问题看起来像是一个bug。请参阅 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天全站免登陆