Reputation: 3266
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?
Upvotes: 1
Views: 2009
Reputation: 1995
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];
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
Upvotes: 2