2017-07-29 1 views
1

Je développe une application de quiz et lorsque vous répondez correctement à une question, elle se met en relation avec un contrôleur de vue et si vous appuyez sur la mauvaise réponse, elle se met en relation avec un autre contrôleur de vue. Cependant, lorsque je lance le simulateur, si je clique sur une mauvaise réponse, rien ne se passe à l'exception de la réinitialisation du compte à rebours. Cependant, si je clique sur la bonne réponse, alors cela fonctionne très bien. Pourquoi est-ce?Comment se fait-il qu'une de mes pages ne fonctionne pas?

Voici mon code:

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

Fournir une image de votre story-board montrant les Segue – Timmy

+0

@DharmeshKheni comment puis-je partager le projet ici? –

Répondre

1

Vous avez mis mal état à l'intérieur buttonAction je l'ai corrigé vérifier maintenant. il suffit de remplacer d'autre si 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

Merci, cela fonctionne maintenant à cause de votre perspicacité! –