.sheet: Shows only once and then never again

前端 未结 1 1789
傲寒
傲寒 2021-01-19 11:06

Working with Beta4, it seems that the bug is still existing. The following sequence of views (a list, where a tap on a list entry opens another list) allows to present the <

相关标签:
1条回答
  • 2021-01-19 11:24

    This is a variant of swiftUI PresentaionLink does not work second time

    The following simplified code exhibits the behavior you're experiencing (the sheet only displays once):

    import SwiftUI
    
    struct ContentView: View {
        @State var isPresented = false
        @State var whichPresented = -1
    
        var body: some View {
            NavigationView {
                List {
                    ForEach(0 ..< 10) { i in
                        Button(action: {
                                self.whichPresented = i
                                self.isPresented.toggle()
                            })
                            { Text("Button \(i)") }
                        }.sheet(isPresented: $isPresented, content: { 
    Text("Destination View \(self.whichPresented)") })
                    }
                }
        }
    }
    

    There appears to be a bug in SwiftUI when you put the .sheet inside a List or a ForEach. If you move the .sheet outside of the List, you should be able to get the correct behavior.

    import SwiftUI
    
    struct ContentView: View {
        @State var isPresented = false
        @State var whichPresented = -1
    
        var body: some View {
            NavigationView {
                List {
                    ForEach(0 ..< 10) { i in
                        Button(action: {
                                self.whichPresented = i
                                self.isPresented.toggle()
                            })
                            { Text("Button \(i)") }
                        }
                    }
                }.sheet(isPresented: $isPresented, content: { Text("Destination View \(self.whichPresented)") })
        }
    }
    
    0 讨论(0)
提交回复
热议问题