Skip to content

Instantly share code, notes, and snippets.

@archieedwards
Last active May 25, 2020 17:42
Show Gist options
  • Save archieedwards/47fe63d6f56e0d67f21f470ce910548b to your computer and use it in GitHub Desktop.
Save archieedwards/47fe63d6f56e0d67f21f470ce910548b to your computer and use it in GitHub Desktop.
struct ContentView: View {
@State var alertItem : AlertItem?
var body: some View {
/// button
Button(action: {
self.alertItem = AlertItem(title: Text("I'm an alert"), message: Text("Are you sure about this?"), primaryButton: .default(Text("Yes"), action: {
/// trigger second alert
DispatchQueue.main.async {
self.alertItem = AlertItem(title: Text("Error"), message: Text("An unexpected error occurred"), dismissButton: .default(Text("OK")))
}
}), secondaryButton: .cancel())
}, label: {
Text("SHOW ALERT")
}).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