LoginSignup
0
1

More than 1 year has passed since last update.

SwiftUI標準的なアラートの表示方法

Last updated at Posted at 2022-08-13

普段はAndroidエンジニアをしています。
SwiftUIでiOSアプリの開発の勉強中です。

勉強記録として振り返れるように記事にしております。
SwiftUIでアラートの表示する方法です。

使用バージョン
Xcode13

SwiftUIでアラートの表示する方法

ContentView.swift
struct ContentView: View {
    @State var isShowAlert: Bool = false
    
    var body: some View {
        Button(action: {
            isShowAlert = true
        }) {
            Text("アラート")
        }.alert(isPresented: $isShowAlert) {
            Alert(title: Text("タイトル"), message: Text("メッセージ"),dismissButton: .default(Text("OK"), action:{}))
        }
    }
}

アラートって表示されているボタンをタップすると標準的なアラートが表示されるようになりました。
OKをタップしたら何か実行したい場合はaction:{}{}の中に実行したいコードを実装をしてください。

スクリーンショット 2022-08-13 18.15.23.png

今後はOKとキャンセルがあるパターンなど調べて見ようと思います。

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