简体   繁体   中英

How do I navigate between these two lists?

I'm working on a test project. My goal is to be able to navigate from HomeList to SampleDataList and then to some different view that's based on the URL file type. I do not want to use Master/Detail.

With the code given below, I get the following error:

Note: Links search for destinations in any surrounding NavigationStack, then within the same column of a NavigationSplitView. A NavigationLink is presenting a value of type "URL" but there is no matching navigationDestination declaration visible from the location of the link. The link cannot be activated.

I have tried...

  • Moving navigationDestination(for:destination:) to HomeList . This doesn't work.
  • Embedding SampleDataList in a NavigationStack . This doesn't work either.
  • Using a NavigationPath . I'm not sure I did this correctly, but it didn't work either.

    struct HomeList: View {
        
        var body: some View {
            NavigationStack {
                List {
                    NavigationLink {
                        SampleDataList()
                    } label: {
                        Text("Sample Data Picker")
                    }
                }
            }
        }
    }

    extension URL: Identifiable {
        
        public var id: Int { self.hashValue }
        
    }

    struct SampleDataList: View {
        
        var urls: [URL] {
            let path = Bundle.main.resourcePath!
            let url = URL(fileURLWithPath: path).appendingPathComponent("SampleData")
            return try! FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil)
        }
        
        var body: some View {
            List(urls) { url in
                NavigationLink(value: url) {
                    Text("\(url.lastPathComponent)")
                }
            }
            .listStyle(PlainListStyle())
            .navigationDestination(for: URL.self) { url in
                Text("\(url.lastPathComponent)")
            }
        }
    }

I've figured out a solution using NavigationPath . I added a navigation path to HomeList and use an enum and navigationDestination(for:destination:) for navigating my static list...

struct HomeList: View {
    
    @State var path = NavigationPath()
    
    enum HomeListOptions: String, CaseIterable {
        case sampleDataPicker = "Sample Data Picker"
        // TODO: Add more cases here
    }

    var body: some View {
        NavigationStack(path: $path) {
            List(HomeListOptions.allCases, id: \.self) { option in
                NavigationLink(option.rawValue, value: option)
            }
            .navigationDestination(for: HomeListOptions.self) { option in
                switch option {
                case .sampleDataPicker:
                    SampleDataList()
                }
            }
        }
    }
}

I kept my SampleDataList the same. It uses a navigationDestination(for:destination:) that handles URLs...

struct SampleDataList: View {
            
    var urls: [URL] {
        let path = Bundle.main.resourcePath!
        let url = URL(fileURLWithPath: path).appendingPathComponent("SampleData")
        return try! FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil)
    }
    
    var body: some View {
        List(urls) { url in
            NavigationLink(value: url) {
                Text("\(url.lastPathComponent)")
            }
        }
        .listStyle(PlainListStyle())
        .navigationDestination(for: URL.self) { url in
            Text("\(url.lastPathComponent)")
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM