Skip to content

Instantly share code, notes, and snippets.

@helje5
Last active June 21, 2021 07:48
Show Gist options
  • Save helje5/38295beb7473a83ad7d236f99172d256 to your computer and use it in GitHub Desktop.
Save helje5/38295beb7473a83ad7d236f99172d256 to your computer and use it in GitHub Desktop.
Sample showing how programmatic SwiftUI navigation doesn't work and ignores the source of truth.
// Created by Miroslav Djukic on 9.6.21.
import SwiftUI
struct AView: View {
@EnvironmentObject var navigatonState: NavigationState
var body: some View {
NavigationView {
VStack {
Text("AView")
Spacer().frame(height: 30)
NavigationLink("Next", destination: BView(), isActive: $navigatonState.bViewActive)
Button("Go to D", action: navigatonState.navigateToDView)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.black)
}
}
}
struct BView: View {
@EnvironmentObject var navigatonState: NavigationState
var body: some View {
VStack {
Text("BView")
Spacer().frame(height: 30)
NavigationLink("Next", destination: CView(), isActive: $navigatonState.cViewActive)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.red)
}
}
struct CView: View {
@EnvironmentObject var navigatonState: NavigationState
var body: some View {
VStack {
Text("CView")
Spacer().frame(height: 30)
NavigationLink("Next", destination: DView(), isActive: $navigatonState.dViewActive)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.green)
}
}
struct DView: View {
var body: some View {
VStack {
Text("DView")
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.yellow)
}
}
final class NavigationState: ObservableObject {
@Published var bViewActive = false
@Published var cViewActive = false
@Published var dViewActive = false
// This doesn't work normal with any delay less then 0.5s, toggle if/else below to see
func navigateToDView() {
#if true // doesn't work
bViewActive = true
cViewActive = true
dViewActive = true
#else // works
bViewActive = true
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
self?.cViewActive = true
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self?.dViewActive = true
}
}
#endif
}
}
@zntfdr
Copy link

zntfdr commented Jun 21, 2021

My FB# on the matter: FB9197698

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment