1
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.

4

1 回答 1

0

OP 答案:最终回到 AppDelegate 并从那里调用 GameViewController1 类。花了一点时间才弄清楚,但这是为我完成的整体代码:

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

我将此代码输入 ViewDidDisappear 以使我的 AVplayer 消失并加载新视图:).. 你不知道这花了我多长时间。

提示:确保将其放入某种 ViewDidLoad 或其中一种视图方法中,否则您将收到错误“预期声明”

于 2015-12-01T08:38:06.853 回答