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

问题描述

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

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

我的目标是在游戏结束时显示分享按钮。点击共享按钮应该呈现SLComposeViewController(Twitter共享)。场景的内容不应该改变。

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上显示一个Share按钮:

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:


推荐答案

您正在创建一个新的视图控制器,但从不提供它:

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 的引用,然后在SpriteMyScene.h中调用 openTweetSheet

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

时访问该引用

@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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 11:00