viewDidLoad is called twice

saman01 picture saman01 · Aug 16, 2011 · Viewed 9.7k times · Source

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?

Answer

Joe picture Joe · Aug 16, 2011

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.