I’m using SwfitUI in my project and I have a NavigationView and List. I’m clicking cell after open the detail view and click navigation back button. I want to remove view (i
You just need to defer your destination creation in your builder, and the @ViewBuilder is a good instrument for this.
It can be used the following wrapper for to create real destination only in when body will be rendered (ie. explicitly in time of navigation clicked in your case)
Tested with Xcode 11.4 / iOS 13.4
struct DeferView: View {
let content: () -> Content
init(@ViewBuilder _ content: @escaping () -> Content) {
self.content = content
}
var body: some View {
content() // << everything is created here
}
}
and now your builder
final class DetailViewBuilder {
static func make(object: Something) -> some View {
DeferView {
DetailView(viewModel: DetailViewModel(object: object))
}
}
}