Nerdy Bunz
Nerdy Bunz

Reputation: 7457

SwiftUI: How to execute closure when Alert is dismissed?

I've been trying out swiftUI and looked at this Ray Wenderlich tutorial... I noticed they didn't re-implement the "nextRound" functionality... so I tried to do it myself. Ran into a problem (which maybe they did, also):

The basic question is more general:

Using swiftUI, how do you trigger a function when an Alert is dismissed -- when the user clicks "OK." ?

I've tried using the dismissButton argument of the Alert constructor...

(and also the .onDisappear method of View but I can't figure out how to apply it to the Alert view.)

Code:

import SwiftUI

struct ContentView: View {

    @State var shouldShowAlert: Bool = false

    // this never gets called
    func onAlertDismissed() {
        print("you will not see this in the console")
    }

    // this doesn't seem to work
    var dismissButton: some View {

        Button(action: {
            self.onAlertDismissed()
        }) {
            // Bilbo Baggins does not appear -- "OK" still shows
            Text("BILBO BAGGINS")
        }
    }

    var body: some View {

        VStack {
            Spacer()

            Button(action: {
                self.shouldShowAlert = true
            }) {
                Text("show the alert!")
            }
            Spacer()
        }.alert(isPresented: $shouldShowAlert, content: {

            // what to add here?
            Alert(title: Text("Alert:"), message: Text("press OK to execute onAlertDismissed()..."))

            // what I have tried and doesn't work:
            /*
             Alert(title: Text("Alert:"), message: Text("press OK to execute onAlertDismissed()..."), dismissButton: self.dismissButton as? Alert.Button)
             */


        })

    }
}

Upvotes: 18

Views: 14063

Answers (3)

Oliver
Oliver

Reputation: 181

It's possible to create alerts like this:

import SwiftUI

struct ContentView: View {

@State var showingAlert = false

var body: some View {
    VStack {
        HStack { 
                Button(action: {
                    self.showingAlert = true
            })
            {
                Text("Save")
                    .font(.headline)
            }
            .alert(isPresented: $showingAlert, content: { 
                return Alert(
                    title: Text("Save Product"), 
                    message: Text("Are you sure you want to save the changes made?"), 
                    primaryButton: .default(Text("Yes"), action: {
                        //insert an action here
                    }), 
                    secondaryButton: .destructive(Text("No")))
                    })
            }
        }
    }
}

Upvotes: 3

brianoqr
brianoqr

Reputation: 91

By looking at your code, it appears you don’t include a button in the alert propert, so your alert is not executing any action, in swiftui the alert signature is

init(title: Text, message: Text? = nil, primaryButton: Alert.Button, secondaryButton: Alert.Button)

Implement the signature properly is the first step

Upvotes: 1

Fabian
Fabian

Reputation: 5348

The button is constructed a little differently. You basically have to use a static factory method from Alert.Button to construct them and pass those in.

Alert(title: Text("Alert:"),
    message: Text("press OK to execute default action..."),
    dismissButton: Alert.Button.default(
        Text("Press ok here"), action: { print("Hello world!") }
    )
)

Alert(title: Text("Alert!"), message: Text("Message"),
    primaryButton: Alert.Button.default(Text("Yes"), action: {
        print("Yes")
    }),
    secondaryButton: Alert.Button.cancel(Text("No"), action: {
        print("No")
    })
)

Upvotes: 28

Related Questions