I'm trying to present a modal within a List. Modals have changed many times in the betas, but I believe I'm using the correct/latest way (as of Xcode Beta 7). However, when I'm triggering the modal from within a list, for each list item it opens once but then never again.
import SwiftUI
//This works perfectly fine
struct BasicTest : View {
struct SampleObject: Identifiable {
var id = UUID()
var name: String
}
let sampleObjects = [
SampleObject(name: "Buffalo"),
SampleObject(name: "Italy"),
SampleObject(name: "Portland"),
]
@State private var showModal:Bool = false
var body: some View {
Button(action: {
print("Modal to open")
self.showModal = true
}) {
Text("Show Modal")
}.sheet(isPresented: self.$showModal)
{
TestDetailView(name: "Example")
}
}
}
#if DEBUG
struct BasicTest_Previews: PreviewProvider {
static var previews: some View {
BasicTest()
}
}
#endif
//this one only opens once
struct ListTest : View {
struct SampleObject: Identifiable {
var id = UUID()
var name: String
}
let sampleObjects = [
SampleObject(name: "Buffalo"),
SampleObject(name: "Italy"),
SampleObject(name: "Portland"),
]
@State private var showModal:Bool = false
var body: some View {
List {
ForEach(sampleObjects) {
item in
Button(action: {
print("Modal to open")
self.showModal = true
}) {
Text("Show Modal for \(item.name)")
}.sheet(isPresented: self.$showModal)
{
TestDetailView(name: item.name)
}
}
}
}
}
#if DEBUG
struct ListTest_Previews: PreviewProvider {
static var previews: some View {
ListTest()
}
}
#endif
struct TestDetailView: View {
@Environment(\.presentationMode) var presentationMode
var name: String
var body: some View {
VStack {
Button(action: {
print("Button clicked")
self.presentationMode.wrappedValue.dismiss()
}) {
Image(systemName: "chevron.compact.down").font(Font.system(.largeTitle).bold())
}
Text(name)
}
}
}
No error messages exist that I can see, just a flash when trying to open a second time.