0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Swift】UIAlertControllerを簡単に実装する関数

Last updated at Posted at 2021-10-22

毎回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のお役立ち情報

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?