Adding a View to a Window Hierarchy

I am trying to create a pause screen in my game. I added a viewController to the "PauseScreen" storyboard panel with the storyboard ID and recovery ID set to "PauseScreenID", and to go to the pause screen, I created a function in the "GameScene":

func pauseSceenTransition(){ let viewController = UIStoryboard(name: "Main", bundle:nil).instantiateViewControllerWithIdentifier("PauseScreenID") as UIViewController let currentViewController = (UIApplication.sharedApplication().delegate as AppDelegate) currentViewController.window?.rootViewController?.presentViewController(viewController, animated: false, completion: nil) } 

However, when it is called, I get an error message:

Warning: try presenting <AppName.PauseScreen: 0x7fae61fe5ff0> in <AppName.StartScreenViewController: 0x7fae61f79980>, whose view is not in the hierarchy of windows!

"StartScreenViewController" is the view controller for my start screen and is the start view controller. He then moves on to โ€œGameScene,โ€ where โ€œPauseScreenโ€ should go. It works if I make the initial view controller "GameViewController" / "GameScene", so I assume that I need to change the second line:

 let currentViewController = (UIApplication.sharedApplication().delegate as AppDelegate) 

so that it displays "PauseScreen" in the "GameViewController" and not in the "StartScreenViewController", but I'm not sure how to do it.

+5
source share
1 answer

The error tells you what exactly is happening.

Warning: Attempt to present <AppName.PauseScreen: 0x7fae61fe5ff0> on <AppName.StartScreenViewController: 0x7fae61f79980> whose view is not in the window hierarchy!

(UIApplication.sharedApplication().delegate as AppDelegate).window?.rootViewController points to an instance of StartScreenViewController . This is bad: rootViewController should point to an instance of GameScene .

The root cause should be how GameScene is GameScene . From your description:

"StartScreenViewController" is the view controller ... Then it goes to "GameScene" ...

That should be your problem. How do you navigate to GameScene from StartScreenViewController ?

I assume that you are adding a new window to the application. You need to install rootViewController .

 let gameScene = UIStoryboard(name: "Main", bundle:nil).instantiateViewControllerWithIdentifier("GameSceneID") as UIViewController let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate) appDelegate.window?.rootViewController = gameScene 

When you return to the start screen, you will install rootViewController again.

 let initialViewController = UIStoryboard(name: "Main", bundle:nil).instantiateInitialViewController() as UIViewController let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate) appDelegate.window?.rootViewController = initialViewController 

You can use transitionFromViewController(,toViewController:, duration:, options:, animations:, completion:) to animate the settings of the root view controller.

+11
source

Source: https://habr.com/ru/post/1209725/


All Articles