Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I want to pop to the root view when a flow is finished in SwiftUI. How can I do this? #10

Open
Rylaa opened this issue Jul 1, 2024 · 2 comments

Comments

@Rylaa
Copy link

Rylaa commented Jul 1, 2024

I have a flow that continues with push, and when the flow is finished, I want to return to the dashboard, but I couldn't figure out how to do it.

@Rylaa
Copy link
Author

Rylaa commented Jul 1, 2024

how can i do?
.popToRoot(DashboardView.self)

@trunghvbk
Copy link

You can use .popToRoot
In the below sample code, you can see we use it to back from BView to root view (ContentView)

import SwiftUI
import Coordinator

@main
struct CoordinatorTestApp: App {
    var body: some Scene {
        WindowGroup {
            NavigationView {
                ContentView()
            }
        }
    }
}

enum ContentDestination {
    case aView
}

class ContentCoordinator: AppKitOrUIKitWindowCoordinator<ContentDestination> {
    override func transition(for route: ContentDestination) -> ViewTransition {
        switch route {
        case .aView:
            return .push(AView())
        }
    }
}

struct ContentView: View {
    @StateObject var coordinator = ContentCoordinator()
    var body: some View {
        VStack {
            Button {
                coordinator.trigger(.aView)
            } label: {
                Text("Go To AView")
            }
        }
        .padding()
        .coordinator(coordinator)
    }
}

struct AView: View {
    @StateObject var coordinator = ACoordinator()

    var body: some View {
        VStack(spacing: 16) {
            Button {
                coordinator.trigger(.popBack)
            } label: {
                Text("Back")
            }

            Button {
                coordinator.trigger(.bView)
            } label: {
                Text("Go To BView")
            }
        }
        .coordinator(coordinator)
    }
}

enum ADestination {
    case popBack
    case bView
}

class ACoordinator: AppKitOrUIKitWindowCoordinator<ADestination> {
    override func transition(for route: ADestination) -> ViewTransition {
        switch route {
        case .popBack:
            return .pop
        case .bView:
            return .push(BView())
        }
    }
}

struct BView: View {
    @StateObject var coordinator = BCoordinator()

    var body: some View {
        VStack {
            Button {
                coordinator.trigger(.popToRoot)
            } label: {
                Text("Back To Root")
            }
        }
        .coordinator(coordinator)
    }
}

enum BDestination {
    case popToRoot
}

class BCoordinator: AppKitOrUIKitWindowCoordinator<BDestination> {
    override func transition(for route: BDestination) -> ViewTransition {
        switch route {
        case .popToRoot:
            return .popToRoot
        }
    }
}


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants