Dovizu
Dovizu

Reputation: 417

Buttons inside ToolbarItem cannot dismiss sheet

In Xcode 12 Beta 6, dismissing a sheet doesn't work inside a button's action inside a ToolbarItem.

My sheet view looks like:

NavigationView {
    Form {
        Section {
            TextField("Name", text: $name)
        }
    }
    .navigationTitle("New Thing")
    .toolbar {
        ToolbarItem(placement: .cancellationAction) {
            Button(action: {
                self.presentation.wrappedValue.dismiss()
            }, label: {
                Text("Cancel")
            })
        }
        ToolbarItem(placement: .confirmationAction) {
            Button(action: {
                do {
                    // some saving logic
                    try managedObjectContext.save()
                    self.presentation.wrappedValue.dismiss()
                } catch {
                    print("didn't save due to \(error.localizedDescription)")
                }
            }, label: {
                Text("Save")
            })
        }
    }
}

EDIT: here's how I constructed the sheet

var body: some View {
        List {
            ForEach(results) { result in
                HStack {
                    NavigationLink(destination: SingleResultView(result: result)) {
                        SingleResultRowView(result: result)
                    }
                }
            }
            .onDelete(perform: deleteResult)
        }
        .navigationTitle("All Results")
        .toolbar {
            ToolbarItem(placement: .primaryAction) {
                Button(action: {
                    self.isNewResultSheetPresented.toggle()
                }, label: {
                    Image(systemName: "plus.circle.fill")
                        .resizable()
                        .frame(width: 30, height: 30, alignment: .center)
                })
                .sheet(isPresented: $isNewResultSheetPresented) {
                    NewResultView()
                    // ^ this contains the code above
                        .environment(\.managedObjectContext, self.managedObjectContext)
                }
            }
        }
    }

When the sheet is first presented, immediately a console log appears:

2020-09-13 20:52:02.333679-0700 MyApp[2710:89263] 
[Presentation] Attempt to present <_TtGC7SwiftUI22SheetHostingControllerVS_7AnyView_: 0x1027b7890> on 
<_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier__: 0x10270d620> 
(from <_TtGC7SwiftUIP10$194f39bd428DestinationHostingControllerVS_7AnyView_: 0x103605930>) 
which is already presenting <_TtGC7SwiftUI22SheetHostingControllerVS_7AnyView_: 0x103606d60>.

I can dismiss the sheet only by swiping down.

For reference, I went back to an older commit where I used NavigationBarItems and it worked perfectly. But from what I understand, this is a situation where I'm supposed to be using ToolbarItem.

Does anybody know why the good old self.presentation.wrappedValue.dismiss() doesn't work here or why is the sheet being presented twice?

Upvotes: 2

Views: 1253

Answers (1)

Asperi
Asperi

Reputation: 257711

Move sheet out of toolbar, as

var body: some View {
    List {
        ForEach(results) { result in
            HStack {
                NavigationLink(destination: SingleResultView(result: result)) {
                    SingleResultRowView(result: result)
                }
            }
        }
        .onDelete(perform: deleteResult)
    }
    .navigationTitle("All Results")
    .sheet(isPresented: $isNewResultSheetPresented) {     // << here !!
        NewResultView()
        // ^ this contains the code above
            .environment(\.managedObjectContext, self.managedObjectContext)
    }
    .toolbar {
        ToolbarItem(placement: .primaryAction) {
            Button(action: {
                self.isNewResultSheetPresented.toggle()
            }, label: {
                Image(systemName: "plus.circle.fill")
                    .resizable()
                    .frame(width: 30, height: 30, alignment: .center)
            })
        }
    }
}

Upvotes: 4

Related Questions