從中間彈出的 Alert
style: .default => 按鈕字體顏色為預設的藍色
handler: nil => 按下後不觸發任何事
let alertController = UIAlertController(title: "alert title", message: "alert message", preferredStyle: .alert)
let alertAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(alertAction)
present(alertController, animated: true, completion: nil)
style: .destructive => 按鈕字體顏色為紅色
handler: { action in } => 按下後觸發 action in 之後的事
let alertController = UIAlertController(title: "alert title", message: "alert message", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .destructive, handler: { action in
//按下OK後會做的事情
print("您按下OK")
}))
present(alertController, animated: true, completion: nil)
由下而上彈出的 alert
按了"確認"的按鈕後,執行print ( phone as Any, password as Any )
let controller = UIAlertController(title: "選單", message: "請問你的年紀?", preferredStyle: .actionSheet)
let ages = ["0 - 19", "20 - 39", "40 - 59", "60 - 79", "80 - 99", "100 - 100以上"]
for age in ages {
let action = UIAlertAction(title: age, style: .default) { action in
print(action.title as Any) // print 出按下的按鈕名稱
}
// 有幾個 ages 就有幾個按鈕。當選項太多時, UIAlertController 會自動變身成可以上下捲動的選單
controller.addAction(action)
}
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
controller.addAction(cancelAction)
present(controller, animated: true, completion: nil)
明天會介紹:
敬請期待!