Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

ios - 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. 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)
    }


}

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)

    }

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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.

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

Just make a new .swift file and add this code

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...