如何从 SKScene 呈现 UIAlertController [英] How to present UIAlertController from SKScene
问题描述
我在 Spritekit 中工作,我正在尝试从我的 SKScene 中展示一个 UIAlertController,但我在做这件事时遇到了麻烦.我看过几个教程,但没有一个 UIAlertController 教程是专门针对 Spritekit 的.我一直在下面看到这段代码,但由于 SKScene 不是 UIViewController,因此它没有生效.
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];
我有下面的其余相关代码.谁能帮我在我的 SKScene 上展示我的 UIAlerController.
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
实例不能调用 presentViewController(_:animated:completion)
因为它不是UIViewController
的子类.但是,如果您这样重写,您的警报将启动:
The SKScene
instance can't invoke presentViewController(_:animated:completion)
because it is not a subclass of UIViewController
. However, if you rewrite as such, your alert will launch:
self.view?.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
ps:虽然 Attempt to present <UIAlertController: 0x7fc31eb32e50> 会有一个警告.在 <Sample_Game.GameViewController: 0x7fc31bd9b4f0>已经呈现
.如果有人知道如何消除此警告,那就太好了.
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.
[2016 年 8 月 11 日更新]
[Updated 11 August 2016]
要消除上述警告,请检查 rootViewController 是否已呈现视图控制器:
To eradicate the aforementioned warning, check if the rootViewController has presented a view controller:
let vc = self.view?.window?.rootViewController
if vc.presentedViewController == nil {
vc.presentViewController(alert, animated: true, completion: nil)
}
这篇关于如何从 SKScene 呈现 UIAlertController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!