Skip to content

Displaying an alert in SwiftUI

   

How to display an alert in SwiftUI. It is written quite differently from UIAlertController. The @State of the Property Wrapper is still unfamiliar.

ShowAlert

import SwiftUI
struct ContentView: View {
@State var showingAlert = false
var body: some View {
Button(action: {
self.showingAlert = true
}, label: {
Text("Push Me!")
}).alert(isPresented: self.$showingAlert) {
Alert(
title: Text("タイトル"),
message: Text("メッセージ"),
primaryButton: .default(Text("ボタンその1")) {
print("ボタンその1")
}, secondaryButton: .destructive(Text("ボタンその2")) {
print("ボタンその2")
})
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

  1. How to get an alert when you tap a cell in a list in SwiftUI
  2. Editing a List in SwiftUI
  3. Using TabView to switch screens (SwiftUI)
  4. Transition from one ListView to another (SwiftUI)