0

I have tried to deal with this issue for too long. Please give me any of your thoughts.

I am presenting a View Controller from an SKScene by sending a Local Notification. [[NSNotificationCenter defaultCenter] postNotificationName:@"closeScene" object:nil];

The notification is handled in the beginning view controller.

- (void)viewDidLoad{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(closeScene) name:@"closeScene" object:Nil];
[super viewDidLoad];}

Then:

-(void)closeScene {
//Remove the SKView, or present another viewController here.
constructionViewController *view = [[constructionViewController alloc] init];
[self presentViewController:view animated:YES completion:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"closeScene" object:nil];

}

And every time the notification is sent, I get the warning:

 Warning: Attempt to present <constructinoViewController: 0x10ae64160> on <constructinoViewController: 0x10ab329d0> whose view is not in the window hierarchy!

Any help would more than appreciated. Thank you in advance!

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
user2918201
  • 52
  • 1
  • 7

1 Answers1

0

When is closeScene called, exactly - before or after viewDidAppear gets called?

The error you're seeing usually happens when you try to present a view controller before the parent is actually displayed on screen. Typically this is because you've called presentViewController before the parent's viewDidAppear method has been called.

Try adding a logging statement to viewDidAppear and see whether it appears in the logs before or after your 'view is not in the window hierarchy' error. If it appears after you'll know what the problem is, and how to fix it (make sure presentViewController is only called after the parent view controller has been displayed). If it's before you probably have an altogether different problem...

lxt
  • 31,146
  • 5
  • 78
  • 83
  • So the main scene, (let us call it x) is the first screen. From x, when you press a button, it presents the scene. The scene loads fine, and then when the game is over, closedScene is called from the scene. closedScene is a method found in x. So as far as if the viewDidAppear is being called, it should have been called at the beginning of the app opening, but then I do not believe it gets called again. – user2918201 Mar 16 '14 at 02:18
  • How are you adding the initial view to the view hierarchy - your app has a root view controller? – lxt Mar 16 '14 at 02:29
  • What do you mean by that? – user2918201 Mar 16 '14 at 02:42
  • How are you creating your view hierarchy on app launch - all iOS apps should have a root view controller, I'm wondering if maybe you don't have one set and that's what's causing the problems here. – lxt Mar 16 '14 at 03:12
  • I do not believe I do... how would I go about settings one? – user2918201 Mar 16 '14 at 17:19
  • Actually, I just set the root view controller like this: – user2918201 Mar 16 '14 at 17:25
  • constructinoViewController *loginController=[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"homestory"]; //or the homeController UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:loginController]; self.window.rootViewController=navController; – user2918201 Mar 16 '14 at 17:25
  • However, it still is having the same issue – user2918201 Mar 16 '14 at 17:26