Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create an alert with more than 2 buttons in SwiftUI?

Tags:

swiftui

I need to create an alert with 3 buttons, but it looks like SwiftUI only gives us two options right now: one button or two buttons. I know that with UIKit, 3 buttons are achievable, but I can't seem to find a workaround in the latest version of SwiftUI to do this. Below is my code where I'm using only a primary and secondary button.

Button(action: {
    self.showAlert = true
}){
    Text("press me")
}
.alert(isPresented: self.$showAlert){
     Alert(title: Text("select option"), message: Text("pls help"), primaryButton: Alert.Button.default(Text("yes"), action: {
         print("yes clicked")
         }), secondaryButton: Alert.Button.cancel(Text("no"), action: {
         print("no clicked")
     })
     )
}
like image 773
Jnguyen22 Avatar asked Sep 08 '19 06:09

Jnguyen22


People also ask

How do I get multiple alerts in SwiftUI?

SwiftUI can't have multiple alert modifiers on the same view (the same branch in a view hierarchy, to be exact). The only latest outermost . alert will work if you got multiple alert modifiers in the same branch in a view hierarchy. These can be categorized into two scenarios.

How do I create a custom alert in SwiftUI?

Build and present custom alerts to your users Presenting system alerts on SwiftUI is super simple. Just call an instance method alert and pass a few parameters, and you are done. But the system alerts that come up are too basic with the non-customizable default design.

How do I trigger alerts in Swift?

To show an alert, create some Boolean state that determines whether the alert should be visible, then attach that to an alert() modifier along with all the buttons you want to show in the alert. All buttons dismiss the alert when tapped, so you can provide an empty action for simple dismissal.

How do I make a button in SwiftUI?

Getting Started. Download the materials for this tutorial — use the link at the top or bottom of this article. Open the starter project in Xcode 13 — the files are placeholders for all the buttons' types and styles you'll be building. Build and run the app to render a beautiful SwiftUI List .


1 Answers

This is now possible on iOS 15/macOS 12 with a new version of the alert modifier: alert(_:isPresented:presenting:actions:).

It works a bit differently because the Alert struct isn't used anymore; you use regular SwiftUI Buttons instead. Add a ButtonRole to indicate which buttons perform the "cancel" action or "destructive" actions. Add the .keyboardShortcut(.defaultAction) modifier to a button to indicate it performs the principal action.

For example:

MyView()
    .alert("Test", isPresented: $presentingAlert) {
        Button("one", action: {})
        Button("two", action: {}).keyboardShortcut(.defaultAction)
        Button("three", role: .destructive, action: {})
        Button("four", role: .cancel, action: {})
    }

Creates the following alert:

Alert

like image 162
halleygen Avatar answered Sep 29 '22 10:09

halleygen