在SKScene上呈现一个viewController

时间:2014-01-13 10:46:34

标签: ios iphone objective-c xcode sprite-kit

我想在UIActivityViewController上展示SkView,但xcode会给我这个错误:

  

'GameOver'没有可见的@interface声明选择器   'presentViewController:动画:完成:

- (void)shareScore {

    //add view
    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];

    //add label
    CGRect fframe = self.view.frame;

    UILabel *score = [[UILabel alloc] initWithFrame:fframe];
    score.text = @"9999";
    score.textAlignment = NSTextAlignmentCenter;
    score.textColor = [UIColor darkGrayColor];
    score.center = CGPointMake(250, 440);
    score.font = [UIFont fontWithName:@"Pixel LCD7" size:50];
    [Sview addSubview:score];


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

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

    [self presentViewController:activityViewController animated:YES completion:nil];

}

如何在SKScene上呈现预设的viewController? 谢谢。

2 个答案:

答案 0 :(得分:21)

我们可以使用“presentModalViewController”来使用此代码来访问根视图控制器

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

现在它运作正常!

答案 1 :(得分:6)

IIRC您创建的第一个SKScene位于SKView内。

SKView位于UIViewController

您可以使用属性或委托或任何您喜欢的方式,通过UIViewController中的SKView访问SKScene上的方法。甚至使用通知。

然后在UIViewController上,你可以毫无问题地展示新的视图控制器。