1

I wish to display UIAlertView in my SpriteKit game (saying there is not enough coins to select an item). I have set UIAlertView *alertView; property in my (only) ViewController and initialized it.

However I can't access this from my Scene (tried to call a public method but it didn't work).

How can I access my ViewController and its properties form the scene?

Yuvals
  • 3,094
  • 5
  • 32
  • 60

1 Answers1

2

As to how to access the ViewController (and its properties) from your SKScene or any SKNode, I would save a pointer inside those SKNodes after creation.

@interface YourScene : SKScene
  @property (weak,nonatomic) UIViewController * presentingViewController;
@end

// inside the ViewController
YourScene * scene = [YourScene new];
scene.presentingViewController = self;    
[skView presentScene:scene];

An answer to your title question: Displaying alertViews in SpriteKit

You don't have to put the alertView property in your ViewController; you can just put it in your SKScene, or wherever you like. You don't even have to set a delegate, but if you want to just have your SKScene subclass conform to the UIAlertViewDelegate protocol.

@interface YourNode : SKNode<UIAlertViewDelegate>
  - (void) displayAlert;
@end

// ...

@implementation YourNode
  - (void) displayAlert {
    UIAlertView * alertView = [UIAlertView alloc] initWithTitle:@"Your title" message:@"Your message is this message" delegate:self cancelButtonTitle:@"Cancel me" otherButtonTitles:@"Whatever", nil];
    [alertView show];
  }
@end
CloakedEddy
  • 1,965
  • 15
  • 27