Swift&SpriteKit – 如何在GameScene中显示警报视图

我需要在游戏场景中提供一个警报视图的帮助。 我目前正在努力做到这一点,因为GameScene.Swift不是一个标准的ViewController。 如果有帮助,我需要这样做,因为我需要用户input一个值作为我的游戏中的Sprite Sprite Kit Node的坐标。 input只是一个标准的整数,所以不是一个问题。 任何其他的想法,我怎么能做到这一点,而不是通过一个警报视图也是受欢迎的。

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

谢谢

可能有以下select:

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

 alert.show() 

但是, UIAlertView已被弃用,所以依赖它是不安全的。

2)更好的解决scheme。 让你的SKScene子类持有一个对你用来呈现场景的视图控制器的引用,当你创build场景时,把它分配给视图控制器:

 myScene.viewController = self 

然后你可以使用它。

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

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

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

一般来说,在SKScenes中引用GameViewController并不是最好的做法,我从来没有真正达到过这样的地步。 NSNotificationCenter,委托或协议扩展是更好的方法。

实际上,我使用Swift 2的协议扩展提供的帮助。

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

就好像它们是场景本身的一部分一样。

希望这可以帮助