如何呈现来自 SKScene 的 UIViewController? [英] How do I present a UIViewController from SKScene?

查看:13
本文介绍了如何呈现来自 SKScene 的 UIViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 Sprite Kit 进入 iOS,我认为这是不明智的.

I'm entering iOS via Sprite Kit, which I recognize is unwise.

我的目标是在游戏结束时显示分享"按钮.点击分享按钮应该会显示一个 SLComposeViewController (Twitter Share).场景的内容不应该改变.

My goal is to display a "Share" button upon Game Over. Tapping the share button should present a SLComposeViewController (Twitter Share). The contents of the scene should not change.

指示游戏结束"的游戏逻辑存在于 SpriteMyScene.m 中,它是 SKScene 的子类.

The game logic that dictates "Game Over" lives in SpriteMyScene.m, a subclass of SKScene.

我可以通过这种方式在 Game Over 上显示分享按钮:

I'm able to display a Share button on Game Over this way:

-(void)update:(CFTimeInterval)currentTime {

if (gameOver){
  UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                [button addTarget:self
                           action:@selector(sendToController)
                forControlEvents:UIControlEventTouchDown];
                [button setTitle:@"Show View" forState:UIControlStateNormal];
                button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
                [self.view addSubview:button]; 
    }
}

- (void)sendToController
{
    NSLog(@"ok");
    SpriteViewController *viewController = [SpriteViewController alloc];
    [viewController openTweetSheet];
}

我卡住的地方是试图让 showTweetButton 方法工作.我的 SpriteViewController.m 看起来像这样:

Where I get stuck is trying to get the showTweetButton method to work. My SpriteViewController.m looks like this:

- (void)openTweetSheet
    {
     SLComposeViewController *tweetSheet = [SLComposeViewController
                                           composeViewControllerForServiceType:
                                           SLServiceTypeTwitter];

    // Sets the completion handler.  Note that we don't know which thread the
    // block will be called on, so we need to ensure that any required UI
    // updates occur on the main queue
    tweetSheet.completionHandler = ^(SLComposeViewControllerResult result) {
        switch(result) {
                //  This means the user cancelled without sending the Tweet
            case SLComposeViewControllerResultCancelled:
                break;
                //  This means the user hit 'Send'
            case SLComposeViewControllerResultDone:
                break;
        }
    };

    //  Set the initial body of the Tweet
    [tweetSheet setInitialText:@"just setting up my twttr"];

    //  Adds an image to the Tweet.  For demo purposes, assume we have an
    //  image named 'larry.png' that we wish to attach
    if (![tweetSheet addImage:[UIImage imageNamed:@"larry.png"]]) {
        NSLog(@"Unable to add the image!");
    }

    //  Add an URL to the Tweet.  You can add multiple URLs.
    if (![tweetSheet addURL:[NSURL URLWithString:@"http://twitter.com/"]]){
        NSLog(@"Unable to add the URL!");
    }

    //  Presents the Tweet Sheet to the user
    [self presentViewController:tweetSheet animated:NO completion:^{
        NSLog(@"Tweet sheet has been presented.");
    }];
}

我总是在日志中得到类似的东西:

I always get something like this in the logs:

-[UIView presentScene:]: unrecognized selector sent to instance 0x13e63d00 2013-10-17 18:40:01.611 Fix[33409:a0b] * 终止应用由于未捕获的异常NSInvalidArgumentException",原因:'-[UIView presentScene:]: 无法识别的选择器发送到实例0x13e63d00'

-[UIView presentScene:]: unrecognized selector sent to instance 0x13e63d00 2013-10-17 18:40:01.611 Fix[33409:a0b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView presentScene:]: unrecognized selector sent to instance 0x13e63d00'

推荐答案

你正在创建一个新的视图控制器,但从未展示它:

You're creating a new view controller but never presenting it:

SpriteViewController *viewController = [SpriteViewController alloc];

我假设 SpriteViewController 是呈现您的 SpriteMyScene 的东西,并且您希望将控制权交还给呈现的 SpriteViewController.

I'm assuming that SpriteViewController is what presents your SpriteMyScene, and you'd like to hand control back to the presenting SpriteViewController.

您需要在 SpriteMyScene 子类中保留对 SpriteViewController 的引用,然后在调用 openTweetSheet 时访问该引用.

You need to keep a reference to SpriteViewController in your SpriteMyScene subclass, and then access that reference when you call openTweetSheet.

在 SpriteMyScene.h 中

in SpriteMyScene.h

@class SpriteViewController;

@interface SpriteMyScene : SKScene

@property (nonatomic, weak) SpriteViewController *spriteViewController;

@end

在 SpriteViewController.m 中

in SpriteViewController.m

// somewhere you initialize your SpriteMyScene object, I'm going to call it myScene

myScene.spriteViewController = self;

在 SpriteMyScene.m 中

in SpriteMyScene.m

#import "SpriteViewController.h"

- (void)sendToController
{
    NSLog(@"ok");
    // use the already-created spriteViewController
    [_spriteViewController openTweetSheet];
}

这篇关于如何呈现来自 SKScene 的 UIViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆