1

I want to present a GameScene with GameViewController from my ViewController. I tried:

// let GameControllerA2 = GameViewController()

            // let transition = CATransition()
            // transition.duration = 0.5
            // transition.type = kCATransitionPush
            // transition.subtype = kCATransitionFromRight
            // view.window!.layer.add(transition, forKey: kCATransition)

            // self.present(GameControllerA2, animated: true, completion: nil)

            if let vc3 = self.storyboard?.instantiateViewController(withIdentifier: "A2ID") as? GameViewControllerA2 {
                let appDelegate = UIApplication.shared.delegate as! AppDelegate
                appDelegate.window?.rootViewController!.present(vc3, animated: true, completion: nil)

Code Photo

But this all doesn't work. Just if I simply drag [Button] from the Storyboard to the the GameViewController it doesn't crash, but I need it programmatically. Any questions about my process? Ask, I'll reply.

Bryan Ash
  • 4,385
  • 3
  • 41
  • 57
user6083214
  • 63
  • 1
  • 8

2 Answers2

0

First you need to assign a storyboard identifier to the view controller you want to present on top of your current controller. You can do that in the storyboard in the identity inspector section

Let that identifier be "StoryboardIdentifierForNewClass" and the class name be NewClassViewController. Then,

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("StoryboardIdentifierForNewClass") as! NewClassViewController
self.presentViewController(vc, animated: true, completion: nil)
Abhishek729
  • 327
  • 5
  • 14
  • This code crashes. "libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)" In my ViewController, i added this code to my func. I don't want in the GameScene to show a ViewController, i want in my ViewController to show the GameScene. – user6083214 Nov 07 '16 at 17:26
0

The idea is pretty simple.

First of all you need to add an SKView to your view controller.

Next you create a GameScene object and you add it to the SKView

view.presentScene(scene)

That's it. Now you have your GameScene inside your ViewController.

Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
  • yeahr, this is in my GameViewController. But now i want to present this GameViewController from an other ViewController. This other ViewController has nothing to do with the GameScene. Can you help me? – user6083214 Nov 07 '16 at 17:21