0

I want to delegate my method through [self.delegate showShareScreen] in GameOverScene.m, but I can't delegate that. I would like to know the reason why and solve it. Working image is that there is a twitter image in Game Over Scene. When I touch it, it will work and twitter share screen will appear and I can use it.

GameOverScene.h

#import  <SpriteKit/SpriteKit.h>

@protocol sceneDelegate <NSObject>
-(void)showShareScreen;
@end
@interface GameOverScene : SKScene
@property (weak, nonatomic) id <sceneDelegate> delegate;
- (id)init;
@end

GameOverScene.m

#import "GameOverScene.h"
#import "NewGameScene.h"
#import "MainScene.h"
#import <Social/Social.h>

@implementation GameOverScene

@synthesize delegate;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];

if([node.name isEqualToString:@"twitterbutton"]){
      [self.delegate showShareScreen];
      //When I use the breakpoint, it is ignored.**
   }
 }

ViewController.h

#import <UIKit/UIKit.h>
#import <SpriteKit/SpriteKit.h>
#import <iAd/iAd.h>
#import "GameOverScene.h"

@interface ViewController : UIViewController <sceneDelegate>

@end

ViewController.m

#import "ViewController.h"
#import "NewGameScene.h"
#import <Social/Social.h>
#import "GameOverScene.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)loadView
{
self.view  = [[SKView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
GameOverScene *theScene = [[GameOverScene alloc]init];
theScene.delegate = self;
}

-(void)showShareScreen
{ 
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
    SLComposeViewController *tweetSheet = [SLComposeViewController
                                                composeViewControllerForServiceType:SLServiceTypeTwitter];
    [tweetSheet setInitialText:@"TestTweet from the Game !!"];
    [self presentViewController:tweetSheet animated:YES completion:nil];

}
}

@end
Ayan Sengupta
  • 5,238
  • 2
  • 28
  • 40
Yaemon
  • 81
  • 1
  • 12
  • 2
    because `note.name` is not `"twitterbutton"`? – Bryan Chen Mar 18 '14 at 20:57
  • On your breakpoint, output to the console what node.name is, you can use `po [node name]` – Tim Mar 18 '14 at 20:58
  • I think yours delegate is not initialized at moment when touch occur. try add `if (nil == self.delegate) NSLog(@"delegate is nil");` – Cy-4AH Mar 18 '14 at 21:03
  • >>Bryan Chen and Jeff Yes, node.name is "twitterbutton", right. – Yaemon Mar 18 '14 at 21:15
  • >>Cy-4AH Thank you for the advice, I did it, and the log shows "delegate is nil" like you indicates. After that, I am so sorry, but may I ask what should I do ? I am a beginner of Xcode... I think I should initialise self.delegate . – Yaemon Mar 18 '14 at 21:18

0 Answers0