ios - 切换视图控制器后第二次调用方法的问题
问题描述
行。所以我正在使用目标 C 在 sprite kit 中开发一个游戏。我有一个方法,我调用它使用运行 skscene 的 viewcontroller 在游戏结束时调用 uialertcontroller。在警报视图中按下 ok 按钮后,我模态回到主菜单视图控制器。这工作正常。但是,当我再次玩游戏时,切换回 gameviewcontroller 并结束另一场游戏时,uialertview 无法触发。我收到一条错误消息:
Warning: Attempt to present <UIAlertController: 0x14205d600> on <GameViewController: 0x141e176f0> whose view is not in the window hierarchy!
这是我调用 UIAlertController 的代码:
UIAlertController * gameOverAlert = [UIAlertController alertControllerWithTitle: @"Game Over!" message: textscore preferredStyle: UIAlertControllerStyleAlert];
//add the button that will take us back to the main menu
UIAlertAction *okButton = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[UIApplication.sharedApplication.keyWindow.rootViewController.presentedViewController performSegueWithIdentifier:@"backToTheMenuNotFuture" sender:self];
}];
[gameOverAlert addAction: okButton];
[UIApplication.sharedApplication.keyWindow.rootViewController.presentedViewController presentViewController: gameOverAlert animated:true completion: nil];
这是我模态到游戏视图控制器时的代码:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"gotoPlay"]){
gameController = (GameViewController *)segue.destinationViewController;
gameController.playingMusic = musicHave;
}
}
正如我所说,这在第一场比赛中表现良好,但是当你再次比赛并输掉第二场比赛时,就会发生错误。
解决方案
解决了:
(isDismissed 是一个从 0 开始的 int 值)
//add the button that will take us back to the main menu
UIAlertAction *okButton = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
isDismissed = 1;
[UIApplication.sharedApplication.keyWindow.rootViewController.presentedViewController performSegueWithIdentifier:@"backToTheMenuNotFuture" sender:self];
}];
[gameOverAlert addAction: okButton];
[UIApplication.sharedApplication.keyWindow.rootViewController.presentedViewController presentViewController: gameOverAlert animated:true completion: nil];
if(isDismissed == 1){
[UIApplication.sharedApplication.keyWindow.rootViewController.presentedViewController dismissViewControllerAnimated:NO completion: nil];
[UIApplication.sharedApplication.keyWindow.rootViewController.presentedViewController presentViewController: gameOverAlert animated:true completion: nil];
[UIApplication.sharedApplication.keyWindow.rootViewController.presentedViewController performSegueWithIdentifier:@"backToTheMenuNotFuture" sender:self];
}
}
所以这里的问题和评论一样是有多个 gameviewcontroller 实例同时运行。上面的代码关闭了“当前”游戏视图控制器,然后尝试运行我想要的原始游戏视图控制器。它可以工作,但是我不确定游戏是否会在每次运行时释放旧的游戏视图控制器或创建更多副本。最后,我认为它是这样的。如果它像它应该的那样释放,那么它很好。如果不是,那么我所说的游戏消耗大约 8MB 的 RAM 最终会导致 iPhone 滞后。如果确实发生了这种情况,那么您显然已经玩了很长时间,以至于无论如何您都应该休息一下。这不是一个错误,这是一个功能!
推荐阅读
- ios - 如何在swift4中的scrollView上设置admob
- mysql - 构造一对多表php mysql的正确方法
- r - 减少多个条形图R中条形的宽度
- html - 视觉和布局视口和文档
- asp.net-mvc - asp.net mvc 从视图中排除 Id
- html - 背景图像悬停转换延迟
- javascript - JavaScript - 禁用提交按钮
- javascript - 解析 html 时 jQuery 不加载
- python - 如何在 boto3 中模拟 AWS CLI EC2 过滤器
- python - 为什么 mysql DB、表、列上的 utf8mb4 在插入特殊字符时出错?