在SpriteKit Game的主ViewController中调用presentViewController

时间:2014-05-20 13:28:31

标签: ios iphone objective-c uiviewcontroller

我正在构建“spritekit”游戏,我需要提供另一个viewController而不是主viewcontroller。 我试图与代表和通知中心执行此操作,以发送要求呈现新的视图控制器。两种方式都在“SkScene”(tuGameOver)中在main viewController(tuViewController)中正确调用了此函数。关键是要正确呈现FacebookLikeViewDemoViewController

-(void)showShareScreen{
    NSLog(@"VIEWCONTROLER RECEIVED");
    FacebookLikeViewDemoViewController *helpVC = [[FacebookLikeViewDemoViewController alloc]initWithNibName:@"FacebookLikeViewDemoViewController" bundle:nil];
    [self presentViewController: helpVC animated: YES completion:nil];
}

执行此函数时,我得到NSlog,然后terminating with uncaught exception of type NSException SIGABRT错误。

我还尝试将此功能修改为:

-(void)showShareScreen{
    NSLog(@"VIEWCONTROLER RECEIVED");

    UIViewController *vc = [[UIApplication sharedApplication] keyWindow].rootViewController;
        FacebookLikeViewDemoViewController *helpVC = [[FacebookLikeViewDemoViewController alloc]init];
    [vc presentViewController: helpVC animated: YES completion:nil];
}

有了这个,我没有崩溃,出现了一些东西,但这是空白的黑屏。

FacebookLikeViewDemoViewController是典型的viewController类,固定在故事板中查看Controller。 有什么想法吗?

2 个答案:

答案 0 :(得分:0)

试试这段代码:

 FacebookLikeViewDemoViewController *helpVC = [[FacebookLikeViewDemoViewController alloc]initWithNibName:@"FacebookLikeViewDemoViewController" bundle:nil];
        [self.navigationController pushViewController:helpVC animated:YES] ;

答案 1 :(得分:0)

Mc.Lover(轻笑)想出了一个在SKScene中展示VC的解决方案。以下是他的解决方案的要点。我已经将它添加到touchesBegan:方法中,因此您可以轻松地对其进行测试。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIView *Sview  = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 512, 512)];
    UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"shareScoreImg.png"]];
    image.frame = Sview.frame;
    [Sview addSubview:image];

    UIGraphicsBeginImageContextWithOptions(Sview.bounds.size, Sview.opaque, 0.0);
    [Sview.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIActivityViewController* activityViewController =
    [[UIActivityViewController alloc] initWithActivityItems:@[screenshot] applicationActivities:nil];

    UIViewController *vc = self.view.window.rootViewController;
    [vc presentViewController: activityViewController animated: YES completion:nil];
}

他原来的Q& A是here