setting SKScene delegate from viewcontroller, dele

2019-06-10 05:40发布

问题:

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:'

回答1:

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];