How do I detect if an actionSheet was dismissed?

Viewed 218

I display an actionSheet in SwiftUI like this

.actionSheet(isPresented: $actionSheetControl.showActionSheet) { () -> ActionSheet in
  getActionSheetPurchase(item)
}

This can be dismissed by tapping outside the action sheet.

How can I detect if the action sheet is dismissed in a case like that?

1 Answers

You can use .onChange for action sheet presented state like on demo below

    @Binding var showActionSheet: Bool
    var body: some View {

       // some view here
        .actionSheet(isPresented: $showActionSheet) {
              // some ActionSheet here
        }
        .onChange(of: showActionSheet) { flag in
            if !flag {
                print("dismissed")        // << here !!
            }
        }
    }
Related