【问题标题】:How to present UIAlertController from SKScene如何从 SKScene 呈现 UIAlertController
【发布时间】:2015-07-01 04:36:14
【问题描述】:

我在 Spritekit 中工作,我正在尝试从我的 SKScene 中呈现一个 UIAlertController,但我在执行此操作时遇到了麻烦。我看过几个教程,但没有一个 UIAlertController 教程是专门针对 Spritekit 的。我一直在下面看到这段代码,但由于 SKScene 不是 UIViewController,因此它没有生效。

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

我有下面的其余相关代码。谁能帮我在我的 SKScene 上展示我的 UIAlerController。

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

【问题讨论】:

    标签: ios objective-c sprite-kit uialertcontroller uialertaction


    【解决方案1】:

    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];
    

    【讨论】:

    • 但是在我的游戏上下文中,当玩家输了时,我使用 UIAlertController 让用户选择再次玩关卡或放弃并进入菜单(我之前使用的是 UIAlertView )。如何从 SKScene 执行该操作?
    • 正确。本质上,您需要将消息中继回视图控制器。您可以在 SKScene 中保留对视图控制器的引用、进行委托或使用 NSNotificationCenter。本质上,您希望在显示警报时调用视图控制器。
    • 您介意在代码中写一个我将如何做到这一点的示例吗?
    【解决方案2】:

    SKScene 实例无法调用presentViewController(_:animated:completion),因为它不是UIViewController 的子类。但是,如果您这样重写,您的警报将启动:

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

    ps:尽管Attempt to present &lt;UIAlertController: 0x7fc31eb32e50&gt; on &lt;Sample_Game.GameViewController: 0x7fc31bd9b4f0&gt; 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)
     }
    

    【讨论】:

      【解决方案3】:

      只需在创建场景时设置指向 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];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-01
        • 1970-01-01
        • 2015-08-09
        • 1970-01-01
        • 1970-01-01
        • 2016-05-06
        相关资源
        最近更新 更多