我正在 Spritekit 中工作,我试图从我的 SKScene 中呈现一个 UIAlertController,但我在做这件事时遇到了困难。我看过几个教程,但没有一个 UIAlertController 教程专门针对 Spritekit。我一直在下面看到这段代码,但它并没有生效,因为 SKScene 不是 UIViewController。
[self presentViewController:self animated:YES completion:nil];
我在下面有其余的相关代码。任何人都可以帮我在我的 SKScene 上展示我的 UIAlertController 吗?
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 来帮助您调用视图控制器。
将其添加到视图控制器的 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];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)