2014-05-04 60 views
0

我正在製作一個精靈套件遊戲,我想在遊戲結束時集成一個twitter共享模塊。如何解決這個分享按鈕?

這是基本的代碼,我嘗試了一個空的現場測試的東西:

@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視圖控制器時,當用戶點擊在現場。

如何正確地將社交框架整合到我的項目中?精靈套件有可能嗎?

+0

'tweetSheet'在'showTweetSheet'方法中聲明爲局部變量。也許這就是爲什麼編譯器在嘗試在單獨的方法「touchesBegan」中使用它時說「使用未聲明的標識符」。 – Anna

+0

多數民衆贊成我認爲,但我做了一個全球性的,並有相同的結果:(@Anna – user3576196

+0

更新問題中的代碼,並顯示你如何使它成爲一個「全局變量」。 – Anna

回答

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屬性訪問該viewController。

+0

工作就像一個魅力。欣賞它。 – user3576196