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)
}
}