当视图不在窗口层次结构中时,呈现视图控制器

时间:2015-11-28 20:43:05

标签: xcode

import AVFoundation
import UIKit
import AVKit

class Class1: UIViewController {


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


override func viewDidAppear(animated:Bool) {
    let fileURL = NSURL(fileURLWithPath: "file.mp4")

    playerView = AVPlayer(URL: fileURL)

    playerViewController.player = playerView

    self.presentViewController(playerViewController, animated: true){
        self.playerViewController.player?.play()
        self.playerTimer = NSTimer.scheduledTimerWithTimeInterval(4.0, target: self, selector:Selector("stopAfter4seconds:"), userInfo: nil, repeats: false)
    }

}
func stopAfter4seconds(timer: NSTimer){
    self.playerViewController.player?.pause()
    self.playerViewController.player = nil
    self.presentingViewController?.dismissViewControllerAnimated(false, completion: nil)
    self.presentViewController(GameViewController1(), animated: false, completion: nil)
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()


}

}

我的问题是,当我提出这一行:

self.presentViewController(GameViewController1(), animated: false, completion: nil)

我收到错误“尝试呈现GameViewController1 ......其视图不在窗口层次结构中!”

我确保在viewDidAppear中将其添加到视图层次结构中,但它似乎没有这样做

[更新]好了我现在意识到我正在呈现avplayerviewcontroller然后尝试播放下一个视图控制器。我如何“解雇”AVplayerviewcontroller以及我在右窗口层次结构中的下一个视图???

1 个答案:

答案 0 :(得分:0)

答案:我用'自我'来表达我的下一个观点。而不是与存在的视图控制器,这恰好是playerViewController所以这行代码适合我。

playerViewController.presentViewController(GameViewController1(), 
animated: false, completion: nil)