I am trying to implement enum based navigation with SwiftUINavigation framework. In my main app I try to setup deeplinking but I faced a problem with navigation links. When I change destination, the app closes the initial screen but doesn't open the new screen.
It seems NavigationLink binding nullifies the value a second time when there is already a new value. When I click Open Second Screen button on the first screen I expect it to close the first screen and open the second screen. Here is a sample code:
import SwiftUI
import SwiftUINavigation
struct ContentView: View {
enum Destination {
case first
case second
}
@State var destination: Destination?
var body: some View {
NavigationView {
VStack {
NavigationLink(
unwrapping: $destination.case(/Destination.first),
onNavigate: { isActive in
if isActive {
destination = .first
}
},
destination: { token in
NextView(buttonTitle: "Open Second View") {
destination = .second
} close: {
destination = nil
}
},
label: {
Text("Open First View")
}
)
NavigationLink(
unwrapping: $destination.case(/Destination.second),
onNavigate: { isActive in
if isActive {
destination = .second
}
},
destination: { token in
NextView(buttonTitle: "Open First View") {
destination = .first
} close: {
destination = nil
}
},
label: {
Text("Open Second View")
}
)
}
}
}
}
struct NextView: View {
let buttonTitle: String
let closure: () -> Void
let close: () -> Void
var body: some View {
VStack {
Button(buttonTitle, action: closure)
Button("Close", action: close)
}
}
}
I am using Xcode 14.2, Simulator iPhone 8 iOS 15.5
So the framework
SwiftUINavigation
doesn't seem to be working for you. You are also usingNavigationView
, which is deprecated.I would suggest you try implementing it using a
NavigationStack
, which works with enums natively.Here is an example implementation. When you go from First -> Second -> First, it goes back to the previous page, instead of going deeper. This is done in the function
goToTarget
. You could change the implementation if you wanted it to behave differently.