问题描述
我正在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> on <Sample_Game.GameViewController: 0x7fc31bd9b4f0> which is already presenting
会出现警告.如果有人知道如何消除此警告,那就太好了.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!