【问题标题】:how to bind fileurls to NavigationLink list SwiftUI [duplicate]如何将文件网址绑定到 NavigationLink 列表 SwiftUI [重复]
【发布时间】:2021-05-23 14:41:28
【问题描述】:

我正在尝试将 fileurls 数组绑定到 NavigationLink 列表我有一个错误

无法将 'Binding' 类型的值转换为预期参数 输入“范围”

struct ContentView: View {

    @State private var fileUrls:[URL] = []
    
    func listDir(dir: String) -> [URL] {
       // list file from directory 
    }
   
    
    var body: some View {
        NavigationView {
            ZStack {
                VStack {
                    List{
                        ForEach($fileUrls) { object in
                            NavigationLink(destination: PDFKitView(url: self.fileUrl1), isActive: $viewLocalPDF) {
                                Button("Swift Style"){
                                    self.viewLocalPDF = true
                                }.padding(.bottom, 20)
                                
                            }
                        }
                    }
                }
                .navigationBarTitle("Bookshelv ", displayMode: .inline).onAppear {
                    fileUrls = listDir(dir: "pdfs")
                }
                
            }
        }
}

【问题讨论】:

    标签: ios list foreach swiftui


    【解决方案1】:

    通过使用$,您指的是fileUrlsBinding。您不需要 ForEach 的绑定,只需要值。

    由于 URL 不符合 Identifiable,您还需要告诉它如何识别每个项目。为此,您可以使用\.self

    ForEach(fileUrls, id: \.self) { object in
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-21
      • 1970-01-01
      • 2018-03-07
      • 1970-01-01
      相关资源
      最近更新 更多