2017-07-28 71 views
0

在我的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解決方案。 –

回答

2

你可以實現你的第一個視圖控制器上的功能viewDidAppear(),該功能將自動在你的第二個視圖控制器是被解僱,因此第一個會被看到(再次)調用。

所以在你的第一個視圖控制器創建此功能:

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

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

我的代碼已經包含函數randomQuestion –

+0

你應該確切地調用該方法,他輸入了「把你的復位」這樣的功能,它會被稱爲每次回到這個控制器時間。 – Mozahler

1

的問題,你的代碼是在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