Search code examples
iosxcodeswiftviewskview

SKView! found nil when presented


class MyClass: UIViewController {

var playerViewController = AVPlayerViewController()
var playerView = AVPlayer()
var playerTimer = NSTimer()


override func viewDidAppear(animated:Bool) {
    super.viewDidAppear(true)
    presentViewController(GameViewController1(), animated: false, completion: nil)
    let fileURL = NSURL(fileURLWithPath: "SomeFile.mp4")

    playerView = AVPlayer(URL: fileURL)

    playerViewController.player = playerView

    self.presentViewController(playerViewController, animated: false){
        self.playerViewController.showsPlaybackControls = false
        self.playerViewController.player?.play()
        self.playerTimer = NSTimer.scheduledTimerWithTimeInterval(4.2, target: self, selector:Selector("stopAfter4seconds:"), userInfo: nil, repeats: false)
    }
}
func stopAfter4seconds(timer: NSTimer){
    playerViewController.player = nil
    playerViewController.removeFromParentViewController()
    playerTimer.invalidate()
    playerViewController.presentViewController(GameViewController1(), animated: false, completion: nil)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()


}

}

After I play a video on 'MyClass' (Class Above)

I then remove the view and present my GameContollerView1 which has the GameScene.
(Class below)

class GameViewController1: UIViewController, ADBannerViewDelegate {


@IBOutlet var skView: SKView!

override func viewDidLoad() {
    super.viewDidLoad()


    if skView.scene == nil {
        let scene = GameScene(size: skView.bounds.size)
        skView.presentScene(scene)
    }
}

override func shouldAutorotate() -> Bool {
    return true
}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        return UIInterfaceOrientationMask.AllButUpsideDown
    } else {
        return UIInterfaceOrientationMask.All
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func prefersStatusBarHidden() -> Bool {
    return true
}

}

My Initial Problem is that when I present the class 'GameViewController1' I get the error:

 fatal error: unexpectedly found nil while unwrapping an Optional value
  (lldb) 

at this line:

if skView.scene == nil {

So it's telling me that my SKView! is nil, which as you can see by my if statement, is what i was looking for anyways.

The weirdest part about this, is that when I skip over 'MyClass' and point my StoryBoard Entry Point to the GameViewContoller1 first, all the code plays correctly.

Does the AVPlayer() have something to do with this? Logically my code should work but for some unknown reason.. it's not. any help would be extremely appreciated.


Solution

  • OP Answer: Ended up going back to the AppDelegate and calling the GameViewController1 class from there. Took a bit to figure it out but this was the overall code that did it for me:

    let gameScene = UIStoryboard(name: "Main", bundle:nil).instantiateViewControllerWithIdentifier("GameViewController1") as UIViewController
        let appDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
        appDelegate.window?.rootViewController = gameScene
    

    I input this code into the ViewDidDisappear as to make my AVplayer Disappear and to load a new view :).. you have no idea how embarrassingly long this took me.

    Tip: Make sure you put this in some kind of ViewDidLoad or one of the view methods, otherwise you will get the error "Expected Declaration"