How to Introduce UIAlertController from SKScene

I work in Spritekit and I am trying to introduce a UIAlertController from my SKScene, but I have problems with this. I looked through several manuals, but none of the UIAlertController tutorials were specific to Spritekit. I continue to see this code below, but it was not effective since SKScene is not a UIViewController.

[self presentViewController:self animated:YES completion:nil];      

I have the rest of the relative code below. Can someone please help me introduce 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#>]
+2
source share
3 answers

SKScene presentViewController(_:animated:completion), UIViewController. , , :

self.view?.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)

ps: , Attempt to present <UIAlertController: 0x7fc31eb32e50> on <Sample_Game.GameViewController: 0x7fc31bd9b4f0> which is already presenting. - , , .


[ 11 2016 ]

, , rootViewController :

 let vc = self.view?.window?.rootViewController
 if vc.presentedViewController == nil {
    vc.presentViewController(alert, animated: true, completion: nil)
 }
0

SKScene , UIAlertController, UIViewController, , GameViewController. , UIViewController.

NSNotificationCenter, .

viewDidLoad,

[[NSNotificationCenter defaultCenter] addObserver:self                                          
                                         selector:@selector(playerLost:) 
                                             name:@"PlayerLostNotification" 
                                           object:nil];   

.

- (void)playerLost:(NSNotification*) notification {
   UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"You Lose!" 
                                         message:@"Do You Want To Beat This Level?" 
                                  preferredStyle:UIAlertControllerStyleAlert];

   UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"GiveUp"
                         style:UIAlertActionStyleDefault
                       handler:^(UIAlertAction * action) {
                          [alert dismissViewControllerAnimated:YES completion:nil];
                       }];
   [alert addAction:cancel];
   [self presentViewController:alert animated:YES completion:nil];
}                             

SKScene, ,

[[NSNotificationCenter defaultCenter] postNotificationName:@"PlayerLostNotification" object:self];
+2

Just set the pointer to your viewController when creating your scene. Then you can call it like this: [self.viewController presentViewController: animated warning: YES termination: zero];

In your ViewController:

// Create and configure the scene.
GameScene *scene = [GameScene sceneWithSize:viewSize];
SKView * skView = (SKView *)self.view;
scene.viewController = self;

// Present the scene.
[skView presentScene:scene];
0
source

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


All Articles