0

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.

Colin Whooten
  • 244
  • 1
  • 7

1 Answers1

-1

It’ll work if you create a view for list row and move .sheet() there

Igor
  • 89
  • 1
  • 5
  • Can you further explain your answer? I got this to work by moving the sheet outside of the list- which has the downside of complicating the button code (in my actual code, not this example, the button was in a sub view called row). It’d be great if I could keep that, my solution got messy quickly as I’m now passing data back and forth between the list and child view. This branch from a blog post helped https://github.com/maeganjwilson/swiftui-show-modal-tutorial/tree/list-example – Colin Whooten Sep 06 '19 at 10:32
  • @ColinWhooten I have to get back to you with that — I've made a whole app on that but reducing the real code to sharable one strangely gets same mistake – Igor Sep 06 '19 at 15:13