如何在SpriteKit中显示警报视图 [英] How to display alert view in SpriteKit
问题描述
我希望在我的SpriteKit游戏中显示UIAlertView(说没有足够的硬币来选择项目)。我在我的(仅)ViewController
中设置了 UIAlertView * alertView;
属性并对其进行了初始化。
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).
如何从场景中访问我的ViewController及其属性?
How can I access my ViewController and its properties form the scene?
推荐答案
关于如何访问ViewController (和它的属性)来自你的SKScene或任何SKNode,我会在创建后在这些SKNode中保存一个指针。
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];
标题问题的答案:在SpriteKit中显示alertViews
您不必在ViewController中放置 alertView
属性;你可以把它放在你的SKScene,或任何你喜欢的地方。您甚至不必设置委托,但如果您想让您的SKScene子类符合 UIAlertViewDelegate
协议。
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
这篇关于如何在SpriteKit中显示警报视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!