The most clean solution is to declare and implement a protocol to let the UIViewController
know that it should react to a change in SKScene
.
@protocol MySceneDelegate <NSObject>
- (void)closeScene;
@end
@interface MyScene : SKScene
@property(nonatomic, strong) SKShapeNode *ball;
@property (weak) id <MySceneDelegate> delegate;
@end
View controller that shows the scene should implement a closeScene
method and set itself as a delegate of the scene. Example:
- (void)viewDidLoad
{
[super viewDidLoad];
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
// Create and configure the scene.
MyScene * scene = [MyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Set the delegate
[scene setDelegate:self];
// Present the scene.
[skView presentScene:scene];
}
Then in the scene you can call the closeScene
method of the view controller which was set as a delegate:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
/* Called when a touch begins */
if ([_delegate respondsToSelector:@selector(closeScene)])
{
[_delegate performSelector:@selector(closeScene)];
}
}
Hope it helps.