Adding a view to the window hierarchy
Asked Answered
M

1

7

I am trying to create a pause screen on my game. I have added a 'PauseScreen' viewController in my storyboard with the Storyboard ID and Restoration ID set as "PauseScreenID" and to move to the pause screen I have have created the function in "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 the it gets called, I get the error :

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

The "StartScreenViewController" is the view controller for my start screen and is the initial view controller. It then goes to the "GameScene" which is where the "PauseScreen" needs to go. It works if I make the initial view controller the "GameViewController"/"GameScene" so I presume that I need to change the second line:

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

so that it is presenting the "PauseScreen" on the "GameViewController", not on the "StartScreenViewController" but I'm not sure how to do that.

Mccallion answered 22/12, 2014 at 18:24 Comment(0)
F
15

The error is telling you exactly what's going on.

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 is pointing to an instance of StartScreenViewController. This is bad: rootViewController should point to an instance of GameScene.

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

The "StartScreenViewController" is the view controller… It then goes to the "GameScene"…

This must be where your problem is. How do you go to GameScene from StartScreenViewController?

My guess is that you are adding a new window to the application. You need to set the rootViewController instead.

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

When you go back to the start screen, you again set the rootViewController.

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 setting the root view controller.

Fagin answered 22/12, 2014 at 18:59 Comment(4)
Hi, I am having trouble with this same issue now, and am curious as to if the solutions have changed over the past year and a half. I am trying to call a UIAlertController from within a nested if inside of a button. The regular if statements within the button display the UIAlertView's fine, but the true results of those if statements throw the same error above.Precarious
@Precarious Sometimes you need to create a window to put a UIAlertController into: #26555394Fagin
Anything you know of that is written in swift?Precarious
@JefferyThomas Great man you are the saviour!! Last three days I searching solution for this issue. Thank you so much.Occipital

© 2022 - 2024 — McMap. All rights reserved.