如何在另一个视图控制器上启动视图控制器?

时间:2016-12-06 19:12:58

标签: swift xcode

我有一个测验应用程序,询问triviaviewcontroller中的问题,在10个问题之后,它会转移到videoviewcontroller,然后播放视频然后将其移回triviaviewcontroller。问题是它从一开始就重新启动问题,而不是问题11.我是否需要从triviaviewcontroller启动视频或我需要改变哪种方法?

@IBAction func submitButton(_ sender: AnyObject) {

        if noWhitespaceUserAnswer == answers[currentQuestionIndex]
        {
            self.currentQuestionTimerLabel.text = ""

            answerField.text = ""
            currentQuestionIndex = currentQuestionIndex + 1 < questions.count ? currentQuestionIndex + 1 : 0
            if (currentQuestionIndex == 4){
                performSegue(withIdentifier: "videoview", sender: self)
                                }
            nextQuestionLabel.text = questions[currentQuestionIndex]


            animateLabelTransitions()

        }
        else {
            incorrectAnswerHoldLabel.isHidden = false
            submitbuttonwronganswer.isHidden = true
            let time = DispatchTime(uptimeNanoseconds:UInt64(0.1) ) + Double(4 * Int64(NSEC_PER_SEC)) / Double(NSEC_PER_SEC)
            DispatchQueue.main.asyncAfter(deadline: time) {
                self.submitbuttonwronganswer.isHidden = false
                self.incorrectAnswerHoldLabel.isHidden = true
            }

            answerField.text = ""

        }
    }

这是我的videoviewcontroller。

 class VideoViewController:  AVPlayerViewController,      AVPlayerViewControllerDelegate {
fileprivate var firstAppear = true

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    if firstAppear {
        do {
            try playVideo()

            firstAppear = false
        } catch AppError.invalidResource(let name, let type) {
            debugPrint("Could not find resource \(name).\(type)")
        } catch {
            debugPrint("Generic error")
        }
    }
}

fileprivate func playVideo() throws {

    guard let path = Bundle.main.path(forResource: "RossCliffJumping", ofType:"m4v") else {
        throw AppError.invalidResource("RossCliffJumping", "m4v")
    }
    let player = AVPlayer(url: URL(fileURLWithPath: path))
    let playerController = AVPlayerViewController()
    playerController.player = player
    playerController.showsPlaybackControls = false
    NotificationCenter.default.addObserver(self, selector: #selector(VideoViewController.itemDidFinishPlaying(_:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)
    self.present(playerController, animated: true) {
        player.play()

    }

}
func itemDidFinishPlaying(_ notification:Notification) {
    dismiss(animated: true, completion: nil)
    performSegue(withIdentifier: "videofinished", sender: self)
    print("finished")
  }
  deinit{
      NotificationCenter.default.removeObserver(self)
  }

}
    enum AppError : Error { case invalidResource(String, String)
}

1 个答案:

答案 0 :(得分:0)

您已经启动了另一个视图控制器。从您的问题的声音,似乎您想要调用self.dismiss(animated: true, completion: nil)而不是执行另一个segue,以回到第一个控制器。通过调用performSegue,您正在创建一个新的控制器,当然这将回到问题1。