1
votes

I have a list in a Navigation View, with a trailing navigation button to add a list item. The button opens a modal sheet. When I dismiss the sheet (by pulling it down), the sheet pops right back up again automatically and I can't get back to the first screen. Here's my code.

struct ListView: View {

    @ObservedObject var listVM: ListViewModel

    @State var showNewItemView: Bool = false

    init() {
        self.listVM = ListViewModel()
    }

    var body: some View {
        NavigationView {
            List {
                ForEach(listVM.items, id: \.dateCreated) { item in
                    HStack {
                        Text(item.name)
                        Spacer()
                        Image(systemName: "arrow.right")
                    }
                }
            }
                .navigationBarTitle("List Name")
                .navigationBarItems(trailing: AddNewItemBtn(isOn: $showNewItemView))
        }
    }
}
struct AddNewItemBtn: View {

    @Binding var isOn: Bool

    var body: some View {
        Button(
            action: { self.isOn.toggle() },
            label: { Image(systemName: "plus.app") })
            .sheet(
                isPresented: self.$isOn,
                content: { NewItemView() })
    }
}

I am getting this error:

Warning: Attempt to present <_TtGC7SwiftUIP13$7fff2c603b7c22SheetHostingControllerVS_7AnyView_: 0x7fc5e0c1f8f0> on which is already presenting (null)

I've tried toggling the bool within "onDismiss" on the button itself, but that doesn't work either. Any ideas?

1

1 Answers

0
votes

Turns out putting the button in the navigationBarItems(trailing:) modifier is the problem. I just put the button in the list itself instead of in the nav bar and it works perfectly fine. Must be some kind of bug.