swiftUI PresentaionLink does not work second time

后端 未结 1 679
一个人的身影
一个人的身影 2020-12-03 12:33

I have a ContentView written in swiftUI as simple as below. \'\'\' var body: some View {

    NavigationView {
        List {
            Section {
               


        
相关标签:
1条回答
  • 2020-12-03 12:52

    PresentationLink has been deprecated in Xcode 11 beta 4 in favor of .sheet, which seems to solve the issue.

    Added improved presentation modifiers: sheet(isPresented:onDismiss:content:), actionSheet(isPresented:content:), and alert(isPresented:content:) — along with isPresented in the environment — replace the existing presentation(_:), Sheet, Modal, and PresentationLink types. (52075730)

    If you change the code to .sheet like below:

    import SwiftUI
    
    struct Testing : View {
        @State var isPresented = false
    
        var body: some View {
            NavigationView {
                List {
                    Button(action: { self.isPresented.toggle() })
                        { Text("Source View") }
                    }
                }.sheet(isPresented: $isPresented, content: { Text("Destination View") })
        }
    }
    

    You will then be able to use the modal as many times as you like instead of just once.

    EDIT: After implementing this in a real scenario, I've found that the underlying bug still seems to exist if you put .sheet inside of the List. If you follow the code example above, you won't experience this issue but in a real scenario where you're using a List, you're probably going to want information about the particular item that was selected passed in to the modal. In that case, you're going to need to pass information about the selection via a @State var or some other means. Below is an example:

    import SwiftUI
    
    struct Testing : 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)
提交回复
热议问题