I'm working in Spritekit and I'm trying to present a UIAlertController from my SKScene, but I am having trouble doing it. I've watched several tutorials but none of the UIAlertController tutorials have been specific to Spritekit. I keep seeing this code below, but it has not been effective since SKScene is not a UIViewController.
[self presentViewController:self animated:YES completion:nil];
I have the rest of the relative code below. Can anybody please help me present my UIAlerController on my SKScene.
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"You Lose!" message:@"Do You Want To Beat This Level?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *CancelButton = [UIAlertAction actionWithTitle:@"GiveUp" style:UIAlertControllerStyleAlert handler:<#^(UIAlertAction *action)handler#>]
SKScene shouldn't be the one presenting the UIAlertController, but rather a UIViewController such as your initial GameViewController. Above code works fine when called from a UIViewController.
You could use NSNotificationCenter to help you call your view controller.
Add this to your view controller's viewDidLoad method,
and you will need to define this method too.
In your SKScene when the player loses,
Just set a pointer to your viewController when you create your scene. Then you can call it like this: [self.viewController presentViewController:alert animated:YES completion:nil];
In your ViewController:
The
SKScene
instance can't invokepresentViewController(_:animated:completion)
because it is not a subclass ofUIViewController
. However, if you rewrite as such, your alert will launch:ps: there will be a warning though that
Attempt to present <UIAlertController: 0x7fc31eb32e50> on <Sample_Game.GameViewController: 0x7fc31bd9b4f0> which is already presenting
. If anyone knows how to eradicate this warning, that will be great.[Updated 11 August 2016]
To eradicate the aforementioned warning, check if the rootViewController has presented a view controller: