0
votes

I know there are lot of discussions around this warning and we should be presenting the view controller only after presenting view controller is completely presented and added in view hierarchy. All these rules are followed and I am still getting this warning when presenting second modal view controller.

This the complete sequence of actions happening here:

  1. On app launch, initialized and added base view controller to window.
  2. From base view controller, on tap on a button, modally presented first view controller.
  3. From first view controller, on tap on a button, modally presented second view controller. Once dismissed, I need to show first view controller so I am not dismissing first VC and then presenting second VC from base VC.

Warning in console:

2015-03-11 13:15:43.467 MyApp[597:84839] Warning: Attempt to present <MyCustomNavigationViewController: 0x15d6a8920> on <MyFirstModalViewController: 0x15d67cc60> whose view is not in the window hierarchy!

Thoughts?

Below is my code [Not showing button handlers; took code from within the calling methods]:

// Present base view controller
self.window.backgroundColor = [UIColor whiteColor];
self.primaryViewController = [[MyParentViewController alloc] init];
self.navigationController = [[MyCustomNavigationViewController alloc] initWithRootViewController:self.primaryViewController];
[self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];

// Present first view controller modally from base view controller
MyFirstModalViewController *firstModal = [[MyFirstModalViewController alloc] init];
MyCustomNavigationViewController *aNavigationController = [[MyCustomNavigationViewController alloc] initWithRootViewController:firstModal];
[aNavigationController.navigationBar setBarStyle:UIBarStyleBlack];
[self presentViewController:aNavigationController animated:YES completion:nil];

// Present second view controller modally from first view controller: On user tap on a button
MySecondModalViewController *secondModal = [[MySecondModalViewController alloc] init];
MyCustomNavigationViewController *aNavigationController = [[MyCustomNavigationViewController alloc] initWithRootViewController:secondModal];
[aNavigationController.navigationBar setBarStyle:UIBarStyleBlack];
[self presentViewController:aNavigationController animated:YES completion:nil];
1
I'm not sure if this is causing the problem, but you shouldn't be inserting the navigation controller's view in the window; adding it as the root view controller adds it as a subview for you. You should delete that line. Otherwise, I don't see any errors in your code. You should post the actual error message.rdelmar
@rdelmar I have added the actual console warning. Also, even after removing the insertion of navigation controller's view in the window, warning persists.Abhinav
I can't duplicate your problem. Are you doing anything in the view controller life cycle methods (viewDidLoad, viewWillAppear, etc.) that is presenting or dismissing a view controller? Do you have a storyboard, and if so, do you have any segues set up?rdelmar
I am not doing any view controller presentation in any of the view initialization methods like viewWillAppear etc. I am not using storyboard and all the presentation is done via code. The code to present new controller is written in a button handler.Abhinav
And you're not doing any dismissing of any controllers in code either?rdelmar

1 Answers

0
votes

If the code you're displaying in your question is in the exact order that it runs on app launch, it's because it's trying to animate the second Modal viewcontroller (the third actual view controller being presented) when the first modal has not finished animating.

On these two lines - [self presentViewController:aNavigationController animated:YES completion:nil]; set animated:NO. This will make those views appear instantaneously, and may remove the error you're getting.

If you need the animation, and you need the views to all open instantaneously like you've written, then you might want to implement a delay before the third viewcontroller (second modal) is called.

You say you're calling these views from buttons? The code indicates otherwise.