Published in
12 min readSep 22, 2022
[作業]選擇題 App — 棒球英文測驗
不查資料地從無到有製作選擇題 App,包含以下功能。
- 製作選擇題 App,每題有四個選項。
- 答對一題加 10 分 。
- 畫面上顯示目前題目是第幾題。
- 自訂選擇題的資料型別。
- 題庫有 n 題,隨機出其中的 10 題,每次玩的時候題目順序都不一樣。( n > 10 )
- 包含多個頁面,至少有問題頁面和分數頁面。
- 利用 IBSegueAction & performSegue 將結果從問題頁傳到分數頁。
- 使用 UIAlertController 顯示 alert。
技術考核項目
- storyboard。
- Swift 基本語法。
- 自訂 view controller 類別 & model 型別。
- viewDidLoad。
- IBOutlet & IBAction。
- IBSegueAction & performSegue。
- UIAlertController。
- 可考慮使用的特別語法: enum,computed property,property observer
Code of qna.swift
import Foundationstruct QnA {
var question: String
var answer: [String]
}
Code of Question1ViewController.swift
import UIKitclass Question1ViewController: UIViewController {
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var startButton: UIButton!
@IBOutlet weak var toNextButton: UIButton!
@IBOutlet var ansButton: [UIButton]!
@IBOutlet weak var scoreLabel: UILabel!
let qnas = [QnA(question: "「投手」的英文?", answer: ["pitcher", "pig", "Peter", "picture"]), QnA(question: "「捕手」的英文?", answer: ["catcher", "cat", "Kate", "castle"]), QnA(question: "「游擊手」的英文?", answer: ["short stop", "show stop", "short start", "showhand"]), QnA(question: "「右外野手」的英文?", answer: ["right fielder", "left fielder", "center fielder", "infielder"]), QnA(question: "「二好三壞」的英文?", answer: ["full count", "two three", "fool count", "fair count"]), QnA(question: "「界外球」的英文?", answer: ["foul ball", "fly ball", "fair ball", "four ball"]), QnA(question: "「界內球」的英文?", answer: ["fair ball", "fly ball", "foul ball", "four ball"]), QnA(question: "「高飛球」的英文?", answer: ["fly ball", "fair ball", "foul ball", "ground ball"]), QnA(question: "「滾地球」的英文?", answer: ["ground ball", "game ball", "foul ball", "guard ball"]), QnA(question: "「平飛球」的英文?", answer: ["line drive", "line fly", "line up", "fly ball"])]
var randomQnas: [QnA] = []
var index = 0
var point = 0
override func viewDidLoad() {
super.viewDidLoad()
initUI()
toNextButton.isHidden = true
}@IBAction func startButtonTap(_ sender: UIButton) {
point = 0
startButton.isEnabled.toggle()
randomQnas = qnas.shuffled()
showQuestion()
for ansBtn in ansButton {
ansBtn.isEnabled = true
}
}
@IBAction func AnsChosen(_ sender: UIButton) {
if sender.currentTitle! == randomQnas[index].answer[0] {
point += 10
scoreLabel.text = "⭕️"
} else {
scoreLabel.text = "❌"
}
if index < 9 {
index += 1
showQuestion()
} else {
for ansBtn in ansButton {
ansBtn.isEnabled = false
}
var resultTitle = ""
if point == 100 {
resultTitle = "🎉🎉🎉Pass🎊🎊🎊"
toNextButton.isHidden = false
} else {
resultTitle = "Try Again"
}
let controller = UIAlertController(title: "The End", message: "Your Score is \(point)", preferredStyle: .alert)
let okAction = UIAlertAction(title: resultTitle, style: .default) { _ in
self.initUI()
}
controller.addAction(okAction)
present(controller, animated: true)
}
}
func initUI() {
questionLabel.text = "Question"
for ansBtn in ansButton {
ansBtn.setTitle("", for: .normal)
ansBtn.isEnabled = false
}
scoreLabel.text = "⚾️"
index = 0
startButton.isEnabled = true
}
func showQuestion() {
questionLabel.text = randomQnas[index].question
let randomAns = randomQnas[index].answer.shuffled()
for (i, answer) in randomAns.enumerated() {
ansButton[i].setTitle(answer, for: .normal)
}
}
@IBAction func toNext(_ sender: UIButton) {
if let nextVC = storyboard?.instantiateViewController(withIdentifier: "\(Question2ViewController.self)") as? Question2ViewController {
nextVC.scorePre = point
nextVC.modalPresentationStyle = .fullScreen
nextVC.modalTransitionStyle = .flipHorizontal
self.present(nextVC, animated: true)
}
}
}
Code of Question2ViewController.swift
import UIKitclass Question2ViewController: UIViewController {
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var startButton: UIButton!
@IBOutlet var ansButton: [UIButton]!
@IBOutlet weak var scoreLabel: UILabel!
@IBOutlet weak var scorePreLabel: UILabel!
let qnas = [QnA(question: "「雙殺」的英文?", answer: [" double play", "two kill", "double kill", "doubt play"]), QnA(question: "「安打」的英文?", answer: ["hit", "pit", "hat", "hot"]), QnA(question: "「二壘安打」的英文?", answer: ["double", "two base", "second base", "doubt"]), QnA(question: "「三壘安打」的英文?", answer: ["triple", "three base", "third base", "triangle"]), QnA(question: "「高飛犧牲打」的英文?", answer: ["sacrifice fly", "fly ball", "infield fly", "butterfly"]), QnA(question: "「短打」的英文?", answer: ["bunt", "short", "popup", "fly"]), QnA(question: "「滑壘」的英文?", answer: ["slide", "fly ball", "foul ball", "four ball"]), QnA(question: "「裁判」的英文?", answer: ["umpire", "sir", "teacher", "umbrella"]), QnA(question: "「記分板」的英文?", answer: ["scoreboard", "whiteboard", "blackboard", "cardboard"]), QnA(question: "「滿壘全壘打」的英文?", answer: ["grand slam home run", "four-points home run", "big home run", "super home run"])]
var scorePre = 0
var randomQnas: [QnA] = []
var index = 0
var point = 0
override func viewDidLoad() {
super.viewDidLoad()
scorePreLabel.text = "\(scorePre)"
initUI()
}@IBAction func startButtonTap(_ sender: UIButton) {
point = 0
startButton.isEnabled.toggle()
randomQnas = qnas.shuffled()
showQuestion()
for ansBtn in ansButton {
ansBtn.isEnabled = true
}
}
@IBAction func AnsChosen(_ sender: UIButton) {
if sender.currentTitle! == randomQnas[index].answer[0] {
point += 10
scoreLabel.text = "⭕️"
} else {
scoreLabel.text = "❌"
}
if index < 9 {
index += 1
showQuestion()
} else {
for ansBtn in ansButton {
ansBtn.isEnabled = false
}
var resultTitle = ""
if point == 100 {
resultTitle = "🎉🎉🎉Pass🎊🎊🎊"
// toNextButton.isHidden = false
} else {
resultTitle = "Try Again"
}
let controller = UIAlertController(title: "The End", message: "Your Score is \(point)", preferredStyle: .alert)
let okAction = UIAlertAction(title: resultTitle, style: .default) { _ in
self.initUI()
}
controller.addAction(okAction)
present(controller, animated: true)
}
}
func initUI() {
questionLabel.text = "Question"
for ansBtn in ansButton {
ansBtn.setTitle("", for: .normal)
ansBtn.isEnabled = false
}
scoreLabel.text = "⚾️"
index = 0
startButton.isEnabled = true
}
func showQuestion() {
questionLabel.text = randomQnas[index].question
let randomAns = randomQnas[index].answer.shuffled()
for (i, answer) in randomAns.enumerated() {
ansButton[i].setTitle(answer, for: .normal)
}
}
@IBAction func backButtonTap(_ sender: Any) {
dismiss(animated: true)
}
}