gpt4 book ai didi

ios - 如何从 SKScene 呈现 UIAlertController

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:00:36 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com