在 GameScene (SpriteKit/Swift) 中显示 UIAlertController [英] Displaying a UIAlertController in GameScene (SpriteKit/Swift)

查看:18
本文介绍了在 GameScene (SpriteKit/Swift) 中显示 UIAlertController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速显示 UIAlertView 的简单方法是:

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

但现在在 iOS 9 中已弃用,建议使用 UIAlertController:

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)

这很好,但是我在使用 SpriteKit 并在 GameScene 中,这给出了一个错误 Value of type 'GameScene' has no member 'presentViewController'...

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'...

我是否需要切换回我的 ViewController 来呈现这个,或者有没有办法从 GameScene 中调用它.

Do I need to switch back to my ViewController to present this or is there a way to call it from a GameScene.

我找到了 THIS 答案,但它是 Objective-C.

I found THIS answer, but it's Objective-C.

推荐答案

有很多方法可以处理这种情况,我不建议 Jozemite Apps 回答,因为这会导致具有多个视图控制器的应用程序出现问题.(你想要在当前视图控制器上显示警报,而不是根)

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

在您的视图控制器中:

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

终于在你要呈现的游戏场景中:

Finally in game scene where you want to present:

self.view.delegate?.sendMessage(message)

这种方式允许对 VC 进行有限访问,并且可以在需要时使用更多选项进行修改.

This way allows limited access to the VC, and can be modified with more options when needed.

另一种方法是设置一个通知系统,使用NSNotificationCenter将消息从场景传递到当前VC,并让它发送消息;

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;

在视图控制器中

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)

另一种方法是将视图控制器指针保存到游戏场景视图:

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.

在视图控制器代码中:私有变量 _instance : UIViewController

In view controller code: private var _instance : UIViewController

class ViewController : UIViewController
{
    class var instance
    {
        get
        {
            return _instance;
        }
    }

    func viewDidLoad()
    {
       _instance = self;
    }
}

然后调用

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 (SpriteKit/Swift) 中显示 UIAlertController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆