【问题标题】:ERROR: whose view is not in the window hierarchy. Spritekit错误:其视图不在窗口层次结构中。精灵包
【发布时间】:2014-09-03 08:38:51
【问题描述】:

我会尽力解释这一点。好的,我在 xcode5 中使用 SpriteKit。在 Myscene.m 中,我有一个名为:

-(void)presentViewController
{
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"MyView"];
    [self.view.window.rootViewController presentViewController:vc animated:YES completion:nil];
}

然后在我的 NSTimeintervalUpdate 方法中我有一个代码说

if(score >= 3)
{
    [self presentViewController]
}

所有这些代码都会从情节提要中调出我想要的视图控制器,就像它应该没有问题一样,但是在这个视图控制器中,它有一个链接回游戏的按钮。当您单击按钮时,它会按原样返回游戏。好吧,在游戏过程中,它并没有像第一次那样以“3”的分数返回我的 ViewController,而是继续计数并在日志中提供以下错误消息:

2014-07-12 22:40:27.710 tests[337:60b] Warning: Attempt to present <ViewController2: 0xc354e40> on <ViewController: 0x9960b80> whose view is not in the window hierarchy!

我的目的是拥有我的游戏 (Myscene.m),而当游戏结束时,是拥有一个游戏结束屏幕 (ViewController)。然后从这个视图控制器中,我希望它有一个再次播放按钮,该按钮链接回 Myscene.m(我只是通过制作按钮和控件并拖动到处理我的 SKScene 的视图控制器来完成)并继续重复过程但是它只会执行一次此过程,然后无法循环返回并显示上述错误。

任何帮助表示感谢!

【问题讨论】:

    标签: ios xcode5 sprite-kit viewcontroller skscene


    【解决方案1】:

    不要直接显示来自SKScene 的视图控制器。使用NSNotificationCenter 告诉父视图控制器呈现另一个视图控制器:

    GameSceneViewController.m:

    @interface GameSceneViewController
    
    @property (nonatomic) int score;
    
    @end
    
    @implementation GameSceneViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentMyViewController:) name:@"presentMyViewController" object:nil];
    }
    - (void)presentMyViewController:(NSNotification *)notification {
        self.score = notification.userInfo[@"score"];
        [self performSegueWithIdentifier:(segue identifier) sender:nil];
    }
    
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        // Make sure your segue name in storyboard is the same as this line
        if ([[segue identifier] isEqualToString:(segue identifier])
        {
            // Get reference to the destination view controller
            MyViewController *vc = [segue destinationViewController];
    
            // Pass any objects to the view controller here, like...
            vc.score = self.score;
        }
    }
    
    
    @end
    

    然后在GameScene.m调用:

    [[NSNotificationCenter defaultCenter] postNotificationName:@"presentMyViewController" object:nil userInfo:@{"score" : self.score}];
    

    【讨论】:

    • 还有什么具体在 selector:@selector() 里面,什么在“序列标识符”里面?顺便谢谢你的帮助
    • 好的,我已经弄清楚了您的回答并且效果很好,但是现在出现了一个新问题.....在使用您的代码拉起 ViewController 之后。每当我按下 UIButton 回到游戏时,它会立即自动再次拉起 ViewController,而不是等待分数上升到“3”。
    • 检查您的postNotificationName:object: 电话。只有在游戏结束时才调用它。尝试调试它。我对你的游戏场景一无所知,因为你没有提供代码,所以这个是你应该自己弄清楚的。
    • 我明白了,非常感谢!最后一个问题。在我的游戏中,我的 MyScene.m 中有一个 int 分数,它跟踪玩家的时间,即玩家的分数,当游戏结束时,我如何获取该 int 分数并将其显示在我的视图控制器上?
    • 两个 Segue 标识符会读取相同的内容吗?我知道我应该在 isEqualToString: @"Name of segue" 之后放什么,但是在 "if" 之后的开头 segue 标识符中应该放什么
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    相关资源
    最近更新 更多