2017-07-29 4 views
1

In meiner Quiz-App, wenn ich die Frage Nummer verfolgen wollte der Benutzer war und es anzeigen, wie würde ich das mit meinem folgenden Code tun? Wenn der Benutzer also zwei Fragen beantwortet hat, wenn die dritte erscheint, möchte ich "Frage Nummer 3" anzeigen. Grundsätzlich möchte ich, dass der Benutzer weiß, auf welcher Nummer die Frage steht, und er sollte der Anzahl der Fragen entsprechen, die er beantwortet hat, plus eins.Wie verfolgen Sie die auf einem Etikett beantworteten Artikel?

Hier ist mein Code:

import UIKit 

class ViewController: UIViewController { 


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) 



    } 






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

    } 


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

    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) 

} 



//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. 



} 

Antwort

1

Take-Zähler für die Gesamtzahl der Frage global und es in zufälliger Frage aktualisieren wie

var answerdQuestion = 1; 



    func randomQuestion() { 
    //random question 
     if questionList.isEmpty { 
      questionList = Array(QADictionary.keys) 
     } 
     lblQuestionNumber.text = Strint(answerdQuestion) 
     answerdQuestion += 1 
: 
: 
} 
Verwandte Themen