简体   繁体   English

Swift & SpriteKit - 如何在 GameScene 中呈现警报视图

[英]Swift & SpriteKit - How to present alert view in GameScene

I need help presenting an alert view in the game scene.我需要帮助在游戏场景中显示警报视图。 Im currently struggling to do so as GameScene.Swift isnt a standard ViewController.我目前正在努力这样做,因为 GameScene.Swift 不是标准的 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.如果有帮助,我需要这样做,因为我需要用户输入一个值,该值用作我游戏中球 Sprite Kit 节点的坐标。 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)
    }


}

That is in the GameViewController file那是在 GameViewController 文件中

    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)

    }

Thanks谢谢

You can show UIAlertControllers directly from SKScenes, simply show them on the rootViewController, which is probably the best place to show them anyway.您可以直接从 SKScene 显示 UIAlertControllers,只需将它们显示在 rootViewController 上,这可能是显示它们的最佳位置。

view?.window?.rootViewController?.present...

In general its not the best practice to reference the GameViewController in SKScenes and I never actually got to a point where I was forced to do so.一般来说,在 SKScenes 中引用 GameViewController 并不是最佳实践,而且我实际上从未达到过被迫这样做的程度。 NSNotificationCenter, delegation or protocol extensions are the better way. NSNotificationCenter、委托或协议扩展是更好的方法。

I actually use a helper for Alerts I made using Swift 2's protocol extensions.我实际上使用了我使用 Swift 2 的协议扩展制作的警报助手。

Just make a new .swift file and add this code只需创建一个新的 .swift 文件并添加此代码

import SpriteKit

protocol Alertable { }
extension Alertable where Self: SKScene {

    func showAlert(withTitle title: String, message: String) {

        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)

        let okAction = UIAlertAction(title: "OK", style: .cancel) { _ in }
        alertController.addAction(okAction)

        view?.window?.rootViewController?.present(alertController, animated: true)
    }

    func showAlertWithSettings(withTitle title: String, message: String) {

        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)

        let okAction = UIAlertAction(title: "OK", style: .cancel) { _ in }
        alertController.addAction(okAction)

        let settingsAction = UIAlertAction(title: "Settings", style: .default) { _ in

            guard let url = URL(string: UIApplicationOpenSettingsURLString) else { return }
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(url)
            } else {
                UIApplication.shared.openURL(url)
            }
        }
        alertController.addAction(settingsAction)

        view?.window?.rootViewController?.present(alertController, animated: true)
    }
}

Now in your scenes you need to show alerts you simply conform to the protocol现在在您的场景中,您需要显示警报,您只需遵守协议

class GameScene: SKScene, Alertable {

} 

and call the methods like并调用类似的方法

showAlert(withTitle: "Alert title", message: "Alert message")

as if they are part of the scene itself.仿佛它们是场景本身的一部分。

Hope this helps希望这可以帮助

There may be the following options:可能有以下选项:

1) Quick solution. 1)快速解决。 Do not use UIAlertController , use UIAlertView .不要使用UIAlertController ,使用UIAlertView Like that:像那样:

alert.show()

However, UIAlertView is deprecated so it's not quite safe to rely on it.但是, UIAlertView已被弃用,因此依赖它并不十分安全。

2) A better solution. 2)更好的解决方案。 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:使您的SKScene子类持有对用于呈现场景的视图控制器的引用,并在创建场景时为其分配视图控制器:

myScene.viewController = self

And then you can use it.然后你就可以使用它了。

self.viewController.presentViewController(alertController, animated: true, completion: nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM