1

I'm stuck on trying to figure out how to present a UIViewController in a SKScene.

To be more specific I'm working on GameScene.swift and when a image is pressed I want to present the uiviewcontroller.

import SpriteKit

class GameScene: SKScene {



    override func didMoveToView(view: SKView) {
        /* Setup your scene here */


        backgroundColor = SKColor.blackColor()
        var timeBetweenDots = NSTimer.scheduledTimerWithTimeInterval(0.35, target: self, selector: Selector("spawnWhiteDots"), userInfo: nil, repeats: true)
    }



    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */

    }




    func spawnWhiteDots(){

    var dotWhite = SKSpriteNode(imageNamed: "whiteDot.png")

    dotWhite.name = "destroyWhiteDot"

    var randomDotPlacement = arc4random() % 9
    switch (randomDotPlacement){
    case 1:
        dotWhite.position = CGPointMake(self.size.width / 2.92, self.size.height / 1)
        break
    case 2:
        dotWhite.position = CGPointMake(self.size.width / 1.81, self.size.height / 1)
        break
    case 3:
        dotWhite.position = CGPointMake(self.size.width / 2.24, self.size.height / 1)
        break
    case 4:
        dotWhite.position = CGPointMake(self.size.width / 1.52, self.size.height / 1)
        break
    case 5:
        dotWhite.position = CGPointMake(self.size.width / 2.92, self.size.height / 1)
        break
    case 6:
        dotWhite.position = CGPointMake(self.size.width / 2.24, self.size.height / 1)
        break
    case 7:
        dotWhite.position = CGPointMake(self.size.width / 1.52, self.size.height / 1)
        break
    case 8:
        dotWhite.position = CGPointMake(self.size.width / 1.81, self.size.height / 1)
        break
    default:
        break
    }

    let fallAction = SKAction.moveToY(-50, duration: 3.0)

    dotWhite.runAction(SKAction.repeatActionForever(fallAction))

    addChild(dotWhite)

    }


    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        /* Called when a touch begins */
        for touch: AnyObject in touches {
            let location = (touch as UITouch).locationInNode(self)
            if let gameOverDot = self.nodeAtPoint(location).name {
                if gameOverDot == "destroyWhiteDot"{
                    self.removeChildrenInArray([self.nodeAtPoint(location)])
                    ///when the image is pressed I want to present the UIViewController here the name of the viewController is gameOverScene
                }
            }
        }
    }
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
theMighty
  • 11
  • 1
  • possible duplicate of [How do I present a UIViewController from SKScene?](http://stackoverflow.com/questions/19438719/how-do-i-present-a-uiviewcontroller-from-skscene) – JAL Jul 02 '15 at 15:59

2 Answers2

1

The way to do this is to create a delegate so that you can present the View controller from GameViewController.swift

Start by creating a protocol above the class declaration

protocol GameDelegate {
    func launchViewController(#scene: SKScene)
} 

Now, add a property to GameScene to hold this

var gameDelegate: GameDelegate?

And finally, from within the touchesBegan method, call your method.

gameDelegate?.launchViewController(scene: self)

It's customary in iOS to pass self as an argument to to function, that way if you had more than one GameScene you could tell them apart.

Now, in GameViewController, add the protocol implementation

class GameViewController: UIViewController, GameSceneDelegate {

    func launchViewController(scene: SKScene) {
        let vc = // create view controller by storyboard or by custom initialization
        presentViewController(vc, animated: true, completion: nil)
    }

    // ... OTHER STUFF ...
}

Last but not least, put the following line of code in before skView.presentScene(scene)

scene.gameDelegate = self

In this way, the delegate allows you to present the view controller from another view controller

JSquared
  • 179
  • 3
  • 4
0

Here is how i solved it since none of the answers in stackoverflow worked for me had to throw in all the pieces together from different ones.

First set up GameScene.swift Touches Begin

NSNotificationCenter.defaultCenter().postNotificationName("showController", object: nil, userInfo: nil)

Than in GameViewController viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "presentView", name: "showController", object: nil)

Create your function

func presentView() {
self.performSegueWithIdentifier("toMyController", sender: self)
}

My segue is a modal segue.

PS: Remember that gameScene is a view inside GameViewController.