Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 4 - viewDidLoad issue

Is anyone else having issues with Xcode 4 where viewDidLoad is being called twice? I have run the same project in both Xcode 3.2 and Xcode 4 and it only acts up in Xcode 4.

like image 627
Renegade428 Avatar asked Mar 11 '11 21:03

Renegade428


People also ask

Is viewDidLoad only called once?

viewDidLoad method is called only once in ViewController lifecycle. The reason retrieveMessage() is called in viewDidLoad because it's adding observer to start listening for received and sent message.

Does viewDidLoad get called before viewWillAppear?

viewWillAppear(_:)Always called after viewDidLoad (for obvious reasons, if you think about it), and just before the view appears on the screen to the user, viewWillAppear is called.

What is the difference between viewDidLoad and viewWillAppear?

viewDidLoad: Whatever processing you have that needs to be done once. viewWilLAppear: Whatever processing that needs to change every time the page is loaded. Labels, icons, button titles or most dataInputedByDeveloper usually don't change.

How many times does viewDidLoad get called?

-viewDidLoad will be called once whenever the view controller needs to load its view hierarchy. Obviously, that'll happen the first time that the controller accesses its view. If the view controller later unloads its view, then -viewDidLoad will be called again the next time the view is loaded.


1 Answers

After researching this on the Apple Developer forums, it seems that in some cases Xcode 4 creates bugged Interface Builder NIBs. The effect is that the application's rootViewController gets loaded in twice, which really screws things up. The same project loaded in Xcode 3 won't exhibit the problem.

In my universal app, it only affected the iPad NIB. The iPhone was fine.

I was able to solve this issue by:

  • Removing the rootViewController connection in Interface Builder (this causes the app to load with window.rootViewController = nil)
  • In viewDidLoad for the main controller (the one that was being loaded twice), I then manually assign appDelegate.window.rootViewController = self

So far this seems to have the desired effect.

like image 83
Jeremy Fuller Avatar answered Nov 19 '22 19:11

Jeremy Fuller