在GameScene中显示UIAlertController(SpriteKit / Swift) [英] Displaying a UIAlertController in GameScene (SpriteKit/Swift)
问题描述
在swift中显示UIAlertView的简单方法是:
let alert = UIAlertView()
alert .title =警报!
alert.message =明智的消息
alert.addButtonWithTitle(好的,谢谢你)
alert.show()
但现在这在iOS 9中已经过折旧,建议使用 UIAlertController
:
让myAlert:UIAlertController = UIAlertController(标题:警告!,消息:哦!Fancy,preferredStyle:.Alert)
myAlert.addAction (UIAlertAction(标题:OK,样式:.Default,handler:nil))
self.presentViewController(myAlert,animated:true,completion:nil)
哪个好,但是我在使用SpriteKit并且在GameScene中,它给出了的错误类型'GameScene'的值没有成员'presentViewController'
...
我是否需要切换回我的ViewController来呈现这个或有没有办法从中调用它一个GameScene。
我发现这个回答,但它是Objective-C。
有很多方法可以解决这种情况,我不推荐Jozemite Apps的答案,因为这个将导致有超过1个视图控制器的应用程序出现问题。(您希望在当前视图控制器上显示警报,而不是根目录)
我喜欢的方式是通过代表团。
需要做的是创建一个处理消息传递的协议:
import Foundation
protocol ViewControllerDelegate
{
func sendMessage(message:String);
}
在您的视图控制器中:
class ViewController:UIViewController,ViewControllerDelegate
{
...
func sendMessage(message:String)
{
//在这里做警报视图代码
}
//在视图控制器视图中执行加载事件
func viewDidLoad()
{
var view = self.view as! GameSceneView
view.delegate = self
}
在您的视图代码中:
var delegate:ViewControllerDelegate
最后在你要呈现的游戏场景中:
self.view.delegate?.sendMessage(message )
这种方式允许对VC的有限访问,并且可以在需要时使用更多选项进行修改。 / p>
另一种方法是设置通知系统,并使用NSNotificationCenter将消息从场景传递到当前VC并让它发送消息;
在ViewController中
func viewDidLoad()
{
NSNotificationCenter 。.defaultCenter()的addObserver(个体,选择器: AlertMessage:,名称: AlertMessage,对象:无);
}
func AlertMessage(通知:NSNotification)
{
if(let userInfo = notification.userInfo)
{
let message = userInfo [message]
.... //在这里做警报视图调用
}
}
在游戏场景代码中:
...当场你想要发送消息
let userInfo = [message:message];
NSNotificationCenter.defaultCenter.postNotificationNamed(AlertMessage,object:nil,userInfo:userInfo)
另一种方法是将视图控制器指针保存到游戏场景视图:
//在游戏场景中查看代码
var viewController:UIViewController;
//在视图控制器视图中确实加载了事件
func viewDidLoad()
{
var view = self.view as! GameSceneView
view.viewController = self
}
//终于在游戏场景中你要呈现
让myAlert:UIAlertController = UIAlertController(标题:警告! ,消息:哦!Fancy,preferredStyle:.Alert)
myAlert.addAction(UIAlertAction(标题:OK,样式:.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!。每当您需要访问视图控制器时,
。
这些方法都有优点和缺点,所以选择最适合你的方式。
The simple way to display a UIAlertView, in swift is:
let alert = UIAlertView()
alert.title = "Alert!"
alert.message = "A wise message"
alert.addButtonWithTitle("Ok, thank you")
alert.show()
But this is now depreciated in iOS 9 and recommends using 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)
Which is great, but I'm using SpriteKit and inside a GameScene, which gives an error of Value of type 'GameScene' has no member 'presentViewController'
...
Do I need to switch back to my ViewController to present this or is there a way to call it from a GameScene.
I found THIS answer, but it's Objective-C.
There are many ways to handle this situation, I do not recommend Jozemite Apps answer, because this will cause problems on apps with more than 1 view controller.(you want to present the alert on the current view controller, not the root)
My preferred way of doing it is through delegation. What needs to be done is create a protocol to handle messaging:
import Foundation
protocol ViewControllerDelegate
{
func sendMessage(message:String);
}
In your view controller:
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
}
In your view code:
var delegate : ViewControllerDelegate
Finally in game scene where you want to present:
self.view.delegate?.sendMessage(message)
This way allows limited access to the VC, and can be modified with more options when needed.
Another way is to set up a notification system, and use NSNotificationCenter to pass a message from the scene to the current VC and have it send a message;
in 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
}
}
In Game scene code:
...at the spot you want to send a message
let userInfo = ["message":message];
NSNotificationCenter.defaultCenter.postNotificationNamed("AlertMessage",object:nil,userInfo:userInfo)
Another approach is to save the view controller pointer to game scene view:
//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)
Yet another way is to make your view controller global.
In view controller code: private var _instance : UIViewController
class ViewController : UIViewController
{
class var instance
{
get
{
return _instance;
}
}
func viewDidLoad()
{
_instance = self;
}
}
Then just call
ViewController.instance!.
whenever you need access to your view controller.
Each of these methods have there strengths and weaknesses, so choose whatever way works best for you.
这篇关于在GameScene中显示UIAlertController(SpriteKit / Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!