在GameScene中显示一个UIAlertController(SpriteKit / Swift)
在swift中显示UIAlertView的简单方法是:
let alert = UIAlertView() alert.title = "Alert!" alert.message = "A wise message" alert.addButtonWithTitle("Ok, thank you") alert.show()
但是现在在iOS 9中这已经被折旧了,build议使用UIAlertController
:
let myAlert: UIAlertController = UIAlertController(title: "Alert!", message: "Oh! Fancy", preferredStyle: .Alert) myAlert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) self.presentViewController(myAlert, animated: true, completion: nil)
这是伟大的,但我使用SpriteKit和一个GameScene,这给出了一个Value of type 'GameScene' has no member 'presentViewController'
的Value of type 'GameScene' has no member 'presentViewController'
的错误Value of type 'GameScene' has no member 'presentViewController'
…
我需要切换回我的ViewController来呈现这个,或者有一种方法来从GameScene调用它。
我find了这个答案,但它是Objective-C。
有很多方法来处理这种情况,我不推荐Jozemite应用程序的答案,因为这将导致与多个视图控制器的应用程序的问题(你想在当前的视图控制器,而不是根目录)
我最喜欢的做法是通过授权。 需要做的是创build一个处理消息的协议:
import Foundation protocol ViewControllerDelegate { func sendMessage(message:String); }
在你的视图控制器中:
class ViewController : UIViewController, ViewControllerDelegate { ... func sendMessage(message:String) { //do alert view code here } //in the view controllers view did load event func viewDidLoad() { var view = self.view as! GameSceneView view.delegate = self }
在您的视图代码中:
var delegate : ViewControllerDelegate
最后在你想要呈现的游戏场景中:
self.view.delegate?.sendMessage(message)
这种方式允许有限的访问VC,并可以在需要时用更多的选项进行修改。
另一种方法是build立一个通知系统,使用NSNotificationCenter将消息从场景传递给当前的VC并发送消息;
在ViewController中
func viewDidLoad() { NSNotificationCenter.defaultCenter().addObserver(self,selector:"AlertMessage:",name:"AlertMessage",object:nil); } func AlertMessage(notification:NSNotification) { if(let userInfo = notification.userInfo) { let message = userInfo["message"] ....//do alert view call here } }
在游戏场景代码中:
...at the spot you want to send a message let userInfo = ["message":message]; NSNotificationCenter.defaultCenter.postNotificationNamed("AlertMessage",object:nil,userInfo:userInfo)
另一种方法是将视图控制器指针保存到游戏场景视图:
//in Game Scene View code var viewController : UIViewController; //in the view controllers view did load event func viewDidLoad() { var view = self.view as! GameSceneView view.viewController = self } //finally in game scene where you want to present let myAlert: UIAlertController = UIAlertController(title: "Alert!", message: "Oh! Fancy", preferredStyle: .Alert) myAlert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) self.view.viewController.presentViewController(myAlert, animated: true, completion: nil)
另一种方法是让你的视图控制器是全局的。
在视图控制器代码中:private var _instance:UIViewController
class ViewController : UIViewController { class var instance { get { return _instance; } } func viewDidLoad() { _instance = self; } }
然后就打电话
ViewController.instance!.
每当你需要访问你的视图控制器。
每种方法都有优点和缺点,所以select最适合你的方法。
尝试使用这个。 我在SpriteKit工作,我使用这个代码在我的游戏中的应用程序购买消息,Chomp'd。
self.view?.window?.rootViewController?.presentViewController(myAlert, animated: true, completion: nil)
@ Knight0fDragon的答案很好,但是我觉得有点长。 我将在这里使用另一个解决scheme使用Cocoapoad Podfile为新来者(其他人有同样的问题)。
-
首先,您需要安装cocoapod并为您的项目启动它(非常容易;请查看一些YouTubevideo或此链接 。
-
在你获得的Podfile中,复制并粘贴:
pod 'SIAlertView'
。 这是“一个UIAlertView取代块语法和花哨的过渡风格”。 (更多详细信息请参考您使用的图书馆作者。 -
最后,在GameScene.swift文件中,在导入之后或GameScene类的右括号之后添加以下内容
private extension GameScene { func showPauseAlert() { let alertView = SIAlertView(title: "Edess!!", andMessage: "Congratulations! test testing bla bla bla") alertView.addButtonWithTitle("OK", type: .Default) { (alertView) -> Void in print("ok was pushed") } alertView.show() } }
如果你愿意的话,你可以添加许多带有标题的button,并做任何你想要的操作。 在这里,我只是打印“确定被推”。
上面引用的链接加上这个帮助我在多级游戏中轻松理解和使用这个UIAlertView,在游戏的“暂停”button上显示警报。