Michael Long
1 min readJul 13, 2019

--

Nice! As did you, I saw NavigationLink and spent most of an afternoon crashing my app in an attempt to get it to work.

Note the code can be cleaned up just a bit…

struct DetailView: View {
var dismiss: PassthroughSubject<Void, Never>
var body: some View {
Button("Here are details. Tap to go back.") {
self.dismiss.send()
}
}
}
struct RootView: View {
var link: NavigationDestinationLink<DetailView>
let linkDismissed = PassthroughSubject<Void, Never>()
init() {
self.link = NavigationDestinationLink(
DetailView(dismiss: linkDismissed),
isDetail: false
)
}
var body: some View {
VStack {
Button("I am root. Tap for more details.") {
self.link.presented?.value = true
}
}
.onReceive(linkDismissed, perform: { _ in
self.link.presented?.value = false
})
}
}

Subjects are reference types, so you can just hand the root subject directly to the detail view.

I wonder if this works for multiple pushed views? Hmmm….

--

--

Michael Long
Michael Long

Written by Michael Long

I write about Apple, Swift, and SwiftUI in particular, and technology in general. I'm also a Lead iOS Engineer at InRhythm, a modern digital consulting firm.

No responses yet