ViewDidLoad getting called twice on rootViewController at launch?

Weird. I haven't seen this particular case, but in general, you ought to assume that viewDidLoad can be called multiple times. It'll get called whenever a nib file that references that controller gets loaded.

Weird. I haven't seen this particular case, but in general, you ought to assume that viewDidLoad can be called multiple times. It'll get called whenever a nib file that references that controller gets loaded.

For a simple app with only one nib, that shouldn't happen. But in a more-complex app that can load and unload view controllers, this happens all the time.

Yeah but what's happening is my objects are getting re-built and i'm getting two of each. I could but checks in to see if they're! = nil but I haven't had to do this in previous apps.

Something weird is going on here. Any ideas what might be calling it irregularly so I look deeper for the cause? – Meltemi Jul 4 '09 at 0:01 In the second case, it's unarchiving an NSArray, which apparently has a reference to the RootViewController.Do you maybe have more than one reference to a RootViewController in the same nib file?

– Mark Bessey Jul 4 '09 at 0:09 I agree, check to see if you have defined 2 instances of RootViewController – Panagiotis Korros Jul 4 '09 at 5:19 1 While Mark is basically right, viewDidUnload and viewDidLoad are counterpart methods. At least it should be possible to assume that for every viewDidLoad call there will be another viewDidUnload call. This way you could do the tear down for the set up in viewDidLoad.

When latter is called multiple times in sequence without a viewDidUnload in between things can go horribly wrong. This shouldn’t happen at all. – Rafael Jul 4 '09 at 14:31 This is incorrect.

ViewDidUnload is only called in low memory situations. It is not guaranteed to be called for every call to viewDidLoad. – cduhn Jul 4 '09 at 12:26.

I had this same issue when my app was first launching. What I found was that in my MainWindow. Xib file, I was setting both my App Delegate's viewController outlet, and my Window's rootViewController outlet to my root view controller.

When you build a View Based project file in Xcode, your App Delegate's didFinishLaunchingWithOptions will be pre-populated with: self.window. RootViewController = self. ViewController; self.

Window makeKeyAndVisible; return YES; I believe that the self. ViewController ivar is instantiated from MainWindow. Xib before didFinishLaunchingWithOptions gets called.

Then the pre-populated code above sets the Window's rootViewController. So if, in conjunction, you specify the rootViewController outlet for the Window in your MainWindow. Xib file, your root view controller will actually be created twice and added as the Window's root view controller two times.

This is exactly the problem that I was having. – David Oct 5 at 23:45.

I did some debugging and here's what I found about the ViewController loading order: initWithNibName:bundle: self = , retainedOutlet = 0x0 loadView >>> self = , retainedOutlet = 0x0 initWithCoder: self = , retainedOutlet = 0x0 initWithCoder: self = , retainedOutlet = 0x0 setView: self = , retainedOutlet = 0x0 setRetainedOutlet: self = , retainedOutlet = 0x1613c40 viewDidLoad self = , retainedOutlet = 0x0 awakeFromNib self = , retainedOutlet = 0x0 loadView The good news is that in this copy, retained outlets are not configured, so you can use this as a test to know if you should initialize variables, call other methods, and most importantly, if you should release and destroy objects during dealloc. Key takeaway: the real viewController will have its retained IBOutlet properties configured. If you are in an overridden method that is getting called multiple times, just check one of your retained IBOutlet properties for NULL.

If they are NULL, then return immediately. Anybody got any clues as to why this is happening this way? Side effect of this: you can't use awakeFromNib reliably.

You can't assume viewDidLoad will be called only once. If you are initializing objects and want a guarantee do the initialization either in the init method or if you are loading from a nib file from the awakeFromNib method.

I had a similar problem and it was a result of renaming my XIB file and its ViewController class (File Owner). Don't do that -- as it really got the views and delegates misdefined inside the XML and it was not recoverable. Meanwhile, I had a reference to the load the original VC that was supposed to be my new VC.

I believe that caused the parent to recreate itself and then the VC I was really tried to invoke. Basically, I created an indirect recursion to the VC that has x2 viewDidLoad entries in my trace. I don't think there is any valid reason for x2 viewDidLoad as it is a genesis and can invoke other initialization with the wrong assumed pre-conditions.

Every time I have seen the x2 viewDidLoad, it was a coding error on my part -- quite often when I was refactoring and moving VC classes around. If there is a valid reason for more than on viewDidLoad call, please someone (Apple Dev are you listening) explain it in technical detail -- I have been searching for that answer for months now.

Had same issue after renaming some files in the project. Ended up creating a new project. – Bemmu Mar 29 at 15:21.

I ran into the same problem as I was redesigning a ViewController from scratch to get rid of the XIB file and to make the class reusable. I had this second ViewController instance which would receive a viewDidLoad message followed by a dealloc message. I found out this was the result of the loadView method not being redefined in the ViewController.

The default loadView called awakeFromNib, with the nibName property set to the class name. Even though I had removed the XIB file from the project, it was still in the application directory on the simulator. So even though you could just reset the contents and settings of the simulator to get rid of the second viewDidLoad, a better way may be to just redefine loadView like this: - (void)loadView { self.

View = UIView alloc initWithFrame:UIScreen mainScreen applicationFrame autorelease; self.view. AutoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin; } It makes sense if you consider the documentation for the UIViewController's view property: If you access this property and its value is currently nil, the view controller automatically calls the loadView method and returns the resulting view. The default loadView method attempts to load the view from the nib file associated with the view controller (if any).

If your view controller does not have an associated nib file, you should override the loadView method and use it to create the root view and all of its subviews.

This happened to me when I merged a project from the storyboard to the old way using xibs for constructing views. The main reason for switching back was the fact I couldn't properly put up a modal view properly. The way I usually do it is by having a delegate method from a UIButton construct an instance of a certain viewcontroller, set some of its properties (the most import one being the delegate so I can properly dismiss the modal view controller again) and then present it in a modal way.In the new storyboard way, this is supposedly done with a segue.

Customizing the transition is only doable by making a custom class that extends the UIStoryboardSegue class. I find this way too much hassle compared to the simple way it used to be so I merged back. How did this cause me to have a viewcontroller load twice?

When transferring the code from the storyboard project to the xib project, I made a couple of xibs (one for each ViewController) and copied the viewcontroller object from the storyboard. This led to a xib with in it not a viw, but a viewcontroller; meaning I had put a viewcontroller in a viewcontroller (since the file's owner is also an instance of the viewcontroller). I don't think in your case that you had this problem but I hope it maybe helps someone some day.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions