Skip to content

Instantly share code, notes, and snippets.

@anupamchugh
Created June 23, 2022 17:00
Show Gist options
  • Save anupamchugh/eba92fc51dbc5e155adfdc3770d31d99 to your computer and use it in GitHub Desktop.
Save anupamchugh/eba92fc51dbc5e155adfdc3770d31d99 to your computer and use it in GitHub Desktop.
import SwiftUI
final class Router: ObservableObject {
@Published var path = NavigationPath()
}
struct ContentView: View {
@EnvironmentObject var router: Router
var body: some View {
NavigationStack(path: $router.path) {
RowListsView()
.navigationDestination(for: Int.self) { i in
RowListsView()
}
}
.environmentObject(router)
}
}
struct RowListsView : View{
@EnvironmentObject var router: Router
var body: some View{
Form{
List(1..<5) { i in
NavigationLink(value: i) {
Text("\(i)")
}
}
Section{
if router.path.count > 0
{
Button("Screen count \(router.path.count)"){
print("")
}
Button("Pop to root", role: .destructive){
router.path = .init()
}
Button("Jump back two screens", role: .none){
if router.path.count >= 2{
router.path.removeLast(2)
}
else if router.path.count >= 1{
router.path.removeLast(1)
}
}
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static let envObject = Router()
static var previews: some View {
ContentView()
.environmentObject(envObject)
}
}
@AmrAbedal
Copy link

@Piggyback13 Yah , it solved , change line 9 to be
@StateObject var router: Router = Router()

@AmrAbedal
Copy link

getting this runtime warning

Publishing changes from within view updates is not allowed, this will cause undefined behavior.

on line https://gist.github.com/anupamchugh/eba92fc51dbc5e155adfdc3770d31d99#file-swiftui-new-navigationapis-swift-L43

from my search , some folks , say that this warning is an Xcode bug .

@Piggyback13
Copy link

@AmrAbedal thank you man

@jason19970210
Copy link

jason19970210 commented Mar 15, 2023

I had a code snippet as below where I modified the navigationDestination with isPresented argument to control when will the view change, but at the end of the SecondView, the reset button didn't bring the navigationStack to the root view with ContentView, it just do nothing when pressing the reset button.

I have tried wrapping the body within the NavigationStack(path: $router.path), but still not working.

import SwiftUI

final class Router: ObservableObject {
    
    @Published var path = NavigationPath()
    
}

struct ContentView: View {
    
    @StateObject var router: Router = Router()
    
    @State private var showFirstView: Bool = false
    
    var body: some View {
        
        NavigationStack(path: $router.path) {
            
            Button("Start") {
                showFirstView = true
            }
            
            .navigationDestination(isPresented: $showFirstView) {
                FirstView()
            }
            
            .navigationTitle("Home")
        }
        .environmentObject(router)
    }
}


struct FirstView: View {
    
    @EnvironmentObject var router: Router
    @State private var showSecondView: Bool = false
    
    var body: some View {
        
        Button("Go second") {
            showSecondView = true
        }
        
        .navigationDestination(isPresented: $showSecondView){
            SecondView()
        }
    }
    
}

struct SecondView: View {
    
    @EnvironmentObject var router: Router
    
    var body: some View {
        
        Button("reset"){
            router.path = .init()
        }
        
    }
    
}

Appreciate for the help. Thanks !!

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