ios – 如何从SKScene呈现UIViewController?

前端之家收集整理的这篇文章主要介绍了ios – 如何从SKScene呈现UIViewController?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我通过Sprite Kit进入iOS,我认识到这是不明智的.

我的目标是在Game Over上显示一个“Share”按钮.轻击共享按钮应该呈现一个SLComposeViewController(Twitter Share).场景的内容不应该改变.

规定“游戏结束”的游戏逻辑生活在SKScene的子类SpriteMyScene.m中.

我可以在游戏中显示一个Share按钮:

-(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看起来像这样:

- (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.");
    }];
}

我总是在日志中得到这样的东西:

-[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’

解决方法

您正在创建一个新的视图控制器,但不会显示它:
SpriteViewController *viewController = [SpriteViewController alloc];

我假设SpriteViewController是呈现您的SpriteMyScene,并且您想手动控制回呈现的SpriteViewController.

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

在SpriteMyScene.h

@class SpriteViewController;

@interface SpriteMyScene : SKScene

@property (nonatomic,weak) SpriteViewController *spriteViewController;

@end

在SpriteViewController.m

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

myScene.spriteViewController = self;

在SpriteMyScene.m

#import "SpriteViewController.h"

- (void)sendToController
{
    NSLog(@"ok");
    // use the already-created spriteViewController
    [_spriteViewController openTweetSheet];
}
原文链接:https://www.f2er.com/iOS/336163.html

猜你在找的iOS相关文章