gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:17:09 24 4
gpt4 key购买 nike

我正在创建一个测验应用程序,一旦您正确回答了问题,它就会转到另一个 View Controller ,其中有一个标签显示“正确答案!” a 包含一个按钮,标签上写着“继续?”。当您按下按钮时,它会将您带回询问问题的原始 View Controller 。你曾经如何使用“继续”从 View Controller 中分离出来?按钮返回到 View Controller ,询问数据重置的问题,并向用户询问他们已经回答过的问题。我怎样才能到达“继续”的地方?按钮 View Controller 会跟踪已完成的问题,这样就不会在问题 View Controller 中再次询问它们?我是 swift 新手,所以我会使用委托(delegate)还是协议(protocol)?或者那些东西是一样的吗?我已经正确地将其从问题 View Controller 连接到“继续” 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: "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)


}

这是我的“继续” View Controller 的代码(它现在包含的只是返回到问题 View Controller 的序列,但我希望它通过存储数组中的数据来跟踪哪些问题已经得到解答问题 View Controller 中的问题):

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

最佳答案

将对 randomQuestion 的调用从 viewDidAppear 移至 viewDidLoad。 viewDidLoad 仅调用一次,但 viewDidAppear 每次显示 Controller 时都会被调用。

另请注意 Apple

If a view controller is presented by a view controller inside of a popover, this method is not invoked on the presenting view controller after the presented controller is dismissed.

查看讨论 about the differences

关于arrays - 如何确保在 ios 测验应用程序中在 segue 期间不会重置数组数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45271450/

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