【Swift UIkit】UIAlertControllerの使い方とUIAlertActionでのアラートボタン実装方法!

この記事からわかること

  • SwiftUIKitUIAlertController使い方
  • アラート表示実装方法
  • UIAlertActionボタン定義する流れ
  • 削除ボタンやキャンセルボタンの作り方

index

[open]

\ アプリをリリースしました /

みんなの誕生日

友達や家族の誕生日をメモ!通知も届く-みんなの誕生日-

posted withアプリーチ

UIKitフレームワークで以下のようなアラート機能を表示できるUIAlertControllerの使い方をまとめていきます。

【SwiftUI】Alertの使い方!複数表示できない理由と解決法

公式リファレンス:UIAlertController

UIAlertControllerの使い方

UIAlertControllerクラスを使用してアラートを表示させる流れ

  1. UIAlertControllerをインスタンス化
  2. ボタンとなるUIAlertActionクラスをインスタンス化
  3. addActionメソッドでボタンを追加
  4. presentメソッドで表示

実装コード

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction  func showAlert(){
        let alert = UIAlertController(title: "アラート", message: "アラート表示です", preferredStyle: .alert)
        let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
            // OKボタン押下時に実行した処理
        })
        alert.addAction(ok)
        self.present(alert, animated: true, completion: nil)
    }   
}

UIAlertControllerをインスタンス化

let alert = UIAlertController(title: "アラート", message: "アラート表示です", preferredStyle: .alert)

最初にUIAlertControllerインスタンスを生成し、アラート表示させるためのタイトルとメッセージを記述していきます。以下のようなイニシャライザが用意されているのでインスタンス化時にそのまま指定します。

convenience init(
    title: String?,
    message: String?,
    preferredStyle: UIAlertController.Style
)

ボタンとなるUIAlertActionクラスをインスタンス化

let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
    // OKボタン押下時に実行した処理
})

公式リファレンス:UIAlertAction

アラート表示にボタンを追加するためにはUIAlertActionクラスを使用します。このクラスでボタンのタイトルと種類、押下された時に実行したい処理を定義します。

ボタンの種類は列挙型として定義されているUIAlertAction.Styleの中から指定します。

enum Style : Int, @unchecked  Sendable {
    case `default`  // デフォルト
    case cancel  // キャンセル
    case destructive // 削除
}
【SwiftUI】Alertの使い方!キャンセルボタンを作成 【SwiftUI】Alertの使い方!キャンセルボタンを作成
@IBAction  func showAlert(){
  let alert = UIAlertController(title: "ここはタイトルです", message: "ここはメッセージです。", preferredStyle: .alert)
  let delete = UIAlertAction(title: "削除", style: .destructive, handler: { (action) -> Void in
      
  })
  let cancel = UIAlertAction(title: "キャンセル", style: .cancel, handler: { (action) -> Void in

  })
  alert.addAction(delete)
  alert.addAction(cancel)
  self.present(alert, animated: true, completion: nil)
}

ボタンは複数実装することもできるので必要な数のUIAlertActionインスタンスを作成しておきます。

addActionメソッドでボタンを追加

alert.addAction(ok)

作成したボタンはUIAlertController紐づける必要があります。addActionメソッドを使用することで引数に渡したUIAlertActionインスタンスをアラートに追加することができます。

presentメソッドで表示

self.present(alert, animated: true, completion: nil)

最後に構築したアラート表示をUIViewControllerクラスが持つpresentメソッドを使って画面に表示させます。モーダルを表示させる時などにも使用するメソッドです。

まだまだ勉強中ですので間違っている点や至らぬ点がありましたら教えていただけると助かります。

ご覧いただきありがとうございました。

searchbox

スポンサー

ProFile

ame

趣味:読書,プログラミング学習,サイト制作,ブログ

IT嫌いを克服するためにITパスを取得しようと勉強してからサイト制作が趣味に変わりました笑
今はCMSを使わずこのサイトを完全自作でサイト運営中〜

自作iOSアプリ

\ アプリをリリースしました /

mapping

自分だけの地図を作ろう!-mapping-

無料posted withアプリーチ

割り勘アプリ-bill-

旅行におすすめ!
割り勘アプリ-bill-

無料posted withアプリーチ

Imakoko

現在地を取得するアプリ!Imakoko

無料posted withアプリーチ

ふるログ

ふるさと納税管理アプリ-ふるログ-

無料posted withアプリーチ

Remind-シンプル通知アプリ-

シンプル通知アプリ-Remind-

無料posted withアプリーチ

CLIPURL

好きな記事をクリップしよう!-CLIPURL-

無料posted withアプリーチ

記録カレンダー

続けたを可視化できるアプリ!記録カレンダー

無料posted withアプリーチ

CART-共有できるお買い物リスト-

CART-共有できるお買い物リスト-

無料posted withアプリーチ

QuickPressPanel

早押しゲーム-QuickPressPanel-

無料posted withアプリーチ

貸し借り管理アプリ

友達とのお金の管理-貸し借り管理アプリ-

無料posted withアプリーチ

みんなの誕生日

友達や家族の誕生日をメモ!通知も届く-みんなの誕生日-

posted withアプリーチ

Githubにて
iOSアプリのソースコードを公開中!

自作Webアプリ

子育て知識共有サイト-mikata-

子育て知識共有サイト-mikata-

New Article

index