斯威夫特& SpriteKit - 如何在GameScene中显示警报视图 [英] Swift & SpriteKit - How to present alert view in GameScene
问题描述
我需要帮助在游戏场景中显示警报视图。我目前正在努力这样做,因为GameScene.Swift不是标准的ViewController。如果它有帮助我需要这样做,因为我需要用户输入一个值,该值用作我游戏中球Sprite Kit Node的坐标。输入只是一个标准整数,所以这不是问题。我也欢迎任何其他关于如何通过警报视图执行此操作的想法。
I need help presenting an alert view in the game scene. Im currently struggling to do so as GameScene.Swift isnt a standard ViewController. If it helps I need to do so as I need the user to input a value which is used as a coordinate for the ball Sprite Kit Node in my game. The input is only a standard integer so that isnt an issue. Any other idea of how I can do this which isnt through an alert view is also welcome.
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
let view = self.view as! SKView
if view.scene == nil {
view.showsFPS = false
view.showsNodeCount = false
let gameScene = GameScene(size: view.bounds.size)
gameScene.scaleMode = SKSceneScaleMode.AspectFill
view.presentScene(gameScene)
}
}
这是在GameViewController文件中
That is in the GameViewController file
var vc : GameViewController!
override init(size: CGSize) {
super.init(size: size)
let alertController = UIAlertController(title: "Bll Starting Position", message: "Please Enter a X Coordinate Value IN Range 0 to 345 ", preferredStyle: .Alert)
alertController.addTextFieldWithConfigurationHandler { (textField) in
textField.placeholder = "Value Must Be In Range 0 To 345"
textField.autocapitalizationType = UITextAutocapitalizationType.None
textField.autocorrectionType = UITextAutocorrectionType.No
textField.clearsOnBeginEditing = true
textField.clearsOnInsertion = true
textField.clearButtonMode = UITextFieldViewMode.Always
let cancelBtn = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
let confirmBtn = UIAlertAction(title: "Confirm", style: .Default, handler: { (confirmView) in
if let field = alertController.textFields![0] as? UITextField {
}
})
alertController.addAction(confirmBtn)
alertController.addAction(cancelBtn)
self.vc.presentViewController(alertController, animated: true, completion: nil)
}
谢谢
推荐答案
可能有以下选项:
1)快速解决方案。不要使用 UIAlertController
,使用 UIAlertView
。像那样:
1) Quick solution. Do not use UIAlertController
, use UIAlertView
. Like that:
alert.show()
然而, UIAlertView
已被弃用,因此依赖它并不安全。
However, UIAlertView
is deprecated so it's not quite safe to rely on it.
2)更好的解决方案。使 SKScene
子类保持对用于显示场景的视图控制器的引用,并在创建场景时为其分配视图控制器:
2) A better solution. Make your SKScene
subclass hold a reference to the view controller which you use to present the scene and when you create the scene assign it the view controller:
myScene.viewController = self
然后你可以使用它。
self.viewController.presentViewController(alertController, animated: true, completion: nil)
这篇关于斯威夫特& SpriteKit - 如何在GameScene中显示警报视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!