0

In iOS 16, I have a 3-column Sidebar-Content-Detail view. When the Sidebar selection changes, I would like to refresh (clear) the detail view.

    var body: some View {
        NavigationSplitView {
            List(Content.allCases, selection: $selectedContent) { content in
                NavigationLink(content.localizedName, value: content)
            }
            .navigationTitle("Sidebar")
        } content: {
            List(
                dataModel.details(in: selectedContent),
                selection: $selectedDetail)
            { detail in
                NavigationLink(detail.name, value: detail)
            }
            .navigationTitle(selectedContent?.localizedName ?? "")
        } detail: {
            MyDetail(recipe: selectedDetail)
        }
    }

I would guess that, upon a change in the Sidebar, I would need to set selectedDetail to nil. But how do I capture a sidebar change?

coco
  • 2,998
  • 1
  • 35
  • 58

1 Answers1

2

You should be able to add an .onChange modifier:

.onChange(of: selectedContent) { _ in 
  selectedDetail = nil
}
ScottM
  • 7,108
  • 1
  • 25
  • 42