2017-07-24 46 views
0

我創建一個測驗應用程序,一旦你正確地回答問題,它會繼續到另一個視圖控制器,在那裏有一個標籤,上面寫着「正確答案!」並且包含一個標籤上顯示「繼續?」的按鈕。一個你按下按鈕,它會將你發回問題所在的原始視圖控制器。怎麼一旦你從視圖控制器中繼續「繼續?」按鈕返回到視圖控制器,該視圖控制器詢問數據重置的問題,並向用戶詢問他們已經回答的問題。我怎麼能把它放到「繼續?」的位置按鈕視圖控制器會跟蹤已完成的問題,以便在問題視圖控制器中不再詢問它們嗎?我對swift很陌生,所以我會使用委託或協議?還是那些東西是一樣的?我已經正確地將它從問題視圖控制器延續到「繼續」視圖控制器,但是我希望數據在我回到問題視圖控制器後保持更新,以便再次詢問已經回答的問題。如何確保數組數據在segue期間不會在ios測驗應用程序中重置?

下面是從問題視圖控制器我的代碼:

Import UIKit 

class ViewController: UIViewController { 

    var questionList = [String]() 


func updateCounter() { 

    counter -= 1 
    questionTimer.text = String(counter) 

    if counter == 0 { 


     timer.invalidate() 
     wrongSeg() 
     counter = 15 

    } 



} 


func randomQuestion() { 



    //random question 
    if questionList.isEmpty { 
     questionList = Array(QADictionary.keys) 

     questionTimer.text = String(counter) 




    } 






    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: "wrongViewSegue", sender: self) 

} 

//proceed screen 
func continueSeg() { 

    performSegue(withIdentifier: "continueSeg", 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)) 


{ 
    counter = 15 
    questionTimer.text = String(counter) 
    print ("Correct!") 
    continueSeg() 
} 

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

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

    } 

    randomQuestion() 

} 

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) 


} 

這裏是我的「繼續」視圖控制器代碼(它包含所有現在是SEGUE回到問題視圖控制器,但我想它跟蹤哪些問題已經回答了從存儲在問題視圖控制器問題陣列中的數據):

import UIKit 

class ContinueView: UIViewController { 


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

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

func backToQuiz() { 

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

@IBAction func `continue`(_ sender: Any) { 

    backToQuiz() 
} 



override func viewDidLoad() { 
    super.viewDidLoad() 

} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
+0

使用計數器,將引導您的應用程序,以顯示該問題將數組被調用。就像你在Array中有10個問題,並且用戶已經回答了5個使計數器計數等於5,並且當您從segue導航回到問題屏幕時調用QuestionArray [Count + 1] ..確保您的計數以0開始並且當您調用questiioArray時接下來會調用Question來計數值。在開始計數爲0,所以調用questionsArray [count + 1] –

回答

0

viewDidAppear將您的來電randomQuestionviewDidLoad。 viewDidLoad只調用一次,但每次顯示控制器時都會調用viewDidAppear。

還注意到,Apple

如果視圖控制器是由一個視圖控制器提出了酥料餅內,所提出的控制器駁回後,該方法不調用呈現視圖控制器上。

拿上討論一下about the differences

相關問題