How to display an alert in SwiftUI.
It is written quite differently from UIAlertController.
The @State
of the Property Wrapper
is still unfamiliar.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} |