毎回3~4行必要とするUIAlertControllerを簡単に実装する関数を作ったのでメモとして残しておきます。
変更前
実装するUIAlertControllerに搭載されているボタンはOKボタンのみです。(汎用性が高いので)
let alert = UIAlertController(title: "UIAlertControllerのタイトル", message:"UIAlertControllerのテキストメッセージ", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
変更後
一度関数を宣言したら、後は毎回self.showAlert(title: "タイトル", text: "メッセージ")
を呼び出すだけ。
// 関数
func showAlert(title : String , text : String){
let alert = UIAlertController(title: title, message: text, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
//実際に呼び出す時
self.showAlert(title: "タイトル", text: "メッセージ")
Swiftのお役立ち情報