I have this code:
import SwiftUI
import Combine
struct ContentView: View {
let items = [
Item(name: "Name 1"),
Item(name: "Name 2"),
Item(name: "Name 3")
]
var body: some View {
NavigationView {
List {
ForEach(items, id: \.name) { item in
NavigationLink(
destination: DetailView(name: item.name),
label: {
Text(item.name)
})
}
}
}
}
}
struct DetailView: View {
var name: String
var body: some View {
Text(name)
}
init(name: String) {
self.name = name
print("Initializing detail view for \(self.name)")
}
}
struct Item {
let name: String
}
And it prints out the initializing print out for each of the 3 items.
In my real application, I don't want the detail views to be initialized until the user clicks on the navigation link, because I will be doing API calls in the detail view's initializer, and so I don't want it to be automatically called for each item.