Viewdidload Is Called Twice

viewDidLoad called twice, using navigation controller

When you create your navigation view controller from the storyboard, this already contains it's rootViewController (which must not to be confused with the rootViewController of the UIWindow). I guess this is your HomeVC (in the storyboard). So, the storyboard magic already creates HomeVC, and you do not have to create it manually in didFinishLaunchingWithOptions.

If you have specify the storyboard as your main interface in the project's/target's properties, you do not need any creational code in didFinishLaunchingWithOptions and just let the framework perform the magic.

If you want to do this programatically, then - in the storyboard - you should remove the navigation controller, and create it manually (not via instantiateViewController) in didFinishLaunchingWithOptions. You would also add the appropriate root view controller here (instantiated from the storyboard), maybe like this:

let storyboard = UIStoryboard(name: "Main", bundle: nil)

if UserDefaults.standard.object(forKey: USERID) != nil {
viewController = storyboard.instantiateViewController(withIdentifier: "HomeVC_ID") as! HomeVC
} else {
viewController = storyboard.instantiateViewController(withIdentifier: "LoginVC_ID") as! LoginVC
}
let navigationController = UINavigationController(rootViewController:viewController)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()

Why is viewDidLoad being called twice

As suggested by @Simon Goldeen and @pbasdf above - the issue was that I was pushing 2 map view controllers onto the stack. There was and old segue that I was using previously for debugging that was causing the issue. I deleted all segues to this map view and instead segued to the map view as follows:

SubmitPointMapViewController *vc = [[SubmitPointMapViewController alloc] init];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
vc = (SubmitPointMapViewController *)[storyboard instantiateViewControllerWithIdentifier:@"SubmitPointMapViewControllerStoryboardID"];
[[self navigationController] pushViewController:vc animated:YES];

Thought I would post my answer here in case anyone else had the same issue.

All credit to @Simon Goldeen and @pbasdf for helping me troubleshoot this issue.

ViewDidLoad gets called twice iOS 13+

With ios 13 don't do anything inside didFinishLaunchingWithOptions Try

 if #available(iOS 13.0, *) { }
else {
window = UIWindow()
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
window?.backgroundColor = .main
window?.rootViewController = UINavigationController(rootViewController: ViewController())
}

Also you need to comment either

window?.rootViewController = ViewController()

or

window?.rootViewController = UINavigationController(rootViewController: ViewController())

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.

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.

viewDidAppear called twice on the same instance, but only the first time this class loads form NIB

You should never rely on -viewWillAppear:/-viewDidAppear: being called appropriately balanced with the disappear variants. While the system view controllers will do the best they can to always bracket the calls properly, I don't know if they ever guarantee it, and certainly when using custom view controllers you can find situations where these can be called multiple times.

In short, your -viewWillAppear:/-viewDidAppear: methods should be idempotent, meaning if -viewDidAppear: is called twice in a row on your controller, it should behave properly. If you want to load custom views, you may want to do that in -viewDidLoad instead and then simply put the on-screen (if they aren't already) in -viewDidAppear:.

You could also put a breakpoint in your -viewDidAppear: method to see why it's being called twice the first time it shows up.

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

It looks like a bug, because it’s only reproducible in Xcode 4. I will file a bug.

See also http://shurl.at/5u (Apple Developer Forums)

Why does a function in my iOS app get called twice?

You have two instances of ThirdViewController when you don't mean to.

This error is very telling:

2018-09-29 23:08:37.279170+0200 MyProject[57733:4748212] Warning: Attempt to present <MyProject.ThirdViewController: 0x7fc709125890> on <MyProject.SecondViewController: 0x7fc70900fcd0> whose view is not in the window hierarchy!

This is telling you that SecondViewController is trying to create ThirdViewController when SecondViewController is not even on the screen. This suggests that the mistake is in SecondViewController (perhaps observing notifications or other behaviors when not on screen). It's possible of course that you also have two instances of SecondViewController.

I suspect you're trying to build all of this by hand rather than letting Storyboards do the work for you. That's fine, but these kinds of mistakes are a bit more common in that case. The best way to debug this further is to set some breakpoints and carefully check the address of the objects (0x7fc709125890 for example). You'll need to hunt down where you're creating an extra one.



Related Topics



Leave a reply



Submit