SKView!提出时发现没有

时间:2015-12-01 04:06:23

标签: ios xcode swift view skview

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()


}

}

我在' MyClass'上播放视频后(上述课程)

然后我删除视图并呈现具有GameScene的GameContollerView1 (下面的课程)

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
}

}

我最初的问题是,当我提出课程时,GameViewController1'我收到错误:

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

在这一行:

if skView.scene == nil {

所以它告诉我我的SKView!是的,你可以通过我的if语句看到,无论如何我正在寻找。

最奇怪的是,当我跳过MyClass'并首先将我的StoryBoard入口点指向GameViewContoller1,所有代码都正确播放。

AVPlayer()是否与此有关?从逻辑上讲,我的代码应该可以工作,但由于某些未知原因......它不是。任何帮助都将非常感激。

1 个答案:

答案 0 :(得分: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,以使我的AV播放器消失并加载新视图:) ..你不知道这让我感到多么尴尬。

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