0

In my storyboard, I have two UIViewControllers. The initial view controller is a loading/splash screen that shows a simple image. The second view controller is a UIWebView.

I am trying to replace the loading view controller with the UIWebView view controller after the app fully loads.

Inside my loading view controller, I have this code to present the UIWebView:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"WebView"];

[self presentViewController:viewController animated:NO completion:^(void){}];

However, this code throws the following error:

Warning: Attempt to present <WebController: 0x145617960> on <LoadingController: 0x145610c40> whose view is not in the window hierarchy!

Am I doing something wrong?

Thanks for any help/suggestions.

Pat
  • 649
  • 1
  • 8
  • 24

1 Answers1

0

If your block of code:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"WebView"];

[self presentViewController:viewController animated:NO completion:^(void){}];

is in a method of you loading view controller that is called when when the view controller is loaded such as viewDidLoad, viewDidAppear:, or any similar methods, the problem is because your web view controller has nothing to present, as self in presentViewController refers to the loading view controller. My suggestion would be to put it in the viewDidAppear: of your loading view controller and dispatch after a second to ensure your loading view controller is actually capable of presenting it

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"WebView"];

    [self presentViewController:viewController animated:NO completion:^(void){}];
});
Chris
  • 7,270
  • 19
  • 66
  • 110