viewDidLoad is called twice
Asked Answered
F

6

8

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?

Farquhar answered 16/8, 2011 at 14:15 Comment(0)
N
12

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.

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

Nonconformance answered 16/8, 2011 at 14:21 Comment(3)
Good Answer. But I am not sure about ViewDidLoad will be called multiple times, unless there are some hidden references. But I may be wrong.Diamond
The viewDidLoad will be called automatically whenever the view is needed but not there. Using controller.view or [controller view] would result in viewDidLoad being called if the view was originally nil. If the view was not set in instanciateWithOwner, or unloaded afterwards it could be the reason it was called again.Nonconformance
what UI behavior would trigger an extra call to viewDidLoad?Gibber
A
5

Is this the same problem?

Why is viewDidLoad called twice when the rootViewController property of UIWindow is set?

Looks like it might be a bug in XCode 4.

Asymptote answered 16/8, 2011 at 14:23 Comment(0)
D
1

You might have to check the object building mechanism. If there is only one nib file with reference to the controller, then this method should not be called multiple times. (unless if the object is getting rebuilt).

I think you might have to make your code within ViewDidLoad idemPotent. It is always better to make sure, that framework call back methods make this assumption.

Diamond answered 16/8, 2011 at 14:23 Comment(0)
S
0

There are two possibilities, whereby this issue happened in my iOS device frequently.

Rule #1: Do not call any view related setup in [init] function, all view related setup must be done in viewDidLoad and viewWillAppear.

Rule #2: Check viewDidLoad and viewWillAppear, are they calling correct super function? For example viewDidLoad -> super viewDidLoad and so on.

Hope this helps.

Sopher answered 16/10, 2013 at 8:29 Comment(0)
L
0

In my case, I used self.view (once) in viewDidLoad while calling viewDidLoad in my unit tests. This resulted in two calls. However, when I replaced [testedViewController viewDidLoad] with [testedViewController view], the double call problem was gone.

Leviathan answered 14/5, 2014 at 14:40 Comment(0)
A
0

Debugging this showed that viewDidLoad was called a second time by @IBInspectable. The root controller is a UITabbarController. @IBInspectable was setting the tab in the storyboard. Not sure if this is a UIKit bug but try checking for this. You should never need to check viewDidLoad for double calls if your project is setup correctly.

Abijah answered 27/1, 2021 at 19:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.