2

Note:

There are several questions (or resources) already out there in relation to this:

So I am sorry for adding a similar question. However, I have attempted to implement those solutions without success.

To make this question a bit more concrete than the aforementioned question, let's use the M.W.E. (Stack Overflow SKSpriteKit) presented in two other Swift related questions:

Context:

In the M.W.E. there is one main UIViewController, GameViewController that calls forth the main menu scene, which is an SKView. The various custom SKViews link cyclicly:

menu -> difficulty -> game -> score -> menu -> etc

I mention this, because in most of the above questions, one must add the UIAlertController to the main UIViewController. I just want to make sure that only the GameScene can present the UIAlertController and none of the other scenes.

Question:

How can I implement a UIAlertController callable from only one of the SKViews? Some of the previous answers, have only the main GameViewController and GameScene, and then give the GameScene (a constant in the GameViewController) a reference to the GameViewController. Since in this code, the GameScene isn't accessed until much later, how can this be achieved (ideally without propagating a reference to the GameViewController through the various SKViews.

Please use the M.W.E. when answering this question.

Goal:

In another related question, Swift3: UISwipeGestureRecognizer in only one of putatively many SKViews, it posed as to how to implement a UIGestureRecognizer for one SKView. Ideally, the UIAlertController would be presented following that action - as sort of an in game menu.

Minimum Working Example

StackOverflow SKSpriteKit

Community
  • 1
  • 1
SumNeuron
  • 4,850
  • 5
  • 39
  • 107
  • 1
    I think it's been pointed out to you before, but let me make it very clear: SpriteKit and UIKit don't play well together. If you can do it all in SpriteKit, you'll have a lot easier way through this and all subsequent problems. Put another way, your desire to use storyboards AND SpriteKit is your biggest source of problems. Nobody has, to my knowledge, found a happy way to work this way. – Confused Jan 24 '17 at 08:21
  • Put another way: SpriteKit and GameplayKit are designed to work together, explicitly. And that's not fun, nor well explained or articulated, and hardly used. Expecting two frameworks at odds with each other (SKScene vs UIViewControllers) to find happy ways to play together is asking a lot of the frameworks that they simply don't seem to have been designed to do. They're not happy campers. – Confused Jan 24 '17 at 08:24
  • @Confused the M.W.E. no longer uses UIKit or storyboards for handling the menus. If you have a better way of implementing a pop-up pause-menu, then I am eager to learn from you – SumNeuron Jan 24 '17 at 08:48
  • 1
    @SumNeuron Upvoted you for your improving to make questions, well done. Minimum working example is the best thing you can put in your questions, this helping a lot, that's great. – Alessandro Ornano Jan 24 '17 at 09:12

2 Answers2

3

UIAlert is an UIKit element, so you can easily present where do you want in your SKScene take references from your view with few code.. In other words you can access to the main window that display your app’s content, then use the rootViewController that provides the content view of the window.

Code sample:

if sprite.contains(touchLocation) {
            print("You tapped the blue sprite")
            let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
            let action = UIAlertAction(title: "Ok", style: .default) { action in
                // Handle when button is clicked
            }
            alert.addAction(action)
            if let vc = self.scene?.view?.window?.rootViewController {
               vc.present(alert, animated: true, completion: nil)
            }
}
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
0

Seems like a long question. Do you want to show a UIAlertController in SKScenes? There is no need to reference the GameViewController from your SKScenes, just show the alerts on the root view controller.

So to present directly from one of your SKScenes you can say this

 view?.window?.rootViewController?.present(YOURALERTCONTROLLER, animated: true)

Hope this helps

crashoverride777
  • 10,581
  • 2
  • 32
  • 56
  • Ill have to recreate them, I was just trying whatever I could find on the forums. This was one suggestion... Perhaps you could demo on the M.W.E – SumNeuron Jan 24 '17 at 12:46
  • That MWE thing is a simple SpriteKit project. To show a UIAlertController for example in GameScene.swift you create it at usual and than present it like in my answer. – crashoverride777 Jan 24 '17 at 12:50