我正在 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/