我目前正在阅读Apple提供的Swift Developer书。我在“个性测验”项目中,对为什么按原样编写代码感到困惑。
问题出在updateSingleStack(using: currentAnswers)
上。如果您看下面的代码,我会有些困惑using: currentAnswers
的含义。
import UIKit
class QuestionViewController: UIViewController
{
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var singleStackView: UIStackView!
@IBOutlet weak var singleButton1: UIButton!
@IBOutlet weak var singleButton2: UIButton!
@IBOutlet weak var singleButton3: UIButton!
@IBOutlet weak var singleButton4: UIButton!
@IBOutlet weak var multipleStackView: UIStackView!
@IBOutlet weak var multiLabel1: UILabel!
@IBOutlet weak var multiLabel2: UILabel!
@IBOutlet weak var multiLabel3: UILabel!
@IBOutlet weak var multiLabel4: UILabel!
@IBOutlet weak var rangedStackView: UIStackView!
@IBOutlet weak var rangedLabel1: UILabel!
@IBOutlet weak var rangedLabel2: UILabel!
@IBOutlet weak var questionProgressView: UIProgressView!
var questions: [Question] =
[
Question(text: "Which food do you like the most?",
type: .single,
answers:
[
Answer(text: "Steak", type: .dog),
Answer(text: "Fish", type: .cat),
Answer(text: "Carrots", type: .rabbit),
Answer(text: "Corn", type: .turtle)
]),
Question(text: "Which activities do you enjoy?",
type: .multiple,
answers:
[
Answer(text: "Swimming", type: .turtle),
Answer(text: "Sleeping", type: .cat),
Answer(text: "Cuddling", type: .rabbit),
Answer(text: "Eating", type: .dog)
]),
Question(text: "How much do you enjoy car rides?",
type: .ranged,
answers:
[
Answer(text: "I dislike them", type: .cat),
Answer(text: "I get a little nervous", type: .rabbit),
Answer(text: "I barely notice them", type: .turtle),
Answer(text: "I love them", type: .dog)
])
]
var questionIndex = 0
override func viewDidLoad()
{
super.viewDidLoad()
updateUI()
}
func updateUI()
{
singleStackView.isHidden = true
multipleStackView.isHidden = true
rangedStackView.isHidden = true
let currentQuestion = questions[questionIndex]
let currentAnswers = currentQuestion.answers
let totalProgress = Float(questionIndex) / Float(questions.count)
navigationItem.title = "Question #\(questionIndex+1)"
questionLabel.text = currentQuestion.text
questionProgressView.setProgress(totalProgress, animated: true)
switch currentQuestion.type
{
case .single:
updateSingleStack(using: currentAnswers)
case .multiple:
updateMultipleStack(using: currentAnswers)
case .ranged:
updateRangedStack(using: currentAnswers)
}
}
func updateSingleStack(using answers: [Answer])
{
singleStackView.isHidden = false
singleButton1.setTitle(answers[0].text, for: .normal)
singleButton2.setTitle(answers[1].text, for: .normal)
singleButton3.setTitle(answers[2].text, for: .normal)
singleButton4.setTitle(answers[3].text, for: .normal)
}
func updateMultipleStack(using answers: [Answer])
{
multipleStackView.isHidden = false
multiLabel1.text = answers[0].text
multiLabel2.text = answers[1].text
multiLabel3.text = answers[2].text
multiLabel4.text = answers[3].text
}
func updateRangedStack(using answers: [Answer])
{
}
}