xcode 在 SKScene 上展示一个 viewController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21089332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:32:46  来源:igfitidea点击:

Presenting a viewController on SKScene

iosiphoneobjective-cxcodesprite-kit

提问by Mc.Lover

I am trying to present UIActivityViewControlleron an SkViewbut xcode gives me this error :

我正在尝试展示UIActivityViewControllerSkView但 xcode 给了我这个错误:

No visible @interface for 'GameOver' declares the selector 'presentViewController:animated:completion:'

'GameOver' 没有可见的 @interface 声明了选择器 'presentViewController:animated:completion:'

- (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];

}

How can I present preset a viewController on SKScene ? thanks .

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

回答by Mc.Lover

We can use "presentModalViewController" by using this code to access the root view controller

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

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

now it works fine !

现在它工作正常!

回答by Fogmeister

IIRC the first SKScenethat you create is inside an SKView.

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

This SKViewis inside a UIViewController.

SKView是在一个UIViewController.

You can use properties or delegation or whatever you like to access methods on the UIViewControllerthrough the SKViewfrom the SKScene. Or even use a notification.

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

Then on the UIViewControlleryou can present the new view controller with no problems.

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