Comments (7)
Oops. I realized there is no way to initialize NavigationStack with a coordinator yet. I've created an issue:
from stinsen.
Hi! There's a version 2.0.0 now that supports deep-links. Please try it out and see if you can use it in your project :)
from stinsen.
Yes, that is intended behavior, since it hasn't appeared earlier in the tree. So your case I would probably write:
final class ACoordinator: NavigationCoordinatable {
var navigationStack = NavigationStack()
enum Route: NavigationRoute {
case c
case b
}
func resolveRoute(route: Route) -> Transition {
switch route {
case .c:
return .modal(NavigationViewCoordinator(CCoordinator()).eraseToAnyCoordinatable())
case .b:
return .modal(NavigationViewCoordinator(BCoordinator()).eraseToAnyCoordinatable())
}
}
@ViewBuilder
func start() -> some View {
AView()
}
}
struct AView: View {
@EnvironmentObject var aRouter: NavigationRouter<ACoordinator.Route>
var body: some View {
VStack {
Button {
aRouter.route(to: .b)
} label: {
Text("TestB")
}
Button {
aRouter.route(to: .c)
} label: {
Text("TestC")
}
}
}
}
and also define C in BCoordinator.
If this creates a too large ACoordinator, or if you want the views to be A->B->C instead of A->C when the user is pressing back button, you might be able to do something like this:
final class ACoordinator: NavigationCoordinatable {
var navigationStack = NavigationStack()
enum Route: NavigationRoute {
case b(startWithC: Bool)
}
func resolveRoute(route: Route) -> Transition {
switch route {
case .b(let startWithC):
return .modal(NavigationViewCoordinator(BCoordinator(startWithC: startWithC)).eraseToAnyCoordinatable())
}
}
@ViewBuilder
func start() -> some View {
AView()
}
}
struct AView: View {
@EnvironmentObject var aRouter: NavigationRouter<ACoordinator.Route>
var body: some View {
VStack {
Button {
aRouter.route(to: .b(false))
} label: {
Text("TestB")
}
Button {
aRouter.route(to: .b(true))
} label: {
Text("TestC")
}
}
}
}
and in BCoordinator you can maybe init the NavigationStack with CCoordinator
from stinsen.
Thank you so much! I'll try it 😆
from stinsen.
Hi!
If you mean you need to navigate to every screen in the app through deep links, yes, that would be correct. You can also create a "tree", so for instance if your deep link needs to open a specific tab and navigate from that, that should also be possible.
from stinsen.
Thank you for your answer.
In fact, I don't need a tree, I just wanted to make Deep-links that can from the first screen to any screen using the coordinators that I made before.
So I tried to put several different coordinators in the first view.
But I don't know if it's the nature of EnvironmentObject.
In View
created by Start()
of ACoodinator
, only ACoodinator
works and attempts to access other coordinators, such as BCoodinator
, results in the following error:
SwiftUI:0: Fatal error: No ObservableObject of type NavigationRouter<Route> found.
A View.environmentObject(_:) for NavigationRouter<Route> may be missing as an ancestor of this view.
Codes are roughly like this.
final class ACoordinator: NavigationCoordinatable {
var navigationStack = NavigationStack()
enum Route: NavigationRoute {
case b
}
func resolveRoute(route: Route) -> Transition {
switch route {
case .b:
return .modal(NavigationViewCoordinator(BCoordinator()).eraseToAnyCoordinatable())
}
}
@ViewBuilder
func start() -> some View {
AView()
}
}
struct AView: View {
@EnvironmentObject var aRouter: NavigationRouter<ACoordinator.Route>
@EnvironmentObject var bRouter: NavigationRouter<BCoordinator.Route>
var body: some View {
VStack {
Button {
// Works well
aRouter.route(to: .b)
} label: {
Text("TestA")
}
Button {
// Crash
bRouter.route(to: .c)
} label: {
Text("TestB")
}
}
}
}
If this is intended, what structure should I fill out to do what I want?
from stinsen.
Thank you for your kind answer.
from stinsen.
Related Issues (20)
- PopToRoot shows second view while transitioning from third to first view HOT 6
- How can I get access to a stack during popToRoot
- Problem with .push transition. HOT 2
- Can't I use onDismiss in route method? HOT 1
- Dismissing item issue HOT 2
- Exclude screens during deeplink HOT 1
- Issues caused by use of SwiftUI's NavigationStack HOT 2
- Can't get NavigationRouter via protocol
- Tabbar items customisation HOT 2
- dismissCoordinator animation
- dismissChild fatal optional
- How to correctly use focusFirst HOT 1
- Example just don't work
- Tabbar Item Accent Color Not working
- Presentation detents for sheets
- Memory leaks in iOS17 HOT 5
- How do I know which route I am in?
- Custom back button modifier HOT 1
- hasRoot / isRoot crashes app HOT 1
- Disable tabCoordinator when show Custom Alert HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stinsen.