5

I'm trying to replicate behaviour similar to NavigationLink, but displaying the new view on a sheet instead of a new screen.

Currently, the list iterates over microphones which is array of my Microphone struct with ForEach, and populates each entry with the Microphone.model parameter and a Button to display a sheet which displays the rest of the information for that Microphone.

However, the first entry in the list displays the detail sheet for microphones[1], and all other entries in the list display the detail sheet for microphones[0]. Is it possible to dynamically generate the contents of a sheet in this way?

This is the Menu view generating the list:

    import SwiftUI

struct BrandMenu: View {

var brand: String

var filteredMicrophones: [Microphone] {
    let allMicrophones: [Microphone] = Bundle.main.decode("MicrophoneTestData.json")
    var filtered: [Microphone] = []
    for microphone in allMicrophones {
        if microphone.brand == brand {
            filtered.append(microphone)
        }
    }
    let sorted = filtered.sorted { $0.model < $1.model }
    return sorted
}

@State private var showingDetails = false



var body: some View {
        List(filteredMicrophones) { microphone in
            if microphone.brand == self.brand {
                    Button(action: {self.showingDetails = true}) {
                        MicModelRow(microphone: microphone).contentShape(Rectangle())

                    }
                    .buttonStyle(PlainButtonStyle())
                    .sheet(isPresented: self.$showingDetails) {
                        MicDetailView(microphone: microphone, showSheet: self.$showingDetails)
                }

            }
        }
        .navigationBarTitle(Text(brand), displayMode: .inline)

}

}

Neil
  • 141
  • 1
  • 8
  • 1
    You need to move sheet out of list. [See this similar post](https://stackoverflow.com/questions/61176412/can-t-pass-data-correctly-to-modal-presentation-using-foreach-and-coredata-in-sw/61176759?r=SearchResults&s=1|0.0000#61176759) for solution. – Asperi Apr 13 '20 at 13:07
  • Perfect, thank you! – Neil Apr 13 '20 at 13:19

0 Answers0