2017-07-29 17 views
1

我正在開發一個測驗應用程序,當你正確地回答一個問題時,它會延伸到一個視圖控制器,如果你按錯誤的答案,它會繼續到另一個視圖控制器。但是,當我運行模擬器,如果我點擊一個錯誤的答案,除了倒數計時器重置除外。但是,如果我點擊正確的答案,然後該塞格斯工作得很好。爲什麼是這樣?我的一個賽段怎麼不起作用?

這裏是我的代碼:

import UIKit 

class ViewController: UIViewController { 

    var questionList = [String]() 
    var counter = 15 
    var timer = Timer() 
    var rightAnswerBox:UInt32 = 0 
    var index = 0 

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

    @IBOutlet weak var questionTimer: UILabel! 


    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) 
     } 

     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() 

    } 

    //dictionary filled with question keys and answer values 
    let QADictionary = [:] 


    //wrong view segue 
    func wrongSeg() { 

     performSegue(withIdentifier: "incorrectSeg", sender: self) 

    } 

    //proceed screen 
    func rightSeg() { 

     performSegue(withIdentifier: "correctSeg", sender: self) 
    } 

    //Answer Button 
    @IBAction func buttonAction(_ sender: AnyObject) { 

     if (sender.tag == Int(rightAnswerBox)) { 

      rightSeg() 
      timer.invalidate() 
      print ("Correct!") 
     } 

     if counter != 0 { 

      counter = 15 

     } else if (sender.tag != Int(rightAnswerBox)) { 

      wrongSeg() 
      print ("Wrong!") 
      timer.invalidate() 
      questionList = [] 

     } 
    } 

    override func viewDidAppear(_ animated: Bool) { 

     randomQuestion() 

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

    } 
} 
+0

提供您的故事板顯示SEGUE – Timmy

+0

@DharmeshKheni我怎麼就在這裏分享項目的形象? –

回答

1

你已經把錯誤的條件內buttonAction我已經改正了,現在檢查。只需更換否則,如果condiotion

@IBAction func buttonAction(_ sender: AnyObject) { 

     if (sender.tag == Int(rightAnswerBox)) { 

      rightSeg() 
      timer.invalidate() 
      print ("Correct!") 
     }/*Here is change*/ 
     else if (sender.tag != Int(rightAnswerBox)) 
     { 

      wrongSeg() 
      print ("Wrong!") 
      timer.invalidate() 
      questionList = [] 

     } 
     if counter != 0 { 

      counter = 15 

     } 
    } 
+0

謝謝,因爲您的洞察力,它現在有效! –

相關問題