[英]Swift - Why is my function within a function not being called?
我正在為一個簡單的測驗應用程序開發一個項目。
import UIKit
class ViewController: UIViewController {
let allQuestions = QuestionBank()
var pickedAnswer: Bool = false
var questionNumber: Int = 0
//Place your instance variables here
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var scoreLabel: UILabel!
@IBOutlet var progressBar: UIView!
@IBOutlet weak var progressLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let firstQuestion = allQuestions.list[0]
questionLabel.text = firstQuestion.questionText
}
@IBAction func answerPressed(_ sender: AnyObject) {
if sender.tag == 1 {
pickedAnswer = true
} else if sender.tag == 2 {
pickedAnswer = false
}
checkAnswer()
questionNumber = questionNumber + 1
nextQuestion()
}
func updateUI() {
}
func nextQuestion() {
if questionNumber <= 12 {
questionLabel.text = allQuestions.list[questionNumber].questionText
}
else {
let alert = UIAlertController(title: "Quiz Complete", message: "You have completed the quiz, do you want to start over?", preferredStyle: .alert)
let restartAction = UIAlertAction(title: "Restart", style: .default, handler: { (UIAlertAction) in
self.startOver()
})
alert.addAction(restartAction)
present(alert, animated: true, completion: nil)
}
}
func checkAnswer() {
let correctAnswer = allQuestions.list[questionNumber].answer
if correctAnswer == pickedAnswer {
print("You got it!")
} else {
print("Wrong!")
}
}
func startOver() {
questionNumber = 0
nextQuestion()
}
}
用戶可以對一組問題回答正確或錯誤。 問題存儲在單獨文件中的數組 0 - 12 中。 有一個變量來確定問題編號。
var questionNumber: Int = 0
在每個問題之后運行的函數中都有一個 if/else 語句 - 如果問題編號變量 <= 12,則提出下一個問題並且問題編號變量增加 1。
@IBAction func answerPressed(_ sender: AnyObject) {
if sender.tag == 1 {
pickedAnswer = true
} else if sender.tag == 2 {
pickedAnswer = false
}
checkAnswer()
questionNumber = questionNumber + 1
nextQuestion()
}
func updateUI() {
}
func nextQuestion() {
if questionNumber <= 12 {
questionLabel.text = allQuestions.list[questionNumber].questionText
}
else {
let alert = UIAlertController(title: "Quiz Complete", message: "You have completed the quiz, do you want to start over?", preferredStyle: .alert)
let restartAction = UIAlertAction(title: "Restart", style: .default, handler: { (UIAlertAction) in
self.startOver()
})
alert.addAction(restartAction)
present(alert, animated: true, completion: nil)
}
否則,系統會詢問他們是否要重新啟動。
目標是當用戶遇到問題 #12 時,他們會看到一個 UIAlertView 來祝賀他們,並提供一個使用“重新啟動”按鈕重新啟動的機會。
“重新啟動”按鈕運行一個函數,該函數將問題編號變量設置回 0,並且應該以詢問第一個問題的 if/else 語句開始該函數。 (更改 UI 文本)
func startOver() {
questionNumber = 0
nextQuestion()
}
但是,按“重新啟動”時,UI 文本不會從最后一個問題更改為第一個問題,並且啟動更改的唯一方法是按“真”或“假”按鈕。
這運行了一個不同的函數並將第一個問題跳過到第二個問題。
由於“真”或“假”按鈕沒有啟動重啟警報(問題數不超過 12),我可以假設函數運行正確並將我的變量設置為 0。
但是,按“重新啟動”時,重新啟動應用程序的功能沒有運行。
什么錯誤導致了這個? 我可以更改什么來使我的功能在按“重新啟動”時運行?
Tl; dr:按重新啟動按鈕運行一個成功設置新變量的函數,但不運行指定的附加函數。 繼續操作的唯一方法是按下應用程序的“真”或“假”按鈕之一,從而打亂應用程序的流程。
非常感謝您的幫助 - 仍然是 Swift 的新手並且渴望學習!
原因是按鈕被命名為重新啟動。 swift 中還有其他函數也是restart()
。 只需更改它,它應該可以工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.