【问题标题】:How to fix this share button?如何修复这个分享按钮?
【发布时间】:2014-05-05 15:01:15
【问题描述】:

我正在开发一个精灵套件游戏,我想在游戏结束时集成一个 Twitter 共享模块。

这是我在一个空场景上测试的 BASIC 代码:

@implementation gameOverScene

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */

        self.backgroundColor = [SKColor orangeColor];
    }
    return self;
}

-(void)showTweetSheet {

    //Create an instance of the tweet sheet
    SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];

    tweetSheet.completionHandler = ^ (SLComposeViewControllerResult result) {
        switch (result) {
                //the tweet was canceled
            case SLComposeViewControllerResultCancelled:
                break;

                //the user hit send
            case SLComposeViewControllerResultDone:
                break;
        }
    };

    //sets body of the tweet
    [tweetSheet setInitialText:@"testing text"];

    //add an image to the tweet
    if (![tweetSheet addImage:[UIImage imageNamed:@"name.png"]]) {
        NSLog(@"Unable to add the image!");
    }

    //add a URL to the tweet, you can add multiple URLS:
    if (![tweetSheet addURL:[NSURL URLWithString:@"url.com"]]) {
        NSLog(@"Unable to add the URL!");
    }


}

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

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];

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

但是当用户点击场景时尝试呈现 tweetSheet 视图控制器时,我不断收到错误“使用未声明的标识符”。

如何将社交框架正确集成到我的项目中?可以在精灵套件上使用吗?

【问题讨论】:

  • tweetSheetshowTweetSheet 方法中被声明为局部变量。也许这就是为什么当您尝试在单独的方法touchesBegan 中使用它时,编译器会说“使用未声明的标识符”。
  • 我就是这么想的,但我把它变成了一个全球性的,结果也一样:(@Anna
  • 更新问题中的代码并展示您如何将其设为“全局变量”。
  • 我不明白你的代码。在 touchesBegan 中,调用 do: [self showTweetSheet]。即使这样,我实际上也不理解您的代码。寻找教程。
  • @Anna woops 我的意思是一个文件范围的实例变量,我把它放在大括号中的实现之后(现在删除它但它是 - implementation MyScene {SLComposeViewController *tweetSheet}

标签: ios objective-c twitter sprite-kit


【解决方案1】:

首先,由于您已经创建了一个方法,您不需要从触摸委托中呈现它。而是调用showTweetSheet 方法。

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

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];

    //presents the tweet sheet to the user
        [self showTweetSheet];
    }
}

你可以使用下面的代码来展示它:

-(void)showTweetSheet
{

    .
    .
    .
    //Your initialisation as before

    [self.view.window.rootViewController presentViewController:tweetSheet animated:YES completion:^{}];

}

由于这是一个 SKScene,它本身不能呈现一个 viewController。您需要从另一个 viewController 呈现 viewController,可以使用 self.view.window.rootViewController 属性访问。

【讨论】:

  • 工作就像一个魅力。欣赏它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-16
  • 2014-04-23
  • 2012-05-10
  • 2011-07-30
  • 1970-01-01
  • 2012-10-24
  • 1970-01-01
相关资源
最近更新 更多