如何确保数组数据在segue期间不会在ios测验应用程序中重置?

问题描述:

我创建一个测验应用程序,一旦你正确地回答问题,它会继续到另一个视图控制器,在那里有一个标签,上面写着“正确答案!”并且包含一个标签上显示“继续?”的按钮。一个你按下按钮,它会将你发回问题所在的原始视图控制器。怎么一旦你从视图控制器中继续“继续?”按钮返回到视图控制器,该视图控制器询问数据重置的问题,并向用户询问他们已经回答的问题。我怎么能把它放到“继续?”的位置按钮视图控制器会跟踪已完成的问题,以便在问题视图控制器中不再询问它们吗?我对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] –

viewDidAppear将您的来电randomQuestionviewDidLoad。 viewDidLoad只调用一次,但每次显示控制器时都会调用viewDidAppear。

还注意到,Apple

如果视图控制器是由一个视图控制器提出了酥料饼内,所提出的控制器驳回后,该方法不调用呈现视图控制器上。

拿上讨论一下about the differences