斯威夫特& SpriteKit - 如何在GameScene中呈现警报视图

时间:2016-09-18 11:50:35

标签: ios swift xcode sprite-kit skspritenode

我需要帮助在游戏场景中呈现警报视图。我目前正在努力这样做,因为GameScene.Swift不是标准的ViewController。如果它有帮助我需要这样做,因为我需要用户输入一个值,该值用作我的游戏中的球Sprite Kit节点的坐标。输入只是一个标准整数,所以这不是问题。我也欢迎任何其他关于如何通过警报视图做到这一点的想法。

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文件中

    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)

    }

由于

2 个答案:

答案 0 :(得分:6)

你可以直接从SKScenes展示UIAlertControllers,只需在rootViewController上显示它们,这可能是最好的展示它们的地方。

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

一般来说,它不是在SKScenes中引用GameViewController的最佳实践,而我实际上从未达到过被迫这样做的地步。 NSNotificationCenter,委托或协议扩展是更好的方法。

我实际上使用了一个助手,用于使用Swift 2的协议扩展制作的警报。

只需创建一个新的.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)
    }
}

现在,在您的场景中,您需要显示符合协议的警报

class GameScene: SKScene, Alertable {

} 

并调用类似

的方法
showAlert(withTitle: "Alert title", message: "Alert message")

好像他们是场景本身的一部分。

希望这有帮助

答案 1 :(得分:2)

可能有以下选项:

1)快速解决方案。请勿使用UIAlertController,请使用UIAlertView。像那样:

alert.show()

但是,UIAlertView已弃用,因此依赖它并不安全。

2)更好的解决方案。使您的SKScene子类保持对用于呈现场景的视图控制器的引用,并在创建场景时为其分配视图控制器:

myScene.viewController = self

然后你可以使用它。

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