13

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

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

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

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

-(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] *由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'-[UIView presentScene:]:无法识别的选择器发送到实例 0x13e63d00'

4

5 回答 5

11

您正在创建一个新的视图控制器,但从未呈现它:

SpriteViewController *viewController = [SpriteViewController alloc];

我假设这SpriteViewController就是呈现你的东西SpriteMyScene,而你想将控制权交还给呈现SpriteViewController

您需要SpriteViewControllerSpriteMyScene子类中保留对的引用,然后在调用时访问该引用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];
}
于 2013-10-18T00:10:41.230 回答
4

您可以使用

UIViewController *vc = self.view.window.rootViewController;

此代码将使您可以访问根视图控制器,因此您可以像往常一样对视图控制器执行任何操作。

但是,您需要添加按钮吗?在这种情况下,使用精灵并向其添加事件更适合您。只需致电:

UIViewController *vc = self.view.window.rootViewController;
[vc openTweetSheet];

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    NSArray *nodes = [self nodesAtPoint:[touch locationInNode:self]];
    for (SKNode *node in nodes) {
        if ([node.name isEqualToString:@"OpenTweet"]) {
            UIViewController *vc = self.view.window.rootViewController;
            [vc openTweetSheet];
        }
    }
}
于 2013-10-18T06:22:09.880 回答
3

如果你想从你的场景中打开另一个 UIViewController,你必须首先在最初创建这个场景的主视图控制器中创建一个委托,以便你的场景可以通知​​它的 ViewController 一个打开的推文动作。您将需要以下步骤:

  1. 在主视图控制器(我们场景的视图控制器)中定义一个委托,以处理打开的推文动作。
  2. 在主视图控制器中实现委托方法
  3. 在您的场景中添加一个委托属性,以便它可以保留其 ViewController 的委托方法实现的句柄。在创建场景期间将此委托设置为主视图控制器
  4. 检测场景中的事件以调用主 ViewController 的委托方法
  5. 在委托方法实现中,将控制权传递给 TweetSheetViewController

这是示例:

@protocol ViewControllerDelegate <NSObject>
-(void) openTweetSheet;
@end

扩展此 ViewController 以在其 .h 文件中支持此协议

@interface ViewController : UIViewController<ViewControllerDelegate>

@end

然后在 .m 文件中实现协议中的方法

-(void) openTweetSheet{
    TweetSheetViewController *ctrl = [[TweetSheetViewController alloc] initWithNibName:@"TweetSheetViewController" bundle:nil];

    [self presentViewController:ctrl animated:YES completion:nil];
}

在您的场景头类中,添加一个委托属性

@interface MyScene : SKScene {

}
@property (nonatomic, weak) id <ViewControllerDelegate> delegate;

@end

在 ViewController 中,在呈现场景之前,在 viewDidLoad 方法中设置其委托:

// Create and configure the scene.
MyScene * scene = [MyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;

[scene setDelegate:self];
// Present the scene.
[skView presentScene:scene];

现在您的场景可以将消息传递回它的 ViewController 并且 ViewController 可以打开另一个 ViewController。在您的场景类中,确定将触发打开 TweetSheetViewController 的操作

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];
        SKAction *fadeOut = [SKAction fadeOutWithDuration:0.5];
        SKAction *fadeIn = [SKAction fadeInWithDuration:1.0];
        SKAction *sequence = [SKAction sequence:@[fadeOut,fadeIn]];

        SKNode *node = [self nodeAtPoint:location];
        if ([[node name]  isEqual: @"openTweet"]) {
            NSLog(@"help");
            [node runAction:sequence];
            [delegate openTweetSheet];
        }

}

希望有帮助。

于 2013-11-19T13:27:31.023 回答
1

在您希望它发生的场景中编写 SlComposeViewController 方法。例如:

@interface GameOverScene: SKScene

...initwithsize bla bla bla
...

添加这些方法:

-(void)OpenTweetShet{

    if ([SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter]) {

    _composeTwitter = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
    [_composeTwitter setInitialText:@"TWEET"];

然后调用:

self.view.window.rootViewController **to present the SLComposeViewController**

    [self.view.window.rootViewController presentViewController:_composeTwitter animated:YES completion:nil];


}

[_composeTwitter setCompletionHandler:^(SLComposeViewControllerResult result){

NSString *output =[[NSString alloc]init];

switch (result) {
    case SLComposeViewControllerResultCancelled:
        output = @"Post cancelled";
        break;
    case SLComposeViewControllerResultDone:
        output = @"Post Succesfull";
        break;
    default:
        break;      
    }

这是在发送/取消帖子后显示 UIAlert 的选项:

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:output delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
    [alert show];     
    }];
}
@end
于 2014-06-03T14:57:53.670 回答
0

这对我有用:self.view.window.rootViewController

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
NSArray *nodes = [self nodesAtPoint:[touch locationInNode:self]];
for (SKNode *node in nodes) {
    if ([node.name isEqualToString:@"OpenTweet"]) {
        UIViewController *vc = self.view.window.rootViewController;
        [self.view.window.rootViewController openTweetSheet];
    }
}
}
于 2014-02-03T20:01:47.827 回答