2016-12-06 150 views
-3

我有一个测验应用程序,在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) 
} 
+0

您应该添加您的代码以获取更多详细信息。这是非常基本的,所以你可以在任何地方轻松找到答案。 – Ryan

+0

我添加了我的代码,并请我指向我可以在任何地方找到此答案的基本位置,因此我不必打扰您。对不起,我没有掌握这一点。我只需要一些方向。谢谢@ryan –

回答

0

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

相关问题