選擇題 App
Published in
8 min readJun 1, 2020
- 關於恐龍的選擇題,每題有三個選項,題目和選項亂數。
- 答對一題加 10 分 ,答完 10 題後會算出總分。
- 顯示目前到了第幾題。
- 玩完後可選擇再玩一次,重新開始玩。
專案開啟後,先另開一個File,選擇Swift File,在這檔案中生一個 struct,因選擇題內容的型別設定只會告知型別,不會存放東西(例如以下question),故無法使用上課中提到的 Class。(學長姐的解釋)
//設定一個Swift File取名questions.swiftstruct Qnas {var question: Stringvar option: [String] //因有很多選項所以使用陣列[]var answer: String}
第一次看到struc覺得很陌生,之前只看過class,但看到好多篇文章都會用struct,了解後才發現差異!
struct vs. class
下面這段說法也很淺顯易懂😄
總的來說,用 class 來定義資料物件的話,就好像是在用雲端共享文件一樣:每個人的螢幕上都會有一份文件可以編輯,但這個文件並沒有存在電腦裡,而是跟雲端的版本連線,所以所有的變動都是直接在雲端版本上更新的。好處是方便,壞處是誰修改了甚麼東西經理不會知道(class 本身沒有帳號功能!)。用 struct 的話,則是像傳統的離線文件檔案一樣。一開始文件只有經理有,而如果他想要讓手下小美去修改文件的話,他就需要拷貝一份檔案給小美。小美修改完檔案後,必須把它交還給經理,然後經理再決定要不要用修改過的檔案取代原本的文件。
進入ViewController
var index = 0 //設定題目起始值為零var score = 0 //設定分數起始值為零var rightAnswer = “” //設定正確答案為字串var questionsInfoemation = [Qnas(question: “最聰明的恐龍?”, option: [“傷齒龍”,”暴龍”,”翼手龍”], answer: “傷齒龍”),Qnas(question: “最笨的恐龍?”, option: [“傷齒龍”,”暴龍”,”劍龍”], answer: “劍龍”),Qnas(question: “最兇猛的恐龍是?”, option: [“傷齒龍”,”暴龍”,”翼手龍”], answer: “暴龍”),Qnas(question: “最高的恐龍是?”, option: [“傷齒龍”,”劍龍”,”波塞東龍”], answer: “波塞東龍”),Qnas(question: “最大的恐龍是?”, option: [“阿根廷龍”,”暴龍”,”翼手龍”], answer: “阿根廷龍”),Qnas(question: “第一個發現會挖洞的恐龍?”, option: [“傷齒龍”,”奔掘龍”,”翼手龍”], answer: “奔掘龍”),Qnas(question: “第一個發現有毒牙的恐龍?”, option: [“中國鳥龍”,”暴龍”,”翼手龍”], answer: “中國鳥龍”),Qnas(question: “會照顧寶寶的龍?”, option: [“傷齒龍”,”暴龍”,”慈母龍”], answer: “慈母龍”)]
拉線及設定選項
@IBOutlet weak var QLabel: UILabel! //問題@IBOutlet weak var countLabel: UILabel! //第幾題@IBOutlet weak var scoreLabel: UILabel! //幾分@IBOutlet var optionButton: [UIButton]! //陣列的選項override func viewDidLoad() {super.viewDidLoad()scoreLabel.text = “\(0)” //分數一開始顯示為零分countLabel.text = String(index+1) //題目一開始就顯示為第一題所以+1questionsInfoemation.shuffle() //讓題目隨機亂數playGame()}
問題Label、答案label、選項的顯示動作設定
其中設定按鈕的文字使用迴圈(for in ),再使用 .setTitle ( function for button , Use this method to set the title for the button) ,意即將設定的內容放入button中。
func playGame(){QLabel.text = questionsInfoemation[index].question
//讓QLabel.text帶出陣列的問題rightAnswer = questionsInfoemation[index].answer
//帶入正確題目的答案questionsInfoemation[index].option.shuffle()
//按鈕選項隨機亂數for i in 0…2{ //三個選項optionButton[i].setTitle(questionsInfoemation[index].option[i], for: UIControl.State.normal)}}
什麼是setTitle? button有很多型態可以設定!
設定選項按鈕
@IBAction func optionPressBtn(_ sender: UIButton) {index = index+1if sender.currentTitle == rightAnswer{ //按鈕上的文字等於正確答案score = score+10 //分數加十分scoreLabel.text = “\(score)” //scoreLabel.text顯示分數}if index == questionsInfoemation.count{ //生出一個alertlet alertController = UIAlertController(title: “遊戲結束”,message: “獲得: \(score)分”,preferredStyle: .alert)//參數 preferredStyle 傳入 .alert 將顯示中間彈出視窗let okButton = UIAlertAction(
//利用 UIAlertAction 生成視窗上顯示的按鈕title: “確定”,style: .default)//利用 addAction 加入按鈕。若呼叫多次 addAction,即可加入多個按鈕。alertController.addAction(okButton)self.present(alertController, animated: true, completion: nil)}else{countLabel.text = “\(index+1)”playGame()}}
顯示警告訊息和選單的 UIAlertController
//有分中間跳出及下拉式
設定重新玩的button
@IBAction func restartBn(_ sender: UIButton) {index = 0score = 0countLabel.text = “\(index+1)”scoreLabel.text = “\(0)”questionsInfoemation.shuffle()playGame()}}