Skip to content

Instantly share code, notes, and snippets.

@archieedwards
Created May 25, 2020 17:28
Show Gist options
  • Save archieedwards/cdde648c441ac047fffcabcd3ce49fb0 to your computer and use it in GitHub Desktop.
Save archieedwards/cdde648c441ac047fffcabcd3ce49fb0 to your computer and use it in GitHub Desktop.
struct ContentView: View {
@State var alertItem : AlertItem?
var body: some View {
VStack{
/// button 1
Button(action: {
self.alertItem = AlertItem(title: Text("I'm an alert"), message: Text("Are you sure about this?"), primaryButton: .default(Text("Yes"), action: {
/// insert alert 1 action here
}), secondaryButton: .cancel())
}, label: {
Text("SHOW ALERT 1")
})
/// button 2
Button(action: {
self.alertItem = AlertItem(title: Text("I'm another alert"), dismissButton: .default(Text("OK")))
}, label: {
Text("SHOW ALERT 2")
})
}.alert(item: $alertItem) { alertItem in
guard let primaryButton = alertItem.primaryButton, let secondaryButton = alertItem.secondaryButton else{
return Alert(title: alertItem.title, message: alertItem.message, dismissButton: alertItem.dismissButton)
}
return Alert(title: alertItem.title, message: alertItem.message, primaryButton: primaryButton, secondaryButton: secondaryButton)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment