简体   繁体   English

viewDidLoad 被调用两次

[英]viewDidLoad is called twice

My viewDidLoad in a view controller is called twice.我在视图 controller 中的viewDidLoad被调用了两次。 Once by [UIViewController View] and a second time by [UINib instanciateWithOwner:Options] .一次是[UIViewController View] ,第二次是[UINib instanciateWithOwner:Options] Why is this happening?为什么会这样? Can it be prevented?可以预防吗?

Any code you put inside of viewDidLoad should be able to run multiple times with out any issues.您放入viewDidLoad中的任何代码都应该能够多次运行而不会出现任何问题。 If you have code that only needs to run once for your controller use -awakeFromNib .如果您的代码只需要为您的 controller 运行一次,请使用-awakeFromNib The reason is because the view of the view controller can be unloaded and loaded multiple times.原因是因为视图controller的视图可以多次卸载加载。 The code inside of viewDidLoad should only modify the UI to reflect the current state. viewDidLoad内部的代码应该只修改 UI 以反映当前的 state。

Now that I got that out of the way, your particular issue looks to be a bug.现在我已经解决了这个问题,您的特定问题看起来是一个错误。 See Ned's answer .内德的回答

Is this the same problem?这是同样的问题吗?

Why is viewDidLoad called twice when the rootViewController property of UIWindow is set? 为什么设置 UIWindow 的 rootViewController 属性时 viewDidLoad 会被调用两次?

Looks like it might be a bug in XCode 4.看起来这可能是 XCode 4 中的错误。

You might have to check the object building mechanism.您可能需要检查 object 构建机制。 If there is only one nib file with reference to the controller, then this method should not be called multiple times.如果只有一个nib文件参考controller,那么这个方法不应该多次调用。 (unless if the object is getting rebuilt). (除非 object 正在重建)。

I think you might have to make your code within ViewDidLoad idemPotent.我认为您可能必须在 ViewDidLoad idemPotent 中编写代码。 It is always better to make sure, that framework call back methods make this assumption.确保框架回调方法做出这种假设总是更好。

There are two possibilities, whereby this issue happened in my iOS device frequently.有两种可能性,这个问题经常发生在我的 iOS 设备中。

Rule #1: Do not call any view related setup in [init] function, all view related setup must be done in viewDidLoad and viewWillAppear.规则 #1:不要在 [init] function 中调用任何与视图相关的设置,所有与视图相关的设置都必须在 viewDidLoad 和 viewWillAppear 中完成。

Rule #2: Check viewDidLoad and viewWillAppear, are they calling correct super function?规则 #2:检查 viewDidLoad 和 viewWillAppear,它们是否调用了正确的超级 function? For example viewDidLoad -> super viewDidLoad and so on.例如 viewDidLoad -> 超级 viewDidLoad 等等。

Hope this helps.希望这可以帮助。

In my case, I used self.view (once) in viewDidLoad while calling viewDidLoad in my unit tests.就我而言,我在单元测试中调用 viewDidLoad 时在 viewDidLoad 中使用了 self.view (一次)。 This resulted in two calls.这导致了两个电话。 However, when I replaced [testedViewController viewDidLoad] with [testedViewController view], the double call problem was gone.但是,当我将 [testedViewController viewDidLoad] 替换为 [testedViewController view] 时,双重调用问题就消失了。

Debugging this showed that viewDidLoad was called a second time by @IBInspectable.对此进行调试表明,@IBInspectable 再次调用了 viewDidLoad。 The root controller is a UITabbarController.根 controller 是一个 UITabbarController。 @IBInspectable was setting the tab in the storyboard. @IBInspectable 正在设置 storyboard 中的选项卡。 Not sure if this is a UIKit bug but try checking for this.不确定这是否是 UIKit 错误,但请尝试检查。 You should never need to check viewDidLoad for double calls if your project is setup correctly.如果您的项目设置正确,则永远不需要检查 viewDidLoad 是否有双重调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM