UIKit-選擇題APP部分功能展示 -shuffle功能、UIAlertController(自訂字型、Struct

#UIAlertController改字型、struct

SHOW TIME:

這個APP之後會上架~敬請期待٩(๑❛ᴗ❛๑)۶

首先我必須得說選擇題是一個練習邏輯的好機會!強烈介意同學們看完寫的方法後理解並寫出自己的code,不要複製貼上。

所以下面提供的程式碼不會是全部的,而是部分功能展示。

  1. 先建Array,這邊我分了兩個file:
建立自己的struct

然後就是在原本的Controller裡建立array把題目、答案、甚至圖片放進去:

var questionArray = [
QuestionFile(type: "生活應用", question: "加油站的會員獎勵計畫中,每消費100元可獲得10點積分。如果小張加油消費了450元,他將獲得多少點積分?", productImage: "gas_station_rewards", answer: "45點", option: ["40點", "45點", "50點"]),}
(以下省略)

2. 選擇題shuffle超簡單,就是上面array建立好,你就給他來一行就搞定:

    override func viewDidLoad() {     super.viewDidLoad()
questionArray.shuffle()
}

3. UIAlertController(自訂字型)

        let attributedTitle = NSAttributedString(string: "輸入你要顯示的字", attributes: [
// 自訂字型及大小
NSAttributedString.Key.font: UIFont(name: "輸入客製化字型名稱", size: 輸入你要的字體大小) ?? UIFont.systemFont(ofSize: 20),
NSAttributedString.Key.foregroundColor: UIColor.black
])

let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
alertController.setValue(attributedTitle, forKey: "attributedTitle")

//創建按鈕跟字型&按下去之後重新開始遊戲
let okAction = UIAlertAction(title: "再玩一次", style: .default){ [
weak self] (_) in
self?.returnGame()//這是我寫的func(提示,幾乎複製貼上viewDidLoad)
}
present(alertController, animated: true)
alertController.addAction(okAction)

(如果還要建另外一個按鈕就是以此類推,以下省略

((課外讀物有興趣請深入研究 - Alert改底色及放圖片

4. 為了讓app自動換題時中間可以塞圖,我還使用其他功能delayed execution,有興趣的同學也可以當成課外讀物。

--

--