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

时间:2023-01-23 22:19:54

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.

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

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

2 个解决方案

#1


1  

There may be the following options:

可能有以下选项:

1) Quick solution. Do not use UIAlertController, use UIAlertView. Like that:

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

alert.show()

However, UIAlertView is deprecated so it's not quite safe to rely on it.

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

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:

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

myScene.viewController = self

And then you can use it.

然后你可以使用它。

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

#2


5  

You can show UIAlertControllers directly from SKScenes, simply show them on the rootViewController, which is probably the best place to show them anyway.

您可以直接从SKScenes显示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. NSNotificationCenter, delegation or protocol extensions are the better way.

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

I actually use a helper for Alerts I made using Swift 2's protocol extensions.

我实际上使用了一个使用Swift 2协议扩展的Alerts帮助程序。

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

希望这可以帮助

#1


1  

There may be the following options:

可能有以下选项:

1) Quick solution. Do not use UIAlertController, use UIAlertView. Like that:

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

alert.show()

However, UIAlertView is deprecated so it's not quite safe to rely on it.

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

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:

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

myScene.viewController = self

And then you can use it.

然后你可以使用它。

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

#2


5  

You can show UIAlertControllers directly from SKScenes, simply show them on the rootViewController, which is probably the best place to show them anyway.

您可以直接从SKScenes显示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. NSNotificationCenter, delegation or protocol extensions are the better way.

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

I actually use a helper for Alerts I made using Swift 2's protocol extensions.

我实际上使用了一个使用Swift 2协议扩展的Alerts帮助程序。

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

希望这可以帮助