gpt4 book ai didi

ios - 如何在两个单独的 View Controller 上跟踪数组的元素?

转载 作者:行者123 更新时间:2023-11-28 06:15:24 25 4
gpt4 key购买 nike

我正在开发一个测验应用程序,在第一个 UIViewController 上,系统会向用户提出一个问题,并给出四个按钮作为答案。如果他们得到正确的答案,则该元素将从要问的问题数组中删除。但是,如果我对另一个 View Controller 实现一个 segue,当它返回到第一个 View Controller 询问另一个问题时询问用户是否要继续,该数组将重新填充用户已经回答的问题,即使他们应该是删除。我如何确保一旦我转到第二个 View Controller 并从第二个 View Controller 询问用户是否要继续,问题数组不会重新填充已回答的问题?

这是我的代码:

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

第二个 View Controller 的代码:

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

最佳答案

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

您的问题与this有关.

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

关于ios - 如何在两个单独的 View Controller 上跟踪数组的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45295474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com