Displaying a UIAlertController in GameScene (SpriteKit/Swift)

后端 未结 3 909
青春惊慌失措
青春惊慌失措 2021-01-15 00:39

The simple way to display a UIAlertView, in swift is:

let alert = UIAlertView()
alert.title = \"Alert!\"
alert.message = \"A wise message\"
alert.addButtonWi         


        
相关标签:
3条回答
  • 2021-01-15 01:21

    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.

    0 讨论(0)
  • 2021-01-15 01:26

    Try using this. I work in SpriteKit and I used this code for my in app purchase messages in my game, Chomp'd.

    self.view?.window?.rootViewController?.presentViewController(myAlert, animated: true, completion: nil)
    
    0 讨论(0)
  • 2021-01-15 01:32

    The answer from @Knight0fDragon is nice, but I think it's a little long. I will just put here another solution using Podfile of Cocoapoad for new comers (others guys having the same problem).

    1. first you need to install cocoapod and initiate it for your project (very easy to do; check some YouTube video or this link.

    2. in your obtained Podfile, copy, and paste this: pod 'SIAlertView'. It is "A UIAlertView replacement with block syntax and fancy transition styles". (More details here. Please give credit to the libraries Authors you're using.

    3. Finally, in your GameScene.swift file add the following after the import or after the closing bracket of the class 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()
          }
      }
      

    You can add many button with title if you want, and do whatever action you want. Here I just print "Ok was pushed".

    The above cited links plus this one helped me to understand and work with this UIAlertView easily in my multiple-level game, to display alert on the "Pause" button of the game.

    0 讨论(0)
提交回复
热议问题