0

Trying to open a view controller from an SKScene.

Found https://stackoverflow.com/a/20072795/1686319 very helpful, but i get an error when trying to set the delegate from the view controller.

No visible @interface for 'SKScene' declares the selector 'setDelegate:'

EABMyScene.h

#import <SpriteKit/SpriteKit.h>

@protocol EABMySceneDelegate <NSObject>
-(void)doSomething;
@end

@interface EABMyScene : SKScene {

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

Any idea?

Update:

EABViewController.h

#import <UIKit/UIKit.h>
#import <SpriteKit/SpriteKit.h>

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

@interface EABViewController : UIViewController <ViewControllerDelegate>

@end

EABViewController.m

#import "EABViewController.h"
#import "EABMyScene.h"

@implementation EABViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Configure the view.
    SKView * skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;

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

    [scene setDelegate:self];

    // Present the scene.
    [skView presentScene:scene];
}

-(void)openTweetSheet
{
    NSLog(@"Open Tweet Delegate Method");
}

EABMyScene.h

#import <SpriteKit/SpriteKit.h>

@interface EABMyScene : SKScene {

}

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

@end

Error: No visible @interface for 'SKScene' declares the selector 'setDelegate:'

Community
  • 1
  • 1
Espen Birk
  • 436
  • 1
  • 5
  • 16
  • cast to (EABMyScene*) – CodeSmile Aug 25 '14 at 21:14
  • If you are trying to implement the code in the link, you are defining the delegate in the wrong place. It should be in your view controller not your SKScene subclass. – 0x141E Aug 25 '14 at 21:42
  • casting the scene to EABMyScene didnt work. The delegate property in EABMyScene.h is still not found/accessible. The delegate @property should be in the SKScene subclass i guess, so that i can set the viewcontroller as a delegate for my SKScene? Looks like no property in the SKScene header file is accessible or found in a viewcontroller after importing SKScene header file. – Espen Birk Aug 25 '14 at 22:34
  • 1
    I suggest you reread the answer in the link. The @protocol definition should be in a view controller's .h file not in EABMyScene.h. – 0x141E Aug 25 '14 at 22:40
  • I see it now, sorry my bad. Changed it, but the i still can not set/find/access the SKScenes delegate property from where the SKScene is initialized in my viewcontrollers viewDidLoad method – Espen Birk Aug 25 '14 at 23:13
  • 1
    If you post your changes, I will try to find the issue. – 0x141E Aug 26 '14 at 08:12
  • I've updated/edited the question with my changes 0x141E – Espen Birk Aug 26 '14 at 19:23
  • 1
    Change [scene setDelegate:self]; to [(EABMyScene *)scene setDelegate:self]; – 0x141E Aug 27 '14 at 09:16
  • WARNING: don't use `delegate` keyword as a name for your property! It is reserved by iOS 8 SDK, your app will crash on iOS 8. Rename it to `mySceneDelegate` or something like this. – Andrey Gordeev Aug 27 '14 at 14:28
  • Worked like a charm 0x141E, thanks a lot. Post it as an answer so i can select it. Thanks for the information Andrey! – Espen Birk Aug 30 '14 at 22:01

1 Answers1

2

Class SKScene does not have a property named delegate, so the statement

[scene setDelegate:self];

produced the compiler error. To fix this issue, cast scene to your SKScene subclass EABMyScene:

[(EABMyScene *)scene setDelegate:self];
0x141E
  • 12,613
  • 2
  • 41
  • 54