2

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#>]
4

3 回答 3

3

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];
于 2015-07-01T08:06:28.910 回答
0

只需在创建场景时设置指向 viewController 的指针。然后你可以这样称呼它:[self.viewController presentViewController:alert animated:YES completion:nil];

在您的视图控制器中:

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

// Present the scene.
[skView presentScene:scene];
于 2016-03-15T18:47:51.983 回答
0

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。如果有人知道如何消除此警告,那就太好了。


[2016 年 8 月 11 日更新]

要消除上述警告,请检查 rootViewController 是否已呈现视图控制器:

 let vc = self.view?.window?.rootViewController
 if vc.presentedViewController == nil {
    vc.presentViewController(alert, animated: true, completion: nil)
 }
于 2015-08-17T04:39:41.290 回答