如何在两个独立的视图控制器上跟踪数组的元素?

时间:2017-07-25 06:28:14

标签: ios arrays swift delegates viewcontroller

我正在开发一个测验应用程序,在第一个UIViewController上,用户被问到一个问题并给出了四个按钮作为答案。如果他们得到的答案是正确的,那么该元素将从要问的问题数组中删除。但是,如果我将segue实现到另一个视图控制器,询问用户是否希望在它回到第一个视图控制器时继续询问另一个问题时,阵列会重新填充用户已经回答的问题,即使它们应该是除去。我怎样才能确定一旦我转向第二个视图控制器,它会询问用户是否要继续使用已经回答的问题来解决问题?

这是我的代码:

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: "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()
    print ("Correct!")
}
    if counter != 0 {
        counter = 15
        questionTimer.text = String(counter)
    }
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)
        }

第二个视图控制器的代码:

import UIKit

class ContinueScreen: 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()
}

2 个答案:

答案 0 :(得分:1)

您需要为此实现委托。您可以找到更多信息here

您的问题与this有关。

请仔细阅读。希望有所帮助。

答案 1 :(得分:0)

问题在于 - 当您返回backToQuiz()中的第一个视图控制器时 - 调用performSegue,然后

  • 创建第一个视图控制器的 new 实例和
  • 使用原始(未经修改的)测验数据重新初始化

相反,你应该做类似

的事情
func backToQuiz() { 
   if let nav = self.navigationController {
        nav.popViewController(animated: true)
    } else {
        self.dismiss(animated: true, completion: nil)
    }
}

返回父视图控制器实例。