ios – SwiftUI’s new NavigationStack warning about navigationDestination not found but I did provide the type


So I have this minimum repro code:

struct ListViewWithNewAPI: View {
  @State var selectedString: String? = nil
  
  var body: some View {
    let details = ["foo", "bar", "baz"]
    
    NavigationStack {
      List {
        ForEach(details, id: \.self) { detail in
          NavigationLink(detail, value: detail)
        }
      }
      .navigationDestination(item: $selectedString) { detail in
        Text("This is a detailed page for \(detail)")
          .navigationTitle("Detail page")
      }
      .navigationTitle("List page")
    }
  }
}

When I tap on the link, I got a warning:

A NavigationLink is presenting a value of type “String” but there is no matching navigationDestination declaration visible from the location of the link. The link cannot be activated.

But as you can tell, I did provide the navigationDestination with correct String type. What is going on here?

Edit: This is my old way of writing it:

    List {
      ForEach(details, id: \.self) { detail in
        let destination = Text("This is a detailed page for \(detail)")
          .navigationTitle("Detail page")
        
        NavigationLink(
          detail,
          destination: destination,
          tag: detail,
          selection: $selectedString)
      }
    }

This API has been deprecated, but it was working well – I did not have to manually set $selectedString.

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img