Swift & SpriteKit - How to present alert view in GameScene

前端 未结 2 773
栀梦
栀梦 2020-12-11 21:35

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

相关标签:
2条回答
  • 2020-12-11 21:51

    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

    0 讨论(0)
  • 2020-12-11 22:13

    There may be the following options:

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

    alert.show()
    

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

    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
    

    And then you can use it.

    self.viewController.presentViewController(alertController, animated: true, completion: nil)
    
    0 讨论(0)
提交回复
热议问题