Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在游戏场景中显示UIAlertController(SpriteKit/Swift)_Ios_Swift_Sprite Kit_Uialertview_Uialertcontroller - Fatal编程技术网

Ios 在游戏场景中显示UIAlertController(SpriteKit/Swift)

Ios 在游戏场景中显示UIAlertController(SpriteKit/Swift),ios,swift,sprite-kit,uialertview,uialertcontroller,Ios,Swift,Sprite Kit,Uialertview,Uialertcontroller,在swift中显示UIAlertView的简单方法是: let alert = UIAlertView() alert.title = "Alert!" alert.message = "A wise message" alert.addButtonWithTitle("Ok, thank you") alert.show() 但在iOS 9中,这一点现在有所降低,建议使用UIAlertController: let myAlert: UIAlertController = UIAlertCo

在swift中显示UIAlertView的简单方法是:

let alert = UIAlertView()
alert.title = "Alert!"
alert.message = "A wise message"
alert.addButtonWithTitle("Ok, thank you")
alert.show()
但在iOS 9中,这一点现在有所降低,建议使用
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,在游戏场景中,它给出了一个错误,
类型为“GameSecene”的值没有成员“presentViewController”

我是否需要切换回我的ViewController来演示此功能,或者是否有方法从游戏场景中调用它


我找到了答案,但它是客观的。试着用这个。我在SpriteKit工作,在我的游戏Chomp'd中,我将此代码用于我的应用程序内购买消息

self.view?.window?.rootViewController?.presentViewController(myAlert, animated: true, completion: nil)

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

我喜欢的做法是通过授权。 需要做的是创建一个协议来处理消息:

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,并且可以在需要时使用更多选项进行修改

另一种方法是建立通知系统,使用NSNotificationCenter将消息从场景传递到当前VC,并让其发送消息

在视图控制器中

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
    }
}
class ViewController : UIViewController
{
    class var instance
    {
        get
        {
            return _instance;
        }
    }

    func viewDidLoad()
    {
       _instance = self;
    }
}
在游戏场景代码中:

...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)
另一种方法是使视图控制器全局化

视图中的控制器代码: 私有变量\u实例:UIViewController

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
    }
}
class ViewController : UIViewController
{
    class var instance
    {
        get
        {
            return _instance;
        }
    }

    func viewDidLoad()
    {
       _instance = self;
    }
}
那就打电话吧

ViewController.instance!.  
每当需要访问视图控制器时


这些方法各有优缺点,所以选择最适合你的方法。

来自@Knight0fDragon的答案很好,但我认为有点长。 我将在这里为新来者(其他人也有同样的问题)提供另一个使用Cocoapoad Podfile的解决方案

  • 首先,你需要安装cocoapod并为你的项目启动它(非常简单;查看一些YouTube视频或视频)

  • 在您获得的pod文件中,复制并粘贴以下内容:
    pod'SIAlertView'
    。它是“用块语法和奇特的转换样式替换UIAlertView”。(更多详细信息。请感谢您使用的库作者

  • 最后,在GameSecene.swift文件中,在导入后或GameSecene类的结束括号后添加以下内容

    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()
        }
    }
    
  • 如果你愿意,你可以添加许多带有标题的按钮,并做任何你想做的动作。这里我只打印“Ok was push”


    上面引用的链接帮助我在我的多关卡游戏中轻松理解并使用此UIAlertView,在“暂停”时显示警报按钮。

    不幸的是,只有当根视图控制器是活动视图控制器时,此方法才能很好地工作。如果您有多个视图控制器,则可能会导致问题。KnightOfDragon的回答非常好,并突出显示了一系列非常方便的点。但是很高兴看到这个小方法,我应该只有1个吗viewController:)我不知道它会导致问题,因为我只需要一个视图控制器和多个场景。此答案的swift3版本:self.parent!.scene!.view?.window?.rootViewController?.present(警报,动画:true,完成:nil)哇,回答太棒了。期待尝试这些:D