错误:其视图不在窗口层次结构中。 Spritekit

时间:2014-07-13 04:02:55

标签: ios xcode5 sprite-kit viewcontroller skscene

我会尽力解释这个问题。好的,我在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的视图控制器)并继续重复过程但它只会执行一次这个过程,然后它无法循环回来,而是出现上述错误。

任何帮助表示感谢!

1 个答案:

答案 0 :(得分:0)

不要直接从SKScene展示View Controller。使用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}];