如何让我的倒数计时器重置为每次加载视图时?

问题描述:

在我的iOS测验应用程序中,您可以在15秒内回答问题。如果回答正确,第二个视图控制器会出现,而不会被点击第二个视图控制器上的按钮而被解除,并且第一个视图控制器会重新出现,并且会有新的问题被回答。但是,只要单击问题的答案,而不是单击第二个视图控制器上的按钮以关闭第二个视图控制器,倒数计时器就会启动。我希望倒数计时器在第二个视图控制器上的按钮关闭后立即重置,并用新问题显示原始视图控制器。实现这一目标的最佳方式是什么?如何让我的倒数计时器重置为每次加载视图时?

这里是我的代码(第一个视图控制器):

import UIKit 

extension ViewController: QuizCompletedDelegate { 
    func continueQuiz() { 
     questionTimer.text = String(counter) 
    } 
} 

class ViewController: UIViewController { 

    //random image func 
    func randomImage() { 
     index = Int(arc4random_uniform(UInt32(questionImages.count))) 
     questionImage.image = questionImages[index] 
    } 

    var questionList = [String]() 

    func updateCounter() { 
     counter -= 1 
     questionTimer.text = String(counter) 

     if counter == 0 {    
      timer.invalidate() 
      wrongSeg() 
     } 
    } 

    func randomQuestion() {   
     //random question 
     if questionList.isEmpty { 
      questionList = Array(QADictionary.keys) 
      continueQuiz()    
     } 

     let rand = Int(arc4random_uniform(UInt32(questionList.count))) 
     questionLabel.text = questionList[rand] 

     //matching answer values to go with question keys 
     var choices = QADictionary[questionList[rand]]! 

     questionList.remove(at: rand) 

     //create button 
     var button:UIButton = UIButton() 

     //variables 
     var x = 1 
     rightAnswerBox = arc4random_uniform(4)+1 

     for index in 1...4 
     { 
      button = view.viewWithTag(index) as! UIButton 

      if (index == Int(rightAnswerBox)) 
      { 
       button.setTitle(choices[0], for: .normal) 
      } 
      else { 
       button.setTitle(choices[x], for: .normal) 
       x += 1  
      } 
      randomImage() 
      continueQuiz() 
     } 
    } 

    let QADictionary = ["Who is Thor's brother?" : ["Atum", "Loki", "Red Norvell", "Kevin Masterson"], "What is the name of Thor's hammer?" : ["Mjolinr", "Uru", "Stormbreaker", "Thundara"], "Who is the father of Thor?" : ["Odin", "Sif", "Heimdall", "Balder"]] 

    //wrong view segue 
    func wrongSeg() {   
     performSegue(withIdentifier: "incorrectSeg", sender: self)   
    } 

    //proceed screen 
    func rightSeg() {  
     performSegue(withIdentifier: "correctSeg", sender: self) 
    } 

    //variables 
    var rightAnswerBox:UInt32 = 0 
    var index = 0 

    //Question Label 
    @IBOutlet weak var questionLabel: UILabel! 

    //Answer Button 
    @IBAction func buttonAction(_ sender: AnyObject) { 
     if (sender.tag == Int(rightAnswerBox)) 
     { 
      rightSeg() 
      print ("Correct!") 
     } 

     if counter != 0 { 

     } 
     else if (sender.tag != Int(rightAnswerBox)) { 
      wrongSeg() 
      print ("Wrong!") 
      timer.invalidate() 
      questionList = [] 
     } 
    } 

    override func viewDidAppear(_ animated: Bool) 
    { 
     randomQuestion() 
    } 

    //variables 
    var counter = 15 

    var timer = Timer() 

    @IBOutlet weak var questionTimer: UILabel! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     timer = Timer.scheduledTimer(timeInterval: 1, target:self, selector: #selector(ViewController.updateCounter), userInfo: nil, repeats: true)   
    } 

代码到第二视图控制器:

import UIKit 

protocol QuizCompletedDelegate { 
    func continueQuiz() 
} 

class ContinueScreen: UIViewController { 

    var delegate: QuizCompletedDelegate? 

    //correct answer label 
    @IBOutlet weak var correctLbl: UILabel! 

    //background photo 
    @IBOutlet weak var backgroundImage: UIImageView! 

    func backToQuiz() { 
     if let nav = self.navigationController { 
      nav.popViewController(animated: true)  
     } 
     else { 
      self.dismiss(animated: true, completion: nil) 
     } 
    } 

    @IBAction func `continue`(_ sender: Any) { 
     backToQuiz() 
     delegate?.continueQuiz() 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 
} 
+0

如果你的第二个视图控制器显示为表演,而不是模型,viewDidLoad中不会再次触发,因为有人在后台仍然加载,从不卸载。使用LukeSideWalkers解决方案。 –

你可以实现你的第一个视图控制器上的功能viewDidAppear(),该功能将自动在你的第二个视图控制器是被解雇,因此第一个会被看到(再次)调用。

所以在你的第一个视图控制器创建此功能:

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

    // HERE PUT YOUR TIMER RESET 
    // ... 
} 
+0

我的代码已经包含函数randomQuestion –

+0

你应该确切地调用该方法,他输入了“把你的复位”这样的功能,它会被称为每次回到这个控制器时间。 – Mozahler

的问题,你的代码是在buttonAction。如果选择了错误的答案,那么你的计时器就是invalidate。但是,如果你的答案是正确的,你对计时器不做任何事情,并允许它继续。

要暂停计时器,您需要使无效,然后重新创建它。所以,你可以做到这一点:

//Answer Button 
@IBAction func buttonAction(_ sender: AnyObject) { 
    if (sender.tag == Int(rightAnswerBox)) 
    { 
     timer.invalidate() // Pause the current timer 
     rightSeg() 
     print ("Correct!") 
    } 

    if counter != 0 { 

    } 
    else if (sender.tag != Int(rightAnswerBox)) { 
     wrongSeg() 
     print ("Wrong!") 
     timer.invalidate() 
     questionList = [] 
    } 
} 

再次创建定时器,你需要添加:

timer = Timer.scheduledTimer(timeInterval: 1, target:self, selector: #selector(ViewController.updateCounter), userInfo: nil, repeats: true) 

出现的观点时,你想补充一点定时器:

override func viewDidAppear(_ animated: Bool) 
    { 
     timer = Timer.scheduledTimer(timeInterval: 1, target:self, selector: #selector(ViewController.updateCounter), userInfo: nil, repeats: true) 

    } 

请注意,如果你在viewDidAppear添加一个计时器,你应该删除定时器viewDidLoad,否则你将创建两个定时器,当你的应用程序加载。

在去除viewDidLoad中的计时器,该功能现在应该是这样的:

override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

} 

总之,这里的想法是:当你得到正确的答案,要暂停定时器(使用定时器。无效),然后当视图出现时,您将创建一个新的计时器。

+0

非常有见地,感谢您的意见和清晰! –

+0

如何删除'viewDidLoad'部分的时间? –

+0

我已编辑上面的帖子。 – Baleroc