iOSクイズアプリでは、15秒で質問に答えます。正しく応答された場合は、2番目のビューコントローラが2番目のビューコントローラのボタンのクリックから解除されたものよりも表示され、最初のビューコントローラが再度応答して回答が返されます。ただし、2番目のビューコントローラを閉じるために2番目のビューコントローラのボタンをクリックしたときではなく、質問に対する回答をクリックするとすぐにカウントダウンタイマーが開始されます。 2番目のビューコントローラーのボタンが閉じられ、新しい質問の元のビューコントローラーが表示されるので、すぐにカウントダウンタイマーをリセットします。これを実現させる最善の方法は何ですか?第2のビューコントローラへビューが読み込まれるたびにカウントダウンタイマーをリセットするにはどうすればよいですか?
import UIKit
extension ViewController: QuizCompletedDelegate {
func continueQuiz() {
questionTimer.text = String(counter)
}
}
class ViewController: UIViewController {
//random image func
func randomImage() {
index = Int(arc4random_uniform(UInt32(questionImages.count)))
questionImage.image = questionImages[index]
}
var questionList = [String]()
func updateCounter() {
counter -= 1
questionTimer.text = String(counter)
if counter == 0 {
timer.invalidate()
wrongSeg()
}
}
func randomQuestion() {
//random question
if questionList.isEmpty {
questionList = Array(QADictionary.keys)
continueQuiz()
}
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()
continueQuiz()
}
}
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 {
}
else if (sender.tag != Int(rightAnswerBox)) {
wrongSeg()
print ("Wrong!")
timer.invalidate()
questionList = []
}
}
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
protocol QuizCompletedDelegate {
func continueQuiz()
}
class ContinueScreen: UIViewController {
var delegate: QuizCompletedDelegate?
//correct answer label
@IBOutlet weak var correctLbl: UILabel!
//background photo
@IBOutlet weak var backgroundImage: UIImageView!
func backToQuiz() {
if let nav = self.navigationController {
nav.popViewController(animated: true)
}
else {
self.dismiss(animated: true, completion: nil)
}
}
@IBAction func `continue`(_ sender: Any) {
backToQuiz()
delegate?.continueQuiz()
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
2番目のViewControllerではなくモデルよりも、ショーとして表示された場合は、ビューがまだ決してバックグラウンドでロードされていない、としたため、のviewDidLoadが再び発生しませんアンロード。 LukeSideWalkersソリューションを使用してください。 –