我创建一个测验应用程序,一旦你正确回答问题,它就会转移到另一个视图控制器,其中有一个标签上写着“正确的答案!”并且a包含一个标签显示“Continue?”的按钮。按下按钮,它会将您发送回原始视图控制器,在那里询问问题。一旦你从视图控制器中取消“继续?”按钮返回到视图控制器,该控制器询问数据被重置的问题,并向用户询问他们已经回答的问题。我怎么能去“继续?”按钮视图控制器跟踪已完成的问题,以便在问题视图控制器中不再询问它们?我是swift的新手,所以我会使用委托还是协议?或者那些东西是一样的吗?我已经正确地将它从问题视图控制器转移到“继续”视图控制器但是我希望数据在我回到问题视图控制器后保持更新,因此不会再次询问已经回答的问题。
以下是我在问题视图控制器中的代码:
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 :(得分:0)
将您的来电从randomQuestion
移至viewDidAppear
至viewDidLoad
。 viewDidLoad只调用一次,但每次显示控制器时都会调用viewDidAppear。
另请注意Apple说
如果视图控制器由弹出框内的视图控制器呈现,则在呈现的控制器被解除后,不会在呈现视图控制器上调用此方法。