0

I do some tests. If I just make a button like this:

Button(action: {
    self.showActionSheet = true
}) {
    Text("Click")
}.actionSheet(isPresented: $showActionSheet) {
    ActionSheet(title: Text("This is a test"))
}

It works!

But if I put it in NavigationView, the bug appears! The ActionSheet will pop up again when I clicked the Cancel.

enter image description here

2 Answers2

1

In SwiftUI the view is a function of the state, so if your flag that is linked to a modal, an action sheet or an alert appearing is not set back to false the overlay will keep on being presented. When the user dismisses the action sheet, your ContentView gets redrawn and because showActionSheet is still true it gets shown again.

ActionSheet

.actionSheet(isPresented: $showActionSheet) {
    ActionSheet(title: Text("This is a test"),
                buttons: [ActionSheet.Button.cancel({
                           self.showActionSheet = false })])
    }

Modal

.sheet(item: $showModal,
       onDismiss: {
           self.showModal = false
}) { Text("Modal") }
LuLuGaGa
  • 13,089
  • 6
  • 49
  • 57
  • 1
    Thanks for answer. I'v tried your solution but it doesn't work. Actually, `ActionSheet` is just for a test. I want to use `Modal` and in `NavigationView` when I `dismiss` the `Modal` it will pop up again. So I don't know how to deal with it. – ninjiacoder Oct 07 '19 at 14:12
0

I post my question on Swift Forum and receive an answer.

https://forums.swift.org/t/actionsheet-and-modal-seems-have-bug-in-navigationview/29590

If I put the ActionSheet or Modal outside NavigationView, it works perfectly!