ios - 如何从 SKScene 呈现 UIAlertController

标签 ios objective-c sprite-kit uialertcontroller uialertaction

我正在 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#>]

最佳答案

SKScene 不应该是呈现 UIAlertController 的那个,而应该是 UIViewController,例如您最初的 GameViewController。当从 UIViewController 调用时,上面的代码工作正常。

您可以使用 NSNotificationCenter 来帮助您调用 View Controller 。

将其添加到 View Controller 的 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];

关于ios - 如何从 SKScene 呈现 UIAlertController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31152621/

相关文章:

ios - 是否可以从 CLI 命令应用 "Update to recommended settings"?

objective-c - 缓存自定义类

swift : How to handle a lot of textures in memory

ios - 首次启动时调试 iOS 应用程序

ios - 按下 UIButton 时加载 UIWebview

objective-c - Objective-C 是否有等同于 Swift 的 @escaping 注释?

ios - 在更改 View (Segue) 时显示事件指示器并为下一个 View (API) 加载数据

ios - 我可以在运行时创建 SpriteKit 纹理图集吗

swift - Spritekit - 移动物理世界问题

iOS:是否有 Apple Store 可接受的方法来检查屏幕是否打开/关闭?