看歌名猜歌手
Published in
8 min readApr 16, 2022
這次的作業主要是練習把值傳到下一頁
畫面為
使用到的action
1.UIbutton
使用到的outlet
- 題目
- 第N題
產出題目
Model
struct Song {
var songname: String?
var options: [String]
var answer: String?
init(songname: String , options: [String] , answer: String){
self.songname = songname
self.options = options
self.answer = answer
}
}
controller
func createSongs() {
Songs = [
Song(songname: "我難過", options: ["5566","183 Club","ENERGY","K ONE"], answer: "5566"),
Song(songname: "愛上你算我賤", options: ["周湯豪","周華健","周杰倫","周興哲"], answer: "周湯豪"),
Song(songname: "閣愛妳一擺", options: ["茄子蛋","告五人","火腿蛋","五月天"], answer: "茄子蛋"),
Song(songname: "重感情的廢物", options: ["TRASH","飛輪海","五堅情","五月天"], answer: "TRASH"),
Song(songname: "牽心萬苦", options: ["謝和弦","謝金燕","謝霆鋒","謝長廷"], answer: "謝和弦"),
Song(songname: "我可以", options: ["蔡旻佑","楊宗緯","蕭敬騰","林宥嘉"], answer: "蔡旻佑"),
Song(songname: "天后", options: ["陳勢安","畢書盡","陳港生","陳時中"], answer: "陳勢安"),
Song(songname: "帥到分手", options: ["周湯豪","周華健","周杰倫","周興哲"], answer: "周湯豪"),
Song(songname: "你我可以", options: ["熊仔","MC Hot Dog","蛋堡","瘦子"], answer: "熊仔"),
Song(songname: "IDOL RAPPER", options: ["婁峻碩","熊仔","高爾宣","陳時中"], answer: "婁峻碩"),
Song(songname: "愛你", options: ["陳芳語","嚴爵","曾沛慈","蔡英文"], answer: "陳芳語"),
Song(songname: "啞巴", options: ["潘瑋柏","潘志遠醫師","黃品源","王傑"], answer: "潘瑋柏"),
Song(songname: "小情歌", options: ["蘇打綠","無糖綠","五月天","蘇打水"], answer: "蘇打綠"),
Song(songname: "你不知道的事", options: ["王力宏","羅志祥","九把刀","吳亦凡"], answer: "王力宏"),
Song(songname: "紳士", options: ["薛之謙","李榮浩","張衛健","張一健"], answer: "薛之謙"),
Song(songname: "戒菸", options: ["李榮浩","薛之謙","張衛健","張一健"], answer: "李榮浩"),
Song(songname: "我在呢", options: ["Tank","Zack","Cook","Jack"], answer: "Tank"),
Song(songname: "兜圈", options: ["林宥嘉","蔡旻佑","楊宗緯","蕭敬騰"], answer: "林宥嘉"),
Song(songname: "倒數", options: ["鄧紫棋","鄧麗君","鄧小平","鄧秋千"], answer: "鄧紫棋"),
Song(songname: "默念你", options: ["宋念宇","周杰倫","王力宏","羅志祥"], answer: "宋念宇"),
Song(songname: "告白氣球", options: ["周杰倫","林俊傑","周興哲","林宥嘉"], answer: "周杰倫"),
Song(songname: "馬子狗", options: ["八三夭","五月天","茄子蛋","告五人"], answer: "八三夭"),
Song(songname: "Still Life", options: ["BIGBANG","BANGBANG","BTS","SHINEE"], answer: "BIGBANG"),
Song(songname: "想知道你在想什麼", options: ["周興哲","周立民","言承旭","蛋堡"], answer: "周興哲"),
Song(songname: "好不容易", options: ["告五人","BIGBANG","F4","瘦子"], answer: "告五人"),
]
}
這樣題目就產出來囉
再來是答案的按鈕
判斷點選的與正確答案是否相同
然後第10題時會使用到alert function
@IBAction func choose(_ sender: Any) {
let Button = sender as! UIButton
//選擇的答案
let chooseAnswer : String = Songs[Number].options[Button.tag]
//正確答案
let correctAnser : String = Songs[Number].answer!
//比較
if chooseAnswer == correctAnser {
print("答對了")
correctNumber = correctNumber + 1
} else {
print("答錯了")
errorNumber = errorNumber + 1
}
//判斷10題後
if Number < 9 {
Number = Number + 1
setUI(Number: Number)
} else {
alert(title: "通知!!", message: "已經10題囉~", button: "OK")
}
}
alert function
如果點選ok時會到下一頁
func alert(title: String , message: String, button: String){
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
//按鈕
let okAction = UIAlertAction(title: button, style: .default) { (okAction) in
self.performSegue(withIdentifier: "resultViewController", sender: nil)
}
//將按鈕加入本體
alert.addAction(okAction)
//persent出來
present(alert,animated: true,completion: nil)
}
還有畫面的segue 要設定
另一個重要的是
到下一頁時會把值丟到下一頁的2個欄位
@IBSegueAction func goResult(_ coder: NSCoder) -> resultViewController? {
let controller = resultViewController(coder: coder)
controller?.correctNumber = correctNumber
controller?.errorNumber = errorNumber
return controller
}
result頁面
從前頁傳來的值前,要先定義,否則會閃退
var correctNumber : Int = 0 //答對題數
var errorNumber : Int = 0 //答錯題數
附上demo gif